WhatsApp serves more than two billion people, and a message to a friend across the world usually arrives before you lock your phone. “Design WhatsApp” (or Messenger, or Slack) is one of the most frequently reported system design questions at companies like Meta, Amazon and Microsoft. Unlike the URL shortener, the server can't just wait to be asked: it has to push messages to the right phone instantly, and hold them safely when the phone is off.
Step 1 · Requirements
- Functional: one-to-one chat; group chat; sent / delivered / read receipts; online status and last seen; offline delivery; photos and videos.
- Non-functional: real-time (well under a second); never lose a message; messages in order within a chat; works on flaky mobile networks; private.
- Out of scope (say it): voice and video calls, stories/status, payments.
Step 2 · Estimates
- Users: assume 500 million daily active users sending 40 messages a day = 20 billion messages/day.
- Throughput: 20B ÷ 100,000 s ≈ 200,000 messages per second, ~400,000 at peak.
- Storage: ~200 bytes per text message with metadata → 4 TB per day (media is far bigger and goes to object storage).
- Connections: if 100 million phones are connected at once and one chat server holds ~200,000 connections, that's ~500 chat servers.
Step 3 · API: how phones talk to servers
First choose the transport. The server must push, so plain request/response isn't enough:
| Option | How it works | Verdict |
|---|---|---|
| Short polling | Client asks “anything new?” every few seconds | Simple, but wasteful and laggy |
| Long polling | Server holds the request open until there's news, then client re-asks | Near real-time; reconnect overhead; good fallback |
| WebSocket | One connection kept open; both sides send any time | Real-time, efficient. The standard for chat |
Over the WebSocket, a few event types do everything:
→ send { clientMsgId, chatId, text } phone → server
← ack { clientMsgId, messageId } server → sender (✓ sent)
← message { messageId, chatId, from, text } server → recipient
→ delivered{ messageId } / → read { chatId, upToMessageId }
← receipt { messageId, status } server → sender (✓✓ / blue)
→ sync { lastMessageId } on reconnect: "what did I miss?"Media (photos, videos) is uploaded separately over normal HTTPS to object storage; the chat message carries only a link and a key.
Step 4 · Design
The life of a message
Follow one message end to end, then try it with Arjun offline:
🎮 Follow one message: Riya → Arjun
Riya's phone
Nothing sent yet
Arjun's phone 🟢 online
No new messages
Try it twice: once with Arjun online, once offline (send, then bring him online). Each tick is just an acknowledgement travelling back to Riya.
- Send: Riya's app sends the message with a
clientMsgIdover her WebSocket. - Store first: chat server 1 assigns a
messageId, writes it to the message store, and only then ACKs Riya (✓). Storing before acknowledging is what makes “never lose a message” true. - Route: it looks up Arjun in the session registry (user → chat server, kept in Redis and updated on connect/disconnect).
- Forward: online → send it to chat server 2, which pushes it down Arjun's socket. Offline → leave it undelivered and trigger a push notification.
- Receipts: Arjun's phone ACKs (✓✓ delivered); opening the chat sends a read receipt (blue ✓✓).
Message IDs and ordering
Messages in a chat must appear in order on every phone. Device clocks can be wrong, so the server assigns IDs that grow with time. A popular scheme is a Snowflake ID: a 64-bit number made of a timestamp, a machine number and a counter, so IDs are unique without coordination and sort by time:
import threading
import time
EPOCH_MS = 1767225600000 # 2026-01-01 UTC: our own "time zero"
class Snowflake:
"""64-bit, time-sortable IDs: 41 bits time | 10 bits machine | 12 bits sequence."""
def __init__(self, machine_id: int):
assert 0 <= machine_id < 1024
self.machine_id = machine_id
self.last_ms = -1
self.seq = 0
self.lock = threading.Lock()
def next_id(self) -> int:
with self.lock:
now = int(time.time() * 1000)
if now == self.last_ms:
self.seq = (self.seq + 1) & 4095 # up to 4096 IDs per ms
if self.seq == 0: # used them all: wait for next ms
while now <= self.last_ms:
now = int(time.time() * 1000)
else:
self.seq = 0
self.last_ms = now
return ((now - EPOCH_MS) << 22) | (self.machine_id << 12) | self.seq
gen = Snowflake(machine_id=7)
a, b = gen.next_id(), gen.next_id()
print(a < b) # True: later IDs are bigger, so they sort by time
print((a >> 12) & 1023) # 7: the machine that made it
print((a >> 22) + EPOCH_MS) # the millisecond it was createdIDs from different machines in the same millisecond aren't perfectly ordered, and real systems also guard against server clocks moving backwards. Within one chat that's usually solved by routing a chat's writes through one place, or with a per-chat sequence number.
Storing messages
messages -- wide-column store (Cassandra / HBase) chat_id PARTITION KEY -- all of one chat lives together message_id CLUSTERING KEY -- Snowflake, newest first sender_id, type, text, media_url, created_at
“Load the latest 50 messages of this chat” becomes one fast read from one partition. A design choice to mention: WhatsApp says it keeps messages on its servers only until they're delivered (history lives on the phones), while apps like Slack keep full history on the server for search and new devices. That choice changes storage size enormously.
Group chats
For a group, the server looks up the member list and forwards the message to each member's chat server (or queues it for offline members). This fan-out is cheap for typical groups. Because group size is capped, the fan-out per message stays bounded. Very large broadcast channels instead let members pull new posts.
Online status and last seen
Each connected app sends a small heartbeat every so often. The presence service marks a user online while heartbeats arrive and records “last seen” when they stop. Only friends who have the chat open need live updates, so presence changes are sent to them, not to everyone in your contacts.
Step 5 · Scale and reliability
- Chat servers: stateless apart from their open sockets. Add more as connections grow. If one dies, its phones reconnect to another and sync from their last message ID.
- Message store: partitioned by
chat_idacross many nodes, replicated (e.g. 3 copies) so a node failure loses nothing. - Between servers: forward directly, or through a message queue (Kafka) to absorb spikes like midnight on New Year's Eve.
- Media: object storage + CDN; the phone uploads once, recipients download from the nearest edge.
- Privacy: WhatsApp uses end-to-end encryption based on the Signal protocol: phones encrypt messages, and the servers route data they can't read. Mention it, but keep the focus on delivery.
- Multiple regions: users connect to the nearest data centre; cross-region messages are forwarded between regions.
🧠 PATH memory card
Remember the chat system design for good
Picture it · Anchor it · Trace it · Hit-recall it. Four quick steps that turn “I read it once” into “I can explain it in an exam”.
- P
Picture it
Your brain remembers scenes far better than definitions.
← swipe to see the whole picture →
One message, four moves. Pipe: each phone keeps one open connection. Store: the server saves the message before saying “got it”. Forward: find your friend's server and push it, or park it and ring the doorbell (push notification). Tick: receipts flow back as ✓, ✓✓ and blue ✓✓.
- A
Anchor it
A silly sentence you can't forget, where every word maps to the concept.
“Pipe, Store, Forward, Tick”
- Pipe→Long-lived WebSocket from each phone to a chat server
- Store→Assign a Snowflake ID, save to the message store, then ACK (✓)
- Forward→Session registry → recipient's server; offline → push notification
- Tick→Delivered ACK (✓✓) and read receipt (blue ✓✓)
- Same clientMsgId→Idempotent retries: no duplicate messages
- T
Trace it
Run it by hand on a tiny example once. Doing beats reading.
Riya → Arjun, Arjun offline. Pipe: message arrives on server 1. Store: ID 4519…, saved, Riya sees ✓. Forward: registry has no entry for Arjun → push notification. 20 minutes later Arjun reconnects to server 7 and sendssync{ lastMessageId }. Server 7 reads his chats from the store, delivers the message. Tick: ✓✓ for Riya; he opens it → blue. - H
Hit-recall it
Test yourself now, then again on the day each card comes back.
Card 1 / 8 · new0 mastered · 0 practisedSay the answer out loud first, then flip and be honest.
Follow-up questions interviewers ask
- “Same user on phone and laptop?” The registry maps a user to several connections; deliver to all of them, and sync read state across devices.
- “A chat server crashes mid-delivery?” Messages are already stored; phones reconnect elsewhere and sync, so nothing is lost. Dedup handles any resends.
- “How do you show typing…?” Send small ephemeral events over the socket. Don't store them.
- “Search old messages?” With server-side history, index messages in a search engine (e.g. Elasticsearch). With end-to-end encryption, search runs on the device.
Where to go next
Chat delivers what people send. Search has to guess what they'll type, with a suggestion after every keystroke. Next: design search autocomplete.
Key takeaways
- ✓Each phone keeps a WebSocket to a chat server so the server can push messages instantly.
- ✓Store the message before ACKing (✓); the recipient's ACK gives ✓✓; a read receipt turns them blue.
- ✓A session registry maps users to chat servers; offline users get a push notification and sync on reconnect.
- ✓Partition messages by chat_id and sort by a time-ordered (Snowflake) ID.
- ✓Idempotent retries (same clientMsgId) prevent duplicates on flaky networks.
Frequently asked questions
How does WhatsApp deliver messages in real time?
Each phone keeps a long-lived connection (like a WebSocket) open to a chat server. When you send a message, your chat server stores it, looks up which server your friend is connected to, and forwards it there; that server pushes it down your friend's open connection immediately. No polling is needed, so messages arrive in milliseconds.
What do the WhatsApp ticks mean in system design terms?
They are acknowledgements. One grey tick means the server has received and stored the message (sent). Two grey ticks mean the recipient's device received it and sent back an acknowledgement (delivered). Two blue ticks mean the recipient opened the chat and their app sent a read receipt. Each receipt is a small message travelling back through the chat servers.
What happens to a WhatsApp message when the receiver is offline?
The server keeps the message as undelivered and sends a push notification through Apple's or Google's push service to wake the phone. When the phone reconnects, it asks for everything it missed since its last received message, the server delivers them in order, and the sender sees two ticks. WhatsApp says undelivered messages are kept on its servers for a limited time.
Why use WebSockets instead of HTTP polling for chat?
With polling, every phone asks the server “anything new?” every few seconds, which wastes battery, bandwidth and server capacity, and still adds delay. A WebSocket is one connection kept open in both directions, so the server can push a message the instant it arrives. Long polling is a middle ground used as a fallback.
Which database is used for chat messages?
Chat is write-heavy and read by conversation in time order, so wide-column NoSQL stores like Cassandra or HBase are popular: partition by chat ID and sort by a time-ordered message ID, which makes “latest 50 messages in this chat” a single fast read. User profiles and group membership can live in a relational database.
🧠 Quick check: chat system design
1. Why does a chat app keep a WebSocket open instead of polling every 5 seconds?
2. Riya sees two grey ticks. What has happened?
3. Chat server 1 holds Riya's connection. How does it know where to send Arjun's message?
4. Best partition key for the messages table?
Checking your access…