Design WhatsApp (real-time chat)
Deliver billions of messages a day in real time, keep them in order, show sent/delivered/read ticks and reach phones that are asleep. The core ideas: persistent connections, finding which server a user is connected to, a write-optimised message store and push notifications.
Builds on these concepts
In one sentence
Each phone keeps a persistent connection to a chat server; a session map says which server each user is on; messages are stored until delivered and offline users are woken with push notifications.
A reference design: it covers what an app like this needs and follows how these companies have described their systems publicly, simplified to be learnable.
Requirements
Functional
- 1:1 and group chats
- Sent / delivered / read receipts, online and typing
- Photos, video and voice notes
- Works across devices and when offline
Non-functional
- Real-time: sub-second delivery
- Messages never lost, order kept per chat
- Huge, write-heavy volume
- Privacy: end-to-end encryption
Back-of-the-envelope estimates
| Metric | Estimate |
|---|---|
| Messages | 100B / day (public order of magnitude) ≈ 1.2M messages/s |
| Concurrent connections | Hundreds of millions of phones online at once |
| Message size | ~100 B of text + metadata; media goes to object storage |
| Connections per server | With an efficient runtime, ~1M per box (WhatsApp famously did ~2M on Erlang) |
Protocol and data model
Chat isn't request/response, so the API is a set of events over a WebSocket (or a custom protocol over TCP) instead of REST endpoints.
→ send { chatId, clientMsgId, body | mediaRef }
← ack { clientMsgId, msgId, ts } // ✓ sent
← message { chatId, msgId, from, body, ts }
→ delivered { msgId } → read { msgId } // ✓✓ and blue ticks
messages(chat_id, msg_id, sender_id, body, created_at) PRIMARY KEY (chat_id, msg_id)
sessions: user_id → chat_server, last_seen (in memory / Redis)Architecture
Step by step · Send a message
- 01Ana types "Dinner at 9?" and hits send
- 02Receives it over Ana's WebSocket
- 03Appended to the chat's partition → ✓ (sent)
- 04Bob is online, connected to server B
- 05Pushes the message down Bob's socket
- 06Bob's phone acks → Ana sees ✓✓ (delivered)
At this scale · 1M users
A message store built for writes
- ▸Messages in Cassandra, partitioned by chat and sorted by time
- ▸A push service handles offline delivery and retries
- ▸Photos and voice notes in object storage, served by CDN
Tap any box to see what it does.
Deep dive: delivering across servers
Connections are stateful: Ana's socket lives on server A, Bob's on server B. Server A needs to know where Bob is. A session store (user → server) answers that, and messages travel A → B through a pub/sub channel or direct RPC. If Bob has no session, the message stays in his inbox and a push wakes his phone.
- Ordering: a per-chat sequence (or timestamp + id) keeps messages in order inside a chat; global order isn't needed.
- Groups: the server fans a message out to each member (small groups), or members fetch from the group's log (large groups).
- Storage: a wide-column store partitioned by
chat_idturns "load this chat" into one sorted partition read. WhatsApp itself deletes messages once delivered and keeps history on devices. - End-to-end encryption (Signal protocol): keys live on the phones, so the server stores and forwards only ciphertext.
How it changes with scale
Step by step · Bob is offline
- 01Ana sends a message
- 02Bob has no open connection
- 03Stored as undelivered
- 04Apple/Google wake up the phone
- 05App reconnects and syncs pending messages
At this scale · 100 users
One server keeps every connection
- ▸Phones keep a WebSocket open to one chat server
- ▸Messages stored in PostgreSQL
- ▸Offline users get a push notification (APNs / FCM)
Tap any box to see what it does.
| Users | What you run | Why it changes |
|---|---|---|
| 100 | One WebSocket server + PostgreSQL + APNs/FCM | Everyone is on the same server: delivery is trivial |
| 10K | Several chat servers, LB, Redis sessions + pub/sub | Users end up on different servers |
| 1M | Cassandra message store, push service, media on S3 + CDN | Write volume and offline delivery need their own components |
| 100M+ | Millions of connections per server, multi-region, E2E encryption | Connection count, cost per user and privacy at global scale |
Key takeaways
- 01Real-time chat needs persistent connections (WebSockets), not polling.
- 02Connections are stateful: keep a map of which server each user is on.
- 03Store messages in a write-optimised store partitioned by chat; deliver offline via push.
- 04Receipts and retries rely on idempotent message ids.
Check your understanding
1 of 3
In a chat app like WhatsApp, how does each phone stay reachable in real time?