Load/Bearing
High-level design · 15 of 22

Chat systems and real-time connections

Millions of open connections, messages that must arrive in order, and users who are offline half the time.

3 min read

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

TransportHowFits
Short pollingThe client asks for new messages every few seconds.Simple, but slow and wasteful.
Long pollingThe server holds the request open until a message arrives or it times out.Works everywhere, but reconnects after every message.
Server-sent eventsOne long HTTP response streams events, server to client only.Live feeds and notifications.
WebSocketOne 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.

CLIENT AGATEWAY 1holds A's socketCHAT SERVICEpersist + routeGATEWAY 7holds B's socketCLIENT B
The chat service saves the message, looks up which gateway holds the recipient's connection in the session registry, and forwards it there.

Where connection state lives

A WebSocket is attached to one gateway server, so the gateways are stateful and everything behind them stays stateless:

Ordering within a conversation

You don't need global ordering, only ordering within each conversation, and client clocks can't be trusted for that.

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:

messages: (conversation_id, seq) → sender_id, body, sent_at, client_msg_id receipts: (conversation_id, user_id) → last_delivered_seq, last_read_seq message 120 is "read by everyone" when min(last_read_seq over members) ≥ 120

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

Interview checklist

Practise it

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 sandbox
Workshop drill

Do it by hand

Interactive drills for the parts the simulator can't show. Scored, with your best result saved in your browser.

Chat correctness

Further reading