Skip to content

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

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

AskWhy it mattersA 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.

The requirements menu

RequirementScopeWhy

Create group chats of up to 100 people

Core

The group cap is a design input: it bounds how many inbox rows and publishes a single send can produce.

Send and receive messages

Core

The product. Everything else hangs off the path from one socket to another.

Receive messages sent while offline, kept for up to 30 days

Core

This is what turns the design from a relay into a store: something durable has to hold the message until the recipient shows up, and let go of it afterward.

Send and receive photos and other media

Core

A photo is far larger than a text message, so media needs a separate path: the servers holding sockets should not carry file contents.

Delivered and read receipts

Optional

Worth taking if time allows: the ack the client already sends is the delivered receipt, so the extra work is a small message back to the sender. Leave it if the interviewer wants depth on routing instead.

Online presence and last seen

Optional

Tempting because the heartbeat already tells you who is connected. Take it if you have a plan for the write load; writing status on each heartbeat for 200 million people is a known trap.

Typing indicators

Optional

Cheap to add on top of the same channel and safe to lose, so they are a good example of traffic that should skip the inbox. Mention it, don't build it.

Push notifications when the app is closed

Optional

Real products need them, and they are a separate service that reads from the same inbox write. Name the hook and move on unless asked.

Strict ordering of messages within a chat

Optional

Leave it unless asked. Server-stamped order is good enough for chat and avoids reorder buffers on the client and coordination between servers.

End-to-end encryption

Optional

Central to the real product, and a large topic of its own: key exchange, device identities and encrypting for each device. For this round, note that the server would store ciphertext and keep the time on delivery.

Voice and video calls

Out of scope

A different system: media streams, codecs and relay servers. Name it as out of scope and keep the round on messaging.

Business accounts

Out of scope

On the interviewer's out-of-scope list. Say so in one sentence and spend the time on delivery.

Sign-up and profiles

Out of scope

Ordinary CRUD behind a stateless API. Assume a user id and an auth token exist when the socket opens.

Spam and scraping

Out of scope

Worth a sentence on rate limits per sender, then park it. Abuse systems are their own round.

Non-functional requirements

RequirementWhat 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

QuantityWorkingResultWhat 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

EntityWhat 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

EndpointNotes

WebSocket createChat { participants, name } → { chat_id }

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.

WebSocket sendMessage { chat_id, message, attachments } → { status, message_id }

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 status of stored means the message is safe even if nobody is online. We'd put a client-generated id inside message so a retry after a dropped socket is recognized instead of stored twice.

WebSocket getAttachmentTarget → { presigned_upload_url }

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 attachments.

Server to client newMessage { chat_id, user_id, message }, the client answers with an ack

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.

WebSocket modifyChatParticipants { chat_id, user_id, add or remove }

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

WebSocketpushAliceL4 load balancerChat server ABob

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.

WebSocketuploadwritepublishchannelnewMessageackdeleteAliceL4 load balancerBlob storageChat server AMessages, inboxRedis pub/subChat server BBob
The whole design

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?

A concept that helps here: Routing messages between stateful serversA concept that helps here: Delivery semantics: at most, at least, exactly onceA concept that helps here: Consistent hashing

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?

A concept that helps here: Fan-out on write and on readA concept that helps here: Sharding and partition keysA concept that helps here: Routing messages between stateful servers

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?

A concept that helps here: Heartbeats and presenceA concept that helps here: Clocks and orderingA concept that helps here: WebSockets, server-sent events and polling

Concepts this guide uses

Sources

  1. Redis documentation: Pub/Sub
  2. RFC 6455: The WebSocket Protocol (RFC Editor)
  3. Alex Xu, System Design Interview, chapter 13: Design a chat system (notes by Daniel Farlow)
  4. WhatsApp blog: ONE MILLION!
  5. High Scalability: The WhatsApp architecture Facebook bought
  6. Wikipedia: C10k problem
  7. Amazon S3 documentation: Uploading objects with presigned URLs
  8. Amazon DynamoDB documentation: Using time to live (TTL)
  9. MyBlueLinux: Linux TCP keepalive
  10. Confluent: How to choose the number of topics/partitions in a Kafka cluster
  11. Wikipedia: Consistent hashing
Sign in to report a mistake