How to design WhatsApp
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Estimates
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- Routing between chat servers
- Groups and several devices
- Dead connections and lost messages
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
The problem
A chat app like WhatsApp keeps a long-lived connection open to each phone, pushes new messages down it, and holds anything the recipient missed until they come back. With 200 million people connected at once and a host holding 1 to 2 million sockets, the person who sends and the person who receives usually sit on different machines, so the answer turns on two things: how a message crosses from one chat server to another in well under 500 ms, and what stores it when the far end is offline or the hop between servers drops it.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
Is this one-to-one chat, groups, or both, and how large can a group get? | Group size decides how many copies a single send produces. A cap of 100 keeps a copy-per-recipient inbox affordable; a cap in the thousands would push you toward a very different fan-out. | Both. One person to another, and groups of 2 to 100 people. |
How long do we keep a message on our servers, and do we keep chat history? | A message that waits until delivery and then disappears is a queue; a permanent history is an archive with search and sync. They size storage very differently. | Keep a message until it is delivered, and for 30 days at the longest. History lives on the device. |
How many users, how many connected at the same time, and how chatty are they? | The number connected at once drives the socket servers; the messages a day drive the write path. Those are different bottlenecks. | About 1 billion users, 200 million connected at once, around 20 messages per user a day, which is roughly 4 billion messages a day. |
Do messages carry photos and other media, and how big? | Large bytes through the same servers that hold millions of sockets is a poor mix. If media is in scope, it needs its own path. | Yes, photos and other media are in scope. A message carries a link to the file, not the bytes. |
Can one account be signed in on several devices at once? | Several devices change what an inbox is: a message delivered to the phone still has to reach the laptop, so the unit of delivery becomes the device, not the person. | Start with one device per account, and say how you'd extend to up to three. The groups and devices deep dive covers it. |
How fast must an online recipient see a message, and may any message be lost? | Sub-second delivery rules out polling and favors a push over an open connection; zero loss rules out any path that fires a message and forgets it. | Under 500 ms for someone who is online, and no message may be lost. Delivery may be late, not missing. |
Do we need strict ordering, read receipts or online presence? | Strict ordering across devices and servers costs reorder buffers; presence costs a write per status change for 200 million people. Both are easy to promise and expensive to keep. | Not for the first version. Messages are stamped on arrival and shown in that order. Presence and receipts are optional extensions if time allows. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
Messages reach online users in under 500 ms | A push over an open connection, not polling. The path from the sender's socket to the recipient's socket is one durable store of the message plus its inbox rows, followed by in-memory hops; nothing on it waits on a batch or a queue that drains on a timer. |
Every message is delivered | A durable copy exists before anything in-memory touches the message, and the recipient's ack is the signal that deletes it. Any hop that may drop a message (the channel between servers, a dead socket) must be backed by that copy. |
Billions of users and high throughput | Hundreds of stateful chat servers, so a message's sender and recipient are usually on different hosts and the design needs a way to route between them. The store is partitioned: message rows by chat and inbox rows by recipient device, so the write load spreads across nodes, and a group send touches one partition per recipient. |
Messages stay on the servers only as long as needed | The inbox row is deleted on ack, and a time-to-live clears anything older than 30 days. There is no permanent archive; history lives on the client. |
Survives the failure of any one component | A dead chat server drops 1 to 2 million sockets at once; clients must reconnect through the balancer to any other server and drain their inbox. The pub/sub layer and the store are replicated, and losing a pub/sub node loses in-flight publishes, not messages, because the inbox still holds them. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Chat servers to hold everyone connected | 200 million connections ÷ 1 to 2 million per host = 100 to 200 hosts | About 100 to 200 chat servers | The size of the stateful fleet, and why routing between servers is unavoidable: with that many hosts, a sender and a recipient are rarely on the same one. |
Memory for open connections | 10 KB per connection × 200 million = 2 TB across the fleet; at 1 million per host that is the 10 GB a host already quoted, at 2 million it is 20 GB | About 2 TB in total, 10 to 20 GB per host | The RAM a chat server needs for sockets, before any buffers for in-flight messages. |
Message and write rate | 200 million active × 20 messages = 4 billion a day; 4 billion ÷ 86,400 seconds ≈ 46,000 a second, rounded to about 40,000; each message also writes one inbox row per recipient, and group messages write more, so about 100,000 writes a second | About 40,000 messages a second, about 100,000 row writes a second | The write load on the store: message rows partitioned by chat and inbox rows partitioned by recipient device, spread over many nodes rather than kept on one. |
Heartbeats a second across the fleet | 200 million connections ÷ 10 seconds = 20 million a second; at a 30 second interval, 200 million ÷ 30 ≈ 6.7 million a second | 20 million heartbeats a second at 10 seconds, about 6.7 million at 30 seconds | The load a heartbeat places on the chat servers. It is spread across 100 to 200 hosts and stays in memory; nothing per ping may reach a database. |
Time to notice a dead phone | App heartbeat: 10 second interval + 5 second wait for the reply ≈ 15 seconds. Linux TCP keepalive defaults: 7,200 seconds idle before the first probe, then 9 probes 75 seconds apart = 7,200 + 675 = 7,875 seconds, about 2 hours 11 minutes | About 15 seconds with an app heartbeat, over 2 hours with default TCP keepalive | How long a phone in a tunnel looks connected, and therefore how long its messages sit unacked on a server with no live socket to deliver them on. |
Cost of one message to a full group | 100 members − 1 sender = 99 inbox rows and 99 publishes with a channel per user; with three devices each, 99 × 3 = 297 inbox rows. A channel per chat costs 1 publish whatever the size, but each connected member then carries a subscription per chat, so where to switch is a judgment call rather than a break-even; a quarter of the cap, 100 ÷ 4 = 25 members, is a reasonable place to put it | 99 publishes and 99 to 297 rows per group message; a per-chat channel above about 25 members | The fan-out of a group message, and the threshold at which a channel keyed by chat beats one keyed by user. |
Last seen written on each heartbeat | 20 million heartbeats a second ÷ 100,000 message writes a second = 200 | About 200 times the write load of the messages themselves | Why presence is written once at disconnect and not on each heartbeat: done naively it dwarfs the real workload. |
Core entities
| Entity | What it holds |
|---|---|
User | One row per account. Profiles are out of scope, so this is the id the rest of the model points at. |
Chat | A conversation between two people or a group of up to 100. One-to-one chats are just groups of two. |
Chat participant | Who is in which chat. Read on each send to find the recipients, so it is keyed by chat. |
Message | What was said, by whom, in which chat. Kept until delivered to each recipient, 30 days at most. |
Inbox | The undelivered queue for one device. A row is written before the message is pushed and deleted when the device acks. |
Client | One device of one user, at most three per account. Delivery, acks and the inbox are per client. |
The API
| Endpoint | Notes |
|---|---|
| Refuses more than 100 participants. Writes the chat row and the participant rows; a one-to-one chat is a two-person group with no name. |
| The server assigns a time-ordered id, writes the message row and one inbox row per recipient device, then publishes. The reply goes back after the writes, so a |
| Returns a short-lived URL that lets the client put one object straight into blob storage. The chat server signs it and doesn't handle the bytes; the message then carries the object's link in |
| Pushed over the recipient's socket. The ack carries the message id; the server deletes that device's inbox row on receipt. No ack means the row stays and is re-sent on the next connect. |
| Updates the participant rows. A removed member stops receiving from the next message on; whatever is already in their inbox still drains, which is acceptable here. |
The high-level design
Step 1 of 4
One chat server holding both sockets
Start with the smallest thing that works. Alice and Bob each open a WebSocket through an L4 load balancer to a chat server, which keeps a map from user id to socket. When Alice calls sendMessage, the server looks up Bob's socket and writes the message to it. The balancer works at layer four because a WebSocket is a single long-lived TCP connection: once it's placed on a server it stays there, and nothing per-request needs inspecting. A WebSocket rather than polling or server-sent events: the phone both sends and receives, and server-sent events carry only the server's side, while polling often enough to meet the 500 ms target would have each of the 200 million connected phones asking several times a second, almost always for nothing. This answers the happy path and nothing else: if Bob is not connected, the message has nowhere to go.
What breaks first
The deep dives
Routing between chat servers. Alice's socket is on one chat server and Bob's is on another. How does Alice's message find Bob's server, what does that choice cost compared with the alternative, and what happens when the hop between servers loses it?
Groups and several devices. Bob is signed in on his phone and his laptop and belongs to a group of 100. When someone posts, who gets what, how many writes and publishes does it cost, and should the channel between servers be keyed by the user or by the chat?
Dead connections and lost messages. Bob's phone drops into a tunnel but his socket still looks open to the server. How do you notice, how fast, what does that checking cost across 200 million connections, and how does Bob's client find out what it missed?
Concepts this guide uses
Sources
- Redis documentation: Pub/Sub
- RFC 6455: The WebSocket Protocol (RFC Editor)
- Alex Xu, System Design Interview, chapter 13: Design a chat system (notes by Daniel Farlow)
- WhatsApp blog: ONE MILLION!
- High Scalability: The WhatsApp architecture Facebook bought
- Wikipedia: C10k problem
- Amazon S3 documentation: Uploading objects with presigned URLs
- Amazon DynamoDB documentation: Using time to live (TTL)
- MyBlueLinux: Linux TCP keepalive
- Confluent: How to choose the number of topics/partitions in a Kafka cluster
- Wikipedia: Consistent hashing