"Design WhatsApp" looks like a chat-box problem, and it isn't. The messages are tiny. What makes it hard is that half a billion people each hold a long-lived connection to you, the person you are talking to is often not connected at all, and nobody forgives a lost message.
The interviewer is testing four things. Can you reason about connections as a scarce resource? Can you route a message to the one server that holds the recipient's socket? Do you know what to do when that recipient is offline? And can you say precisely what "delivered" and "read" mean, and what you guarantee about order?
This page is about 1:1 and group delivery. Channels, search and presence at workspace scale are a different problem, covered in Design Slack.
Clarify the requirements
Functional requirements:
- Send a text message to one person, and have it arrive in real time if they are online.
- Group chats of up to 1,024 members.
- Messages sent to an offline user are delivered when they reconnect, and a push notification wakes the phone.
- Show sent, delivered and read receipts.
- Show "online" and "last seen".
- Send images and short videos.
Out of scope, said aloud: calls, status stories, payments, server-side search, multi-device sync.
Non-functional requirements: these decide the architecture.
- No message is lost. Once the server has acknowledged a send, the message must reach the recipient eventually, even if they are offline for a week.
- Per-conversation order. Two messages from the same sender in the same chat must appear in the order they were sent. Global order across chats does not matter.
- Low latency when both sides are online: a few hundred milliseconds end to end.
- Server stores as little as possible. If messages are end-to-end encrypted, the server relays ciphertext it cannot read, which also rules out server-side features that need to read it. Keep messages only until they are delivered.
Estimate the scale
Assume 500 million daily active users, each sending 40 messages a day, mostly 1:1 and some in groups. Assume an average message needs 1.5 deliveries (most go to one person, a few to a group), so each user also receives 60 a day.
Sends: 500M × 40 = 20 billion messages a day. 20B ÷ 86,400 ≈ 230,000 per second on average, and roughly 700,000 per second at peak (3x).
Deliveries: 500M × 60 = 30 billion a day, or about 350,000 per second.
Receipts: each delivery produces a "delivered" receipt and usually a "read" receipt, so up to two more tiny events per delivery. That is as many as 700,000 receipts per second, which is more than the messages themselves.
Connections: if 40% of daily users are connected at the busiest moment, that is 200 million open sockets. A tuned gateway host holds around 100,000, so we need about 200M ÷ 100K = 2,000 gateway hosts.
Storage: if we kept every message for 30 days, that would be 20B × 200 B × 30 = 120 TB. But we only need messages that have not been delivered yet. If about 3% are waiting at any time, that is a few terabytes.
"The bytes are small and the message rate is large but ordinary. What is unusual is 200 million long-lived connections and 2,000 gateway hosts. The hard part is knowing which of 2,000 hosts holds the recipient, and not losing the message when the answer is none of them."
The capacity estimator reproduces the send rate and the 120 TB upper bound.
API and data model
Most traffic is not request and response. The client opens one WebSocket and everything flows over it as frames.
client -> server SEND { clientMsgId, chatId, to[], ciphertext }
server -> client ACK { clientMsgId, serverMsgId, seq }
server -> client DELIVER { serverMsgId, chatId, from, seq, ciphertext }
client -> server RECEIPT { serverMsgId, type: delivered | read }
client -> server SYNC { lastSeqSeen } (on reconnect)
POST /v1/media -> { uploadUrl, mediaId }The server stores one thing per recipient: an inbox of messages they have not acknowledged.
inbox (user_id PK, seq CK, chat_id, from, ciphertext, created_at)
chats (chat_id PK, members[], created_at)
session user_id -> { gateway_id, connected_at } (Redis, short TTL)The non-obvious choice: the inbox is partitioned by recipient, not by chat. Every read the system makes is "give me what user U has not yet seen", so that is the key. A group message is written once per member, which is a deliberate use of fan-out on write.
High-level design
We simulate the core delivery path: clients hold WebSocket connections to a gateway fleet, a message service receives each send, looks up the recipient's session in a fast presence store, and writes the message to a durable inbox.
- Clients keep one long-lived connection each to the WebSocket gateway.
- The message service looks up where the recipient is connected, then writes the message to the durable inbox.
Describe this diagram
Components
- clients (Client)
- ws gateway (WebSocket gateway)
- message service (Service)
- session registry (Redis)
- inbox (Cassandra)
Connections
- clients sends to ws gateway
- ws gateway sends to message service
- message service sends to session registry
- message service sends to inbox
Run it, then try cutting the gateway's connection limit or the inbox's node count. Connections run out before bytes do.
The write path: the sender's frame reaches a gateway, which hands it to the message service. The service writes the message to the recipient's inbox before acknowledging the sender. Only then does the sender see one tick.
The read path for an online recipient: the service looks up the recipient's session, finds the gateway ID, and forwards the message to that gateway, which pushes it down the socket. The recipient's app sends a delivered receipt, and the inbox row can be deleted.
The read path for an offline recipient: there is no session. The message waits in the inbox, a push notification goes out through APNs or FCM, and when the app reconnects it sends SYNC with the last sequence number it saw and receives everything after it.
The interesting problem: routing a message to a connection
With 2,000 gateways, the recipient's socket lives on exactly one of them, and it moves every time the phone reconnects. There are three ways to deal with that.
Every client asks "anything new?" every few seconds.
Approach B: route by user hash
Hash each user ID to a gateway, and make every client connect to its own home gateway. To reach user U, any server computes hash(U) and forwards there. No lookup table is needed.
Good: no presence store on the hot path, and routing is a pure function.
Bad: it couples connection placement to the hash. Adding or removing a gateway moves users and forces mass reconnects, even with consistent hashing, and a hot or dead gateway cannot shed its users elsewhere.
Approach C: a session registry, with a durable inbox behind it
Any gateway accepts any client. On connect, the gateway writes user_id -> gateway_id to Redis with a short TTL that it refreshes on heartbeats. The message service reads that key to route.
Good: clients can connect anywhere, a gateway can be drained freely, and a dead gateway's entries expire by themselves.
Bad: the registry can be wrong. A phone may have dropped a minute ago while its entry lives on, and a forward to the old gateway fails.
The answer is to treat the registry as a hint, never as truth. The inbox is the truth.
Because the message is durable before any routing happens, a stale or missing registry entry costs latency and nothing else: the recipient picks it up on the next SYNC.
Ordering and exactly-once
Networks retry, so the honest guarantee is at-least-once delivery with deduplication. Two ideas make it work.
- Client-generated message IDs. The sender attaches a
clientMsgIdto every send. If the ACK is lost and the app retries, the server sees an ID it already stored and re-sends the ACK. The recipient dedupes byserverMsgIdfor the same reason. This is idempotency applied at both ends. - A per-recipient sequence number. The server assigns each inbox row an increasing
seqas it writes. The client remembers the highestseqit has seen, andSYNCsimply asks for everything above it. Gaps are visible, and order within a chat follows fromseqbecause one sender's messages go through the same inbox partition in sequence.
Groups are where the cost lives. A message to a 1,024-member group is 1,024 inbox writes. That is fine at a cap of around a thousand members, and it is why the cap exists.
Receipts outnumber messages, so batch them: a client opening a chat with 40 unread messages sends one read up to seq N. In a large group, show aggregate state ("read by 12") instead of per-person ticks.
Bottlenecks and failures
A gateway host dies. Its 100,000 users all lose their connection at once and reconnect together. Give clients jittered backoff so they spread over a minute instead of arriving in one second, and keep the handshake cheap. Nothing is lost, because the inbox holds everything unacknowledged.
The inbox store. It takes 230,000 writes a second and a similar rate of deletes. A wide-column store like Cassandra suits this shape: writes are appends, reads are partition-local, and capacity grows by adding nodes. Deleting at that rate leaves tombstones, so use TTLs and time-bucketed partitions rather than many individual deletes.
Hot groups. One busy group multiplies writes by its size. Cap the size and write the copies from a queue, so the sender's ACK never waits on 1,024 writes.
What if the presence store is down? Fall back to treating everyone as offline: write to the inbox, send a push, and let clients sync. Latency gets worse for online users and no message is lost. That is the payoff of making the registry a hint.
Trade-offs to name aloud
- Write to the inbox before routing: an extra durable write on every message, in exchange for never losing one when presence is wrong.
- Fan-out on write for groups: fast reads and simple unread state, at the cost of write amplification that a group-size cap bounds.
- Per-conversation order, not global order: far cheaper, and users never notice.
What interviewers listen for
- A working answer: WebSocket connections, a service that forwards to the recipient's gateway, a database for offline messages and a push notification when the user is offline.
- A strong answer: sizes the connection fleet, makes the inbox the source of truth with the presence registry as a hint, uses client-generated IDs and per-inbox sequence numbers, and explains what happens on reconnect.
- A standout answer: also quantifies receipts as the larger event stream and batches them, caps group size with a reason, plans reconnect storms with jittered backoff, and says what server-side features end-to-end encryption rules out.
Check your understanding
Common mistakes
Choosing HTTP polling and calling it simple. At 200 million clients, polling every few seconds costs tens of millions of mostly empty requests per second and adds seconds of latency. State why you rejected it.
Treating the presence store as the source of truth. If routing depends on the registry being right, every dropped connection loses a message. Make the inbox durable first.
Forgetting receipts. They are the largest event stream and they flow in the reverse direction. A design that sizes only for messages is off by a factor of three.
Ignoring the reconnect storm. The failure of one gateway is a thundering herd of 100,000 clients. Without jitter you turn a single host failure into a fleet-wide incident.
Practice
A user opens the app after two weeks on a plane and has 6,000 pending messages across 200 chats. What goes wrong with a naive SYNC, and how do you fix it?
Write your own answer first, on paper or in a note. Then compare.
Show a hint
Think about the size of one response, the order the user wants to see things, and what the server has to do per message.
Show a strong answer
One giant response blocks everything. Returning 6,000 messages in one reply holds a gateway thread and a large buffer, delays the first visible message, and fails entirely if the connection drops partway. Page the sync: send a batch of a few hundred rows at a time, each ending with a cursor, and have the client ask for the next.
Make acknowledgment cheap. The client should acknowledge a whole batch with one ack up to seq N, and the server deletes rows up to that point in one range operation, not 6,000 individual deletes.
The pattern worth keeping: any "catch up from a cursor" operation needs paging, a summary-first order and range-based acknowledgment, because the worst case is not the average case.
The product team wants multiple devices per account: a phone and a laptop that both receive every message. What changes in your design?
Write your own answer first, on paper or in a note. Then compare.
Show a hint
Ask what a 'recipient' is now, and where the delivered receipt comes from.
Show a strong answer
The recipient becomes a device. Keep one inbox per device, or one per user with a per-device cursor. The simplest approach is a per-device inbox: the sender's message is written once per recipient device, so the 1.5x delivery factor rises with the average number of devices.
The session registry holds a set. user_id now maps to several (device_id, gateway_id) entries. Delivery forwards to each, and each device syncs on its own lastSeqSeen.
The pattern worth keeping: when an entity splits into several (user into devices), re-ask which key your inbox, your registry and your receipts should use.
Key takeaways
- Connections are the scarce resource: 200 million sockets means about 2,000 gateway hosts, not a bandwidth problem.
- Write to the recipient's inbox first, then route. The presence registry is a hint with a TTL.
- At-least-once plus client-generated IDs gives you effectively-once without a coordination protocol.
- Order per conversation with a sequence per inbox. Never promise a global order.
- Receipts are the biggest stream. Batch them, and aggregate in large groups.
Further reading
- I. Fette and A. Melnikov, RFC 6455 "The WebSocket Protocol" (2011): the framing and handshake every chat gateway builds on.
- A. Lakshman and P. Malik, "Cassandra: A Decentralized Structured Storage System", ACM SIGOPS Operating Systems Review (2010): the write-optimized, partitioned store behind inbox-shaped workloads.
- G. DeCandia et al., "Dynamo: Amazon's Highly Available Key-value Store", SOSP (2007): the always-writable, eventually consistent model that inboxes rely on.