A chat system looks simple: send a message, the other person sees it. Under the hood it needs persistent connections to millions of devices, a way to find which server holds each user's connection, ordering within each conversation, and a plan for everyone who is offline.
Getting messages to the device
| Transport | How | Fits |
|---|---|---|
| Short polling | The client asks for new messages every few seconds. | Simple, but slow and wasteful. |
| Long polling | The server holds the request open until a message arrives or it times out. | Works everywhere, but reconnects after every message. |
| Server-sent events | One long HTTP response streams events, server to client only. | Live feeds and notifications. |
| WebSocket | One persistent, two-way connection. | Chat, multiplayer, collaborative editing. |
Chat uses WebSockets. Mobile apps also use push notifications (APNs, FCM) to wake the app when it's in the background.
Where connection state lives
A WebSocket is attached to one gateway server, so the gateways are stateful and everything behind them stays stateless:
- Session registry: when a client connects, its gateway writes
user_id → gateway_idto Redis with a TTL that heartbeats refresh. A user on three devices has three entries. - Routing: the chat service stores the message, looks up each recipient's gateways, and publishes to those gateways, through a per-gateway channel in Redis pub/sub, an internal RPC, or a message broker.
- Capacity: a tuned gateway holds hundreds of thousands of mostly idle connections, so 50M concurrent users need a few hundred gateways. Memory per connection is the limit.
- Deploys and failures: when a gateway dies, its clients reconnect (with backoff and jitter, so they don't all return at once) to another gateway, which re-registers them. Clients then resync anything they missed (below).
Ordering within a conversation
You don't need global ordering, only ordering within each conversation, and client clocks can't be trusted for that.
- Give each conversation a single writer: partition by
conversation_id(a Kafka partition or a database shard owns each conversation). That writer assigns a monotonically increasing sequence number to every message. - Clients sort by sequence number and spot gaps ("I have 41 and 43, fetch 42").
- Clients attach a
client_msg_idto every send, so a retry after a dropped connection doesn't post the message twice.
Storage and receipts
Messages are written constantly and read by conversation, newest first, which fits a wide-column store partitioned by conversation_id and sorted by sequence number (see SQL vs NoSQL). For receipts, don't store a row per message per reader. Store watermarks per member:
One small write per member updates every receipt at once, and "seen by 4 of 7" in a group is a simple count.
Offline users
Messages are saved before delivery, so "offline" just means "not delivered yet". When a client reconnects, it sends its last seen sequence number per conversation, and the server returns everything newer. While the user has no live connection, the chat service also asks the notification service to send a push. A per-user list of conversations with unread messages makes the inbox view cheap to load.
Presence and groups
- Presence ("online", "last seen") comes from heartbeats with a TTL. Broadcasting every change to every contact is expensive, so send presence only to contacts who currently have that conversation open, and fetch it on demand otherwise.
- Small groups fan out each message to every member's gateway. Huge channels (100,000 members) switch to pull: clients fetch new messages for the channels they're viewing. It's the same trade-off as the news feed.
Interview checklist
- Choose WebSockets, and say where connection state lives (a session registry).
- Route messages through stateless services to the right gateway.
- Order with per-conversation sequence numbers and a single writer; dedupe with client message IDs.
- Model receipts as per-member watermarks.
- Persist first, sync on reconnect, push to offline devices.
Model it in the sandbox
Set the traffic from your estimate, then build the read and write paths and see which component turns red first.
Open the sandboxDo it by hand
Interactive drills for the parts the simulator can't show. Scored, with your best result saved in your browser.
Further reading
- Alex Xu, System Design Interview: An Insider's Guide, volumes 1 and 2. Worked case studies in interview format.
- Martin Kleppmann, Designing Data-Intensive Applications (O'Reilly). The standard book on replication, partitioning and consistency.
- Site Reliability Engineering, Google's free book on running reliable systems.
- The Amazon Builders' Library: practical articles on timeouts, retries, caching and load shedding.
- The System Design Primer: a broad, community-maintained overview.