Back to blog

Ten million users want direct messages: could you design the chat system?

Thắng Đoàn
Thắng Đoàn

FotoFlow's last feature is direct messages: ten million daily users, one-to-one and small-group chat, messages arriving instantly when both people are online and waiting when they are not. The priorities are the opposite of the feed: a post may be late and nobody is harmed, but here the promise is strict, no message lost, messages in order, each shown exactly once, and speed, under 200 ms online, never buys a violation of those three.

Numbers, including a new one

Ten million users sending 40 messages each is 400 million messages a day, about 4,600 a second, at 200 bytes each about 80 GB a day. The unusual number is connections: chat is pushed, not polled, so every online user holds a connection open, 20 million at peak. One well-tuned server holds a few hundred thousand mostly-idle sockets, so that is around a hundred servers that do nothing but hold connections.

Dumb gateways and a registry

Each phone opens a WebSocket, one long-lived connection the server can push down at any moment. Twenty million sockets need about a hundred servers, so the roles split: gateways do nothing but hold connections, and the stateless chat service behind them holds the logic, so logic can deploy daily without dropping a single socket. Alice is on gateway 1 and Bob on gateway 2, so when Alice's message arrives, the service needs to know where Bob lives: a registry entry, bob -> gateway 2, with a short TTL that heartbeats keep refreshing. A missing or stale entry breaks nothing; Bob is treated as offline and gets the message when he reconnects.

Store first, then sync

The rule that keeps the promise: store first, then deliver. The chat service writes each message to a durable store and only then tells Alice "sent." In the same step it stamps the message with the conversation's next sequence number: order is decided once, by the server, never by the phones' clocks, which disagree. The store is write-heavy and always read the same way, "the latest messages of conversation X," which fits a wide-column store like Cassandra, partitioned by conversation id and sorted by sequence.

When Bob is offline, a push notification wakes his phone; when he reconnects, his app says "I have conversation 88 up to 41" and the server replies with everything after it. One mechanism covers a five-second gap, a week offline, or a brand-new device. And when Alice's "sent" reply is lost and her app retries, the duplicate is caught because the app gave every message a unique id before sending and reuses it on retries; the server acknowledges a stored id again. At-least-once delivery plus idempotent receivers adds up to exactly once on the screen.

What this does not solve

Groups store once and deliver to many, fan-out on write like the feed, which works to a few hundred members; a 100,000-member channel becomes the celebrity problem again and switches to fetch-on-open. Presence is its own trap: every connect and disconnect may update millions of contact screens, so treat it as ephemeral, derive "online" from registry heartbeats, never store "typing" at all, and let the next heartbeat correct a lost update. One product, two consistency levels: messages are durable and ordered; presence is best-effort. And when a gateway crashes, 200,000 sockets drop at once with nothing lost, because everything was stored first; the reconnect storm is the real risk, so apps reconnect with exponential backoff and jitter, the balancer spreads them, and gateways are drained gently before deploys. End-to-end encryption would change what the server can read, not this architecture: it stores and forwards ciphertext.

A gateway holding 200,000 sockets dies right now. How long before every one of those users is fully caught up, and what protects them meanwhile?
Your app must work one week offline. Which single mechanism in this design already covers it?

The series is complete: from one server and 100 users to ten million people messaging each other. Start again from the beginning: How a photo app survives ten million users.

Sources

Discord Blog: How Discord Scaled

MDN: WebSockets

Amazon Dynamo Paper

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.