Messaging and social · Design 15

Design WhatsApp

Design WhatsApp the way you would in a 45-minute interview: requirements, estimates, API, data model, architecture, and deep dives on delivery, ordering, offline users and groups.

IntermediateInterview priority: High55 min
On this page
  1. How to use this page
  2. 1. Clarify the requirements
  3. 2. Back-of-the-envelope estimates
  4. 3. API
  5. 4. Data model
  6. 5. High-level design
  7. 6. Deep dives
  8. 7. Bottlenecks, failures and monitoring
  9. 8. Follow-up questions
  10. Quiz

How to use this page

This page follows the order of a real system design interview. At each step there is a Your turn box: write your own answer first (it stays in your browser), then open ours and compare. Reading a finished design feels easy; producing one under time pressure is the skill you are practising.

PhaseTime in a 45-min interviewWhat you produce
1. Clarify requirements5 minFunctional list, non-functional targets, what's out of scope
2. Estimate5 minMessages/s, storage/day, open connections, number of servers
3. API5 minThe handful of calls and events the client needs
4. Data model5 minThe main tables, their keys and which database
5. High-level design10 minBoxes and arrows, and one or two flows traced through them
6. Deep dives10–15 minThe 2–3 hardest problems, options compared, a decision, and how it fails
7. Wrap up2–3 minBottlenecks, failures, what you would monitor

Users send text and photo messages 1:1 and in groups of up to 1,000 people. Show sent, delivered and read ticks, and online / last seen. Messages must reach people who are offline later. Skip voice and video calls, status stories and payments.

1. Clarify the requirements

Your turn: scope it

The interviewer says only: "Design WhatsApp." Before reading on, write down what you would ask and agree on: 4–6 functional requirements, 4–6 non-functional requirements with a target, and 2–3 things you would put out of scope.

Compare with our answer

The questions that matter most: 1:1 only or groups too? How big can a group be? Do we keep chat history on the server? Do we need end-to-end encryption? Media? Multiple devices per user? Then write the agreed list on the board, as below.

Functional: what it must do

  • 1:1 messages: Text and media, delivered in order
  • Group chats: Up to 1,000 members
  • Delivery receipts: Sent, delivered, read
  • Offline delivery: Messages wait until the phone comes back
  • Presence: Online / last seen
  • Media: Photos, videos and files (WhatsApp allows up to 2 GB)

Out of scope

Voice and video callsStatus storiesPayments

Non-functional: how well it must work

QualityTargetWhy
Latency< 200 ms when both are onlineChat must feel instant
Availability99.99%People depend on it like SMS
DurabilityNo message lost once the server acks itLost messages destroy trust
OrderingIn order within each chatA reply before its question is confusing
PrivacyEnd-to-end encryptedThe server must not be able to read messages
Scale2 B users, 100 B messages/dayOne of the largest systems on earth

2. Back-of-the-envelope estimates

Your turn: the numbers

Assume 2 billion daily users, 50 messages each per day, 100 bytes per text, 5% of messages carrying a 300 KB photo, and 20% of users online at any moment. Work out messages per second, text and media stored per day, and how many open connections the servers must hold.

Compare with our answer
  • Messages per day: 2 B × 50 = 100 B. Per second: 100 B ÷ ~86,400 ≈ 1.2 M/s on average (plan for 2–3× at peak).
  • Text: 100 B × 100 bytes = 10 TB/day. Small.
  • Media: 100 B × 5% × 300 KB = 1.5 PB/day. Enormous: this must go to object storage and a CDN, never through chat servers.
  • Open connections: 2 B × 20% = 400 M at once. Plan for about 250 K per server and 50% headroom: 400 M ÷ 250 K × 1.5 ≈ 2,400 chat servers.
  • Don't forget receipts: every message also produces "delivered" and "read" events (per member in a group), so the event rate is at least 3× the message rate.
  • Media bandwidth: 1.5 PB ÷ 86,400 s ≈ 17 GB/s (about 140 Gbps) of uploads, and more for downloads because each photo goes to every recipient.

A strong answer mentions: messages/s with a peak factor · storage split into tiny text vs huge media · open connections as the number that sizes the fleet · headroom · receipt amplification.

Assumptions: daily active users 2 B; messages per user per day 50; average text message 100 B; share of messages with media (%) 5; average media size 300 KB; users online at once (%) 20.

NumberEstimateWhy it matters
Messages per second1.16 M / s100 B a day ÷ ~100 K seconds ≈ 1 M/s on average; peaks 2–3× higher.
Text per day10 TBSmall: messages are deleted once delivered.
Media per day1.5 PB5 B photos × 300 KB = 1.5 PB a day: media dominates storage and bandwidth.
Open connections400 MEach online phone holds one open connection.
Chat servers (≈250 K connections each, plus 50% headroom)2.4 KWhatsApp famously pushed ~2 M connections per Erlang server; plan conservatively and keep spare capacity so losing a rack doesn’t overload the rest.
Media upload bandwidth17.4 GB/s≈17 GB/s (about 140 Gbps) of uploads on average; downloads are larger, because each photo goes to every recipient, so a CDN serves them.
Events per second incl. receipts3.47 M / sEach message also produces a “delivered” and a “read” receipt (per member in groups), so the event rate is at least 3× the message rate.
  • Text is tiny; media is huge, so media goes to object storage and a CDN, never through chat servers.
  • Connections, not messages per second, are what size the chat server fleet.

The key insight from the numbers: connections, not messages per second, size the chat server fleet. A million messages a second is a lot, but each one is tiny; holding 400 million idle-but-open connections is the real cost. WhatsApp famously pushed about 2 M connections per Erlang server, but in an interview plan conservatively and show the headroom.

3. API

Your turn: the API

Which calls does the phone need, and which of them must be real-time? Write the 4–6 calls or events with their main fields.

Compare with our answer

A strong answer mentions: one long-lived connection for send/receive · a client-generated message ID on every send · an ack carrying the server's sequence number · receipts as events · a separate HTTP upload for media · a sync call that resumes from the last sequence number.

Chat is two-way and must be instant, so the core of the API is a persistent WebSocket over which the client sends messages and the server pushes them. Plain HTTP handles everything that isn't real-time: uploading media, syncing history after being offline, managing groups.

CallRequestResponseNotes
WS connect /v1/chatauth token, deviceIdconnection openOne long-lived WebSocket per device
WS send{ clientMsgId, chatId, ciphertext, type }ack { msgId, seq, ts }clientMsgId lets the server drop duplicates when the phone retries
POST /v1/mediaencrypted file (resumable upload){ mediaId, url }Upload first, then send a small message containing the URL
GET /v1/sync?after={cursor}–{ messages[], receipts[], cursor }Catch up after being offline
POST /v1/groups{ name, members[] }{ groupId }Group admin APIs: add/remove members

Real-time events

DirectionEventPayload
server → phonemessage{ msgId, chatId, from, seq, ciphertext }
phone → serverreceipt{ msgId, status: delivered | read }
server → phonereceipt{ msgId, status } (turns ticks blue)
both wayspresence{ userId, online | lastSeen }
both waystyping{ chatId, userId }

4. Data model

Your turn: the tables

WhatsApp keeps a message on the server only until every recipient device has it. What tables do you need, what is each one's key, and which database would you pick for each?

Compare with our answer

A strong answer mentions: a per-device inbox keyed for in-order reads · how undelivered messages expire · where the per-chat sequence comes from · sessions per device in memory · users, groups and devices in a relational store · media in object storage.

inbox (until delivered) Wide-column store (HBase / Cassandra with time buckets)

  • (device_id, day) (partition key)
  • inbox_seq (clustering key)
  • chat_id, chat_seq, msg_id, sender_id
  • ciphertext (or a pointer to a shared group payload)
  • expires_at (e.g. 30 days if never delivered)

Store and forward: one row per recipient device, read in order on sync and removed after the device acks. Partitioning by device and day keeps partitions small, and whole day-buckets expire by TTL instead of piling up deletes (tombstones).

chat sequence Owned by the chat’s home server (consistent hashing on chat_id), persisted with the message

  • chat_id → last_seq

One owner per chat hands out chat_seq numbers in order, and assigns a number only when the message is durably written, so there are no holes.

sessions In-memory store (Redis), one entry per device

  • user_id → { device_id: chat_server_id }
  • last_heartbeat per device (TTL)

Hot lookup on every message: which chat server is each of this user’s devices connected to?

groups and users Relational (MySQL) + cache

  • groups(group_id, name, created_by)
  • members(group_id, user_id, role)
  • devices(user_id, device_id, identity_key)

Small, relational, read often: who is in group G, and which devices do they have?

media Object storage + CDN

  • media/{mediaId} (encrypted)
  • expires after the download window

Petabytes of encrypted blobs; downloads served from the edge.

The design decision that shapes everything here is store-and-forward: the server is a mailbox, not an archive. So the main table is an inbox per recipient device, not a chat history:

ColumnExampleWhy
(device_id, day) partition keyd_77a1, 2026-11-18One device's undelivered messages sit together; a day bucket keeps partitions small
inbox_seq clustering key5531Sync reads "everything after 5530" as one range read
chat_id, chat_seqc_81f2, 10452The phone uses chat_seq to order messages inside each chat
msg_idclient UUIDDe-duplicates retries
ciphertextbytesEnd-to-end encrypted: the server stores bytes it cannot read

Two details interviewers probe:

  • Deletes are expensive in Cassandra. Each delete writes a tombstone that later reads must skip, so a queue-like table full of deletes gets slow. Bucketing by day and expiring whole buckets with a TTL (plus time-window compaction) avoids millions of individual deletes. HBase, which Facebook used for Messages, handles this pattern well too.
  • Group messages are stored once. With the Signal "sender key" scheme the group ciphertext is the same for every member, so store the payload once and put a small pointer in each member device's inbox. The payload is removed when every device has acked it (a reference count) or when it expires.

Sessions (which device is on which chat server) change constantly and are read on every message, so they live in memory in Redis, one entry per device. Users, devices and group membership are relational, read often and changed rarely: MySQL or Postgres with a cache in front.

5. High-level design

Your turn: draw it

Sketch the boxes: how does Alice's phone connect, where is a message stored, how does it find Bob's connection, and what happens if Bob's phone is off? Then step through ours.

Compare with our answer

Compare your sketch with the figure below. The pieces people most often forget: the session map (how one server finds another user's connection), the push service for offline phones, and keeping media off the chat path.

The architecture, then four flows traced through it

  1. The architecture. Phones keep a WebSocket open to a chat server through a layer-4 load balancer. A session map records which server each user is on. Undelivered messages wait in each device’s inbox, groups live in a group service, photos go to media storage, and a push service wakes phones that are offline.
  2. Both online: Alice’s phone sends the message over its open connection. The server saves it and acks: one tick. It looks up Bob in the session map, forwards to his chat server, which pushes it down Bob’s connection. Bob’s phone acks, and Alice sees two ticks.
  3. If Bob is offline, the message waits in his pending inbox and a push notification wakes his phone. When it reconnects, it syncs everything it missed, in order, then the pending copies are deleted.
  4. In a group, the server asks the group service who the members are, then fans the message out: one copy per member, each routed through the session map like a 1:1 message.
  5. Photos never travel through chat servers. Alice’s phone encrypts and uploads the photo to media storage, then sends a tiny message holding its link and key. Bob’s phone downloads it from the CDN.
ComponentWhat it does
L4 load balancerSpreads millions of long-lived connections across chat servers
Chat serversHold WebSocket connections; route messages; stateless apart from their connections
Session mapuser → chat server, in Redis; updated on connect and disconnect
Message storeMessages by chat, ordered by sequence number; pending inbox per user
Group serviceMembers per group, heavily cached
Media store + CDNEncrypted photos and videos
Push serviceApple / Google push to wake offline phones
PresenceOnline / last-seen, driven by connection heartbeats

Trace the main flow out loud in the interview, one hop at a time:

  1. Alice's phone sends {msgId, chatId, ciphertext} over its WebSocket. The L4 load balancer has already pinned this connection to chat server 1.
  2. Chat server 1 assigns the next sequence number for the chat, writes the message to the message store, and only then sends Alice an ack (one tick).
  3. It looks up Bob in the session map: he is on chat server 2.
  4. It forwards the message to chat server 2, which pushes it down Bob's WebSocket.
  5. Bob's phone acks delivery; the receipt flows back and Alice sees two ticks.

6. Deep dives

Your turn: the hard parts

Before reading the deep dives, write two or three sentences on each: how does a message find a recipient on another server? How do you keep messages in order? How do you make sure a message is neither lost nor shown twice?

Compare with our answer

A strong answer mentions:

  • A session map from user/device to chat server, refreshed by heartbeats, and what happens when it is stale.
  • A per-chat sequence number assigned by one owner when the message is stored, not phone timestamps.
  • Ack only after a durable write; retries on both legs; de-duplication by client message ID.
  • The offline path: inbox, push notification, sync from the last sequence number.

Pick two or three of these, depending on what the interviewer pushes on. Each one follows the same shape: the problem, the options, the choice and its cost.

Real-time delivery: how does a message reach a phone instantly?

OptionHowLatencyCost at 400 M users
Short pollingPhone asks "anything new?" every few secondsUp to the poll intervalBillions of empty requests per minute
Long pollingRequest stays open until there is news or a timeoutLowA new HTTP request after every message; awkward for sending
Server-Sent EventsOne-way stream server → phoneLowSending still needs separate requests
WebSocketOne persistent two-way connectionLowestOne open connection per online user; that's what we size for

Choice: WebSocket. Each chat server holds hundreds of thousands to a few million mostly idle connections (WhatsApp famously ran around 2 million per server on Erlang). Connections are long-lived, so the load balancer works at layer 4 (TCP) and a phone stays on one server until it disconnects. Phones send a small heartbeat every 30–60 s so dead connections are detected and mobile networks don't close the socket.

Routing: how does chat server 1 find Bob?

Alice and Bob are almost never on the same server. Options:

  • Broadcast to every server: simple, and hopeless at 400 servers × 1 M messages/s.
  • Session map (chosen): when Bob connects, his chat server writes user:bob → cs-2 into Redis with a TTL refreshed by his heartbeat. A sender's server looks it up, then forwards directly to cs-2. One lookup per message, which Redis handles easily (and servers can cache recent lookups).
  • Pub/sub channel per user: Bob's server subscribes to inbox:bob; senders publish there. Works well (it's the same idea with a broker doing the routing) but adds a broker in the hot path.

If the session map says Bob has no server, he's offline: go to the offline path.

Ordering: how do messages stay in order?

Phone clocks are wrong and networks reorder packets, so timestamps can't define order. Instead, the server assigns each chat a monotonically increasing sequence number when it stores a message. Phones display messages by seq, and a gap (got 41, then 43) tells the phone to fetch 42.

Who hands out the numbers? Options, with their catch:

OptionHowCatch
Redis INCR seq:{chat_id}Atomic counter in memoryRedis replicates asynchronously: after a failover the new primary can hand out a number that was already used
Database conditional writeInsert with seq = last + 1 only if last hasn't changed (a lightweight transaction)Slower (a consensus round per message) and retries under contention
One owner per chat (chosen)Consistent hashing maps each chat_id to one chat server or sequencer, which keeps the counter and persists last_seq with each messageNeeds a membership service so ownership moves cleanly when a server dies (with an epoch number so an old owner can't keep issuing numbers)

Assign the number in the same step that durably stores the message. If you allocate 42 and then the write fails, phones see a gap that will never be filled. If allocation and storage happen together, every number exists, and a gap really does mean "fetch it".

Ordering is only promised within a chat, never globally, which is what makes it cheap.

Exactly once: no lost messages, no duplicates

Networks fail in the middle, so we can't get exactly-once delivery from the network itself. We build it from two parts:

  1. At-least-once delivery: the sender retries until it gets an ack; the server only acks after the message is durably stored; the recipient's server retries pushing until the phone acks.
  2. De-duplication: every message has the client's msgId. The server ignores a second write with the same ID; the phone ignores a second delivery with the same ID or seq.

At-least-once plus idempotent processing gives effectively exactly-once. This same pattern (retry plus idempotency key) appears in payments, order systems and queues.

Offline users and push notifications

If Bob is offline, the message is stored in his pending inbox and the server asks the push service to send a notification through Apple (APNs) or Google (FCM). The notification carries no message content (it's encrypted anyway); it just wakes the app. When Bob's phone reconnects it calls sync?after=<last seq per chat>, receives everything it missed in order, acks, and the pending copies are deleted.

Store forever, or delete after delivery?

WhatsApp historically used store-and-forward: the server keeps a message only until every recipient device has it, then deletes it. History lives on the phones (and in user-controlled backups). This keeps server storage small and fits end-to-end encryption, since the server never needs to read old messages.

Slack and Telegram's cloud chats keep full, readable history on the server, which makes new devices and search easy but means storing everything forever. Messenger moved to end-to-end encryption by default in 2023 and keeps an encrypted history that only the user's devices can unlock. Either way, a server-side history plus end-to-end encryption rules out server-side search.

In an interview, state the choice and its trade-off: "I'll store until delivered, which keeps storage to the pending window; multi-device sync then needs each device to receive its own copy."

Group messages: fan-out

When Alice writes to a 40-person group, her chat server asks the group service for the members (cached), stores the message once under the group's chat_id, then fans it out: one delivery per member, each routed through the session map exactly like a 1:1 message, with offline members going to their pending inbox.

Fan-out on write is fine because WhatsApp caps groups (a few hundred to about a thousand members). For much larger "groups" (broadcast channels with millions of followers), you'd switch to fan-out on read: store once, and followers pull new posts when they open the channel, the same trade-off as a Twitter timeline.

End-to-end encryption and what it changes

With end-to-end encryption (the Signal protocol), phones exchange keys and encrypt messages so the server only ever handles ciphertext. Consequences for the design:

  • The server can't search, filter or generate previews of message content.
  • Each recipient device needs its own encrypted copy, so multi-device multiplies fan-out.
  • Groups use a shared "sender key" so a sender encrypts once per message rather than once per member.

Presence and "last seen"

Presence is the easiest part to over-build. Each connection's heartbeat updates last_seen:{user} in Redis. Instead of pushing every presence change to every contact (huge fan-out for little value), the phone subscribes to presence only for the chat that is open on screen, and asks for "last seen" when a chat is opened.

Media

Photos and videos never go through chat servers. The phone encrypts the file, uploads it straight to object storage (using a pre-signed URL from POST /v1/media), then sends a tiny chat message containing the media ID and the decryption key. The recipient downloads the encrypted blob from the CDN. At 1.5 PB a day, this separation is not optional.

7. Bottlenecks, failures and monitoring

Scaling bottlenecks

BottleneckFix
Millions of open connectionsMany chat servers behind an L4 balancer; tune each for ~1 M sockets; heartbeats every ~30 s
Message store write loadPartition by chat_id; write-optimised wide-column store; replicate across zones
Hot group (busy 1,000-member chat)Cache membership; batch fan-out per destination server
Media bandwidthObject storage + CDN; resumable uploads; thumbnails sent first

When things fail

FailureWhat happens
A chat server diesIts phones reconnect through the LB to another server; session map entries update; unacked messages are retried
Session map node lostRebuilt from reconnections; replicas take over
Recipient offline for weeksMessages wait in the pending inbox, with an expiry (about 30 days)
Push provider slowMessages still wait safely; the phone gets them on next connect

What to monitor

Message delivery latency (p99)Undelivered messages queuedConnections per serverReconnect ratePush success rateError and retry rates

8. Follow-up questions

Your turn: the interviewer pushes

Answer these three in order, out loud or in writing, before opening ours. 1) A chat server holding 250 K connections crashes. What happens to in-flight messages? 2) A user adds a second device. What changes? 3) A data centre blips and 50 M phones reconnect in one minute. What breaks?

Compare with our answer

A strong answer mentions:

  1. Nothing acked is lost, because acks follow durable writes; phones reconnect elsewhere with jitter and sync from their last sequence; unacked sends are retried and de-duplicated by message ID.
  2. Sessions, inboxes and encryption all become per device: each device gets its own copy (and its own encrypted copy), so fan-out grows with devices, not users.
  3. A reconnect storm hits load balancers, TLS handshakes and the session map; fix with jittered exponential backoff on clients, connection rate limits, and cheap session writes.

The senior-level questions, roughly in the order interviewers escalate:

How do read receipts work?

They’re just tiny messages from Bob’s phone back to Alice (status = read), routed exactly like normal messages.

How would you add multi-device?

Each device has its own connection and keys; a message is encrypted and fanned out to every device of the recipient.

How does end-to-end encryption affect the server?

The server only routes ciphertext. It can’t search, scan or index message content, so features like server-side search are impossible.

What about “last seen”?

Presence is driven by connection heartbeats, only pushed to contacts who are currently viewing that chat to avoid a presence storm.

More senior follow-ups
  • A chat server crashes with 1 M connections. Phones notice the dropped socket, reconnect (with jitter) through the load balancer to other servers, re-register in the session map and sync from their last seq. Nothing is lost because nothing was acked before being stored.
  • Multi-region. Users connect to the nearest region; the session map says which region and server holds a user; cross-region forwarding happens between chat servers. Message stores replicate across regions asynchronously, with each chat's sequence owned by one home region to keep ordering simple.
  • A hot group. A 1,000-member group in a noisy event produces 1,000 deliveries per message. Batch deliveries per destination chat server (one forward carrying all members on that server) instead of one per member.
  • Search. With end-to-end encryption, search happens on the phone over its local history.
WhatsApp on one page
  • Numbers: ~1.2 M msgs/s average (×3 with receipts), 10 TB text/day, 1.5 PB media/day (~140 Gbps up), 400 M open connections → ~2,400 chat servers at 250 K each with headroom.
  • Connections: WebSocket per online phone, L4 load balancer, heartbeats. Connections size the fleet.
  • Routing: session map in Redis user → chat server, TTL refreshed by heartbeat.
  • Write path: chat owner assigns seq while storing durably → ack (✓) → forward → recipient device acks (✓✓). Forwarding is not delivery.
  • Exactly once = retries (at-least-once) + client msgId de-duplication.
  • Offline: pending inbox + push (APNs/FCM) → sync by last seq on reconnect → delete after ack.
  • Groups: store once, fan out per member on write (groups are capped); fan-out on read for huge channels.
  • Storage: per-device inbox in a wide-column store, bucketed by day and expired by TTL (avoid tombstone pile-ups); group payload stored once.
  • Media: encrypted upload to object storage + CDN, only a link goes through chat.
  • E2E encryption: server sees ciphertext only; per-device copies; no server-side search.
Mock interviewPractise it as a mock interview: scope, size and the big decisionsOpens in the ByteAtlas lab. Your progress is saved in this browser.
Check what stuck

1. Why does WhatsApp-style chat keep a WebSocket open instead of polling over HTTP?

2. With 2 billion users and 20% online at once, what mostly decides how many chat servers you need?

3. Alice's message reaches chat server 1, but Bob is connected to chat server 2. How does server 1 find him?

4. How are messages kept in order within one chat?

5. The phone sends a message, the server stores it, but the ack is lost, so the phone sends it again. What prevents a duplicate?

6. Where should a 5 MB video sent in a chat travel?

7. Bob's phone is switched off when Alice messages him. What happens?

8. A design hands out each chat's sequence numbers with Redis INCR. What is the risk?

Score 60% or more and the lesson is marked complete.

Passing the quiz also marks it complete. Progress is saved in this browser only.