vishal patel
LearningAdvancedUpdated 2026-09-23

Design: Real-time Chat

WebSockets at scale — connection gateways, message fan-out, ordering, delivery receipts, presence and offline sync.

websocketsreal-timepub-subordering

Requirements

1:1 and group chat, delivery and read receipts, presence, history, multi-device sync, push when offline. p99 delivery under 200 ms for online users.

Design

diagram

Key decisions

  • Stateful gateways hold connections. A registry maps each user to their gateway node(s).
  • Ordering per channel via a monotonically increasing sequence. Clients sort by seq and detect gaps.
  • Idempotent sends: the client generates a clientMsgId, and the server dedupes on it.
  • Storage: write-heavy and append-only, read by recency. Wide-column stores (Cassandra/ScyllaDB) partitioned by channel and time bucket.
  • Sync: on reconnect, the client sends its last seq per channel and the server returns the delta.
  • Large groups: fan-out on read instead of on write for channels with thousands of members.

Cheatsheet

The whole topic on one page. Click to open full screen.

Sources & further learning

Videos, courses, docs and books I recommend for this topic.

Related topics