Chat App: Requirements & High-Level Design
Problem Statement
Design a messaging app like WhatsApp. People send text messages to one person or to a group of up to 1,024 members, and share photos, videos and voice notes. A message reaches an online recipient within a second; for one who is offline it waits, for up to 30 days. Every message is shown exactly once, in the order its sender wrote it. The sender sees when it was sent, delivered and read. A person uses a phone and up to four other devices at the same time. Messages are end-to-end encrypted: the servers carry them but cannot read them. People see whether a contact is online and when they were last seen. The app has 2 billion users.
The functional requirements below come from its verbs; the rest of the statement sets the non-functional ones.
Introduction
WhatsApp is the largest system of this kind, and it once published its numbers. In March 2014 its servers took in 19 billion messages a day and sent out 40 billion, for 465 million monthly users; they held 147 million concurrent connections, about a million on each of some 150 chat servers. In 2020 it passed two billion users and said it delivers roughly 100 billion messages a day.
Sending a message looks like the simplest thing a server can do: receive bytes, pass them on. What makes it hard is everything that goes wrong in between:
| Challenge | Why it is hard |
|---|---|
| A connection to every phone | Hundreds of millions of devices stay connected at once. A message has to find the one server that holds its recipient's connection |
| Offline is normal | Phones sleep, lose signal, run out of battery. A message has to wait for its recipient safely and arrive when they are back, however often the network drops in between |
| Exactly once, in order | Networks lose acknowledgements, so everything is retried. A retry must not show a message twice or move it out of order |
| Many devices per person | A message goes to every device of the recipient, and to the sender's own other devices |
| The server cannot read | With end-to-end encryption the servers route sealed envelopes. No server-side search, no content filter, no history on the server |
| Groups | One message to a group of 1,024 people with five devices each is 5,119 deliveries, and every member must see the group's messages in the same order |
In this design every device keeps one long-lived connection to a gateway, and a small registry records which gateway holds which device. Each device has a mailbox on the server: an ordered queue of everything waiting for it. Sending a message means writing it into the mailboxes of the devices it is for; the sender's first tick (✓) means it is stored. The gateway that holds a receiving device pushes it; the device stores it and acknowledges, and the mailbox deletes it. A device that was away reads its mailbox from where it left off. The sending phone encrypts every message once for each receiving device, so the servers never see the text. A group message is encrypted once and copied into every member's mailboxes by a fan-out service.
Related lessons: the building blocks are in WebSockets: The Two-Way Street, The 10K Connections Problem, L4 vs L7 Load Balancing (+ Sticky Sessions), Idempotency: Safe to Retry, Delivery Semantics: At-Most, At-Least, Exactly-Once, Calling Services Safely: Timeouts, Retries & Backoff and Queue Models: Broker (RabbitMQ) vs Log (Kafka) (Fundamentals of System Design), Sync vs Async Replication & Failover and Choosing a Shard Key (Data Systems at Scale), Heartbeats & Failure Detection, Ordering in Practice and Retries, Budgets & Retry Storms (Distributed Systems & Reliability), and Pattern: Real-time Updates, Pattern: Fan-out — Feeds & Timelines, Pattern: Handling Large Blobs and Kafka vs RabbitMQ vs SQS (System Design Interview Mastery).
Functional Requirements
We extract the operations from the verbs of the problem statement:
FR1 — Send and receive. A user sends a text message to another user. It appears on every device of the recipient and on the sender's own other devices. Sending the same message again, as a phone does after a timeout, never makes two.
FR2 — Offline delivery. Messages for a device that is offline wait on the server and arrive, in order, when it reconnects. A message waits up to 30 days and is then deleted.
FR3 — Message status. The sender sees one tick when the server has the message, two when it reached one of the recipient's devices, and two blue ones when it was read. In a group the second tick appears when every member has it.
FR4 — Groups. A user creates a group of up to 1,024 members, adds and removes members, and sends to it.
FR5 — Many devices. A user links up to four devices to their phone. Each one sends and receives on its own, including while the phone is off.
FR6 — Media. Users send photos, videos, voice notes and files.
FR7 — Presence. A user sees whether a contact is online or typing, and when they were last seen.
Out of Scope
- Voice and video calls
- Status updates, Channels and Communities
- Registration, phone-number verification and finding which contacts use the app
- Backups of a user's chats to their own cloud storage, and keeping history on the server (a staff-level topic in the Interview Guide)
- Search: with end-to-end encryption it runs on the device
- Payments, businesses and bots
Non-Functional Requirements
The rest of the problem statement gives the quality requirements:
- "reaches an online recipient within a second" → Latency
- "waits, for up to 30 days" → Durability and Retention
- "shown exactly once, in the order its sender wrote it" → Correctness
- "the servers carry them but cannot read them" → Privacy
- "2 billion users" → Scale
One more follows from what the app is for, reaching people: Availability.
Latency
- A message reaches an online recipient's device within 1 second at the 99th percentile
- The sender sees the first tick within 500 ms
Availability
- Sending and receiving: 99.99%, about 52 minutes a year. A gateway, a database server or a whole data-center zone can fail without users noticing more than a reconnect
Durability
- A message the server has confirmed with a tick survives the loss of any one server, also in the second after the tick
Retention
- A message is deleted from the servers as soon as every device it was meant for has it, and after 30 days if some device never comes back
Correctness
- A message appears once on each device. One sender's messages appear in the order they were written. Every member of a group sees the group's messages in the same order
Privacy
- The servers store and forward ciphertext only. They know who sends to whom, when and how much, but not what
Scale
- 2 billion users: about 1.5 million messages a second and 630 million connected devices at the peak (estimated next)
Resource Estimation
What People Do
WhatsApp published one detailed set of numbers, in a talk by one of its engineers at Erlang Factory in March 2014. We take the shape of the traffic from it and scale it to our 2 billion users.
| Assumption | Value | Based on |
|---|---|---|
| Users | 2 billion | the problem statement (WhatsApp since 2020) |
| Messages sent a day per user | 41 | WhatsApp, 2014: 19 billion in a day for 465 million monthly users |
| Busiest second over the average second | 1.56× | WhatsApp, 2014: 342,000 messages in at the peak, 220,000 a second on average |
| Deliveries per message sent | 2.1 | WhatsApp, 2014: 40 billion messages out a day for 19 billion in, because a group message goes out many times |
| Devices connected at once | 32% of users | WhatsApp, 2014: 147 million concurrent connections for 465 million monthly users |
| One envelope (an encrypted message with its routing) | about 200 bytes | a typical text is 36 bytes (the median of 55,835 real text messages); Signal's library makes 137 bytes of ciphertext of a 40-byte text; the server adds about 50 bytes of ids and times |

Traffic
| What | A day | A second | At the peak |
|---|---|---|---|
| Messages sent | 2 billion × 41 = 82 billion | 946,000 | ~1.5 million |
| Deliveries (one per receiving device) | 82 billion × 2.1 = 172 billion | 2.0 million | ~3.1 million |
| Logins (devices connecting) | ~990,000 (WhatsApp, 2014: 230,000) |
A check: in 2020, with two billion users, WhatsApp said it delivers roughly 100 billion messages a day. That lies between our 82 billion sent and 172 billion delivered.
Two of these numbers surprise people. The busiest second is only 1.56 times the average: the users live in every time zone, so the evening of one country is the morning of another. And the servers send out more than twice what comes in, and that was before linked devices: each one receives its own copy of every message too.
Connections
| What | Calculation | Result |
|---|---|---|
| Devices connected at the peak | 2 billion × 32% | ~630 million (each online linked device adds one) |
| Gateways at a million connections each | 630 million ÷ 1 million | ~630 |
| Gateways that survive the loss of one zone of three | 630 × 3 ÷ 2 | ~950 |
| Registry, one entry per connected device | 630 million × 126 bytes | ~80 GB of memory |
A million connections on one server is WhatsApp's own figure: in 2014 each chat server held about a million phones, and in January 2012 WhatsApp reported one server holding over 2 million TCP connections with 42% of its CPU idle. What limits a gateway is memory and kernel settings, not a magic number. The spare capacity is the point of the last row: when a zone goes dark, its devices reconnect to the other two, and those must have room for them.
Storage
A messenger that deletes delivered messages stores only what is waiting. How much that is follows from Little's law: the number of envelopes waiting equals the deliveries per second times how long an envelope waits on average. In PostgreSQL a waiting envelope took 282 bytes with its index:
| Average wait | Envelopes waiting | Storage |
|---|---|---|
| 10 seconds | 20 million | 5.6 GB |
| 1 minute | 120 million | 34 GB |
| 10 minutes | 1.2 billion | 337 GB |
| 1 hour | 7.2 billion | 2.0 TB |
| 1 day | 172 billion | 49 TB |
The number of messages sent does not decide the storage; the time they wait does. WhatsApp's 2014 talk put it in five words, "Most messages picked up very quickly"; the average is pulled up by the phones that stay off for days, and the 30-day limit caps it.
Bandwidth
| What | Calculation | Result |
|---|---|---|
| Text at the peak | 3.1 million deliveries × 200 bytes | ~610 MB/s, ~4.9 Gb/s |
| Media on a peak day | WhatsApp, Christmas Eve 2013: 146 Gb/s, scaled by users × 4.3 | ~630 Gb/s |
Text is cheap. Photos and videos are over a hundred times more traffic, which is why they travel through their own servers and a content delivery network, not through the chat connection.
Data Model
The servers keep very little: who has which devices, what waits for each device, and which sends they have already stored. The text lives on the devices.
On the Mailbox Servers
The tables are those the tests run, in PostgreSQL. They are split across servers by user_id, so all devices of one person live on the same server.
-- every device of every account on this shard, and the next position in its mailbox
CREATE TABLE devices (
user_id bigint NOT NULL,
device_id smallint NOT NULL, -- 1 is the phone, 2 to 5 are linked devices
next_seq bigint NOT NULL DEFAULT 1,
PRIMARY KEY (user_id, device_id)
);
-- what waits for a device, in the order the device reads it
CREATE TABLE mailbox (
user_id bigint NOT NULL,
device_id smallint NOT NULL,
seq bigint NOT NULL,
message_id uuid NOT NULL, -- chosen by the sending device
sender_user bigint NOT NULL,
sender_device smallint NOT NULL,
envelope bytea NOT NULL, -- ciphertext: the server cannot read it
stored_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, device_id, seq)
);
-- sends this shard has stored, so that a retry stores nothing
CREATE TABLE accepted (
user_id bigint NOT NULL,
sender_user bigint NOT NULL,
sender_device smallint NOT NULL,
message_id uuid NOT NULL,
accepted_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, sender_user, sender_device, message_id)
);devicesholds each device'snext_seq, the number its next envelope gets. Every device has its own sequence: its mailbox is an ordered queue that it reads from where it left off.mailboxholds one row per envelope waiting for a device. Theenvelopeis ciphertext, encrypted on the sending device for this device alone. A row lives until the device acknowledges it, at most 30 days.message_idis chosen by the sending device, a random UUID. It is the message's name everywhere: in every envelope, in the retries, in the receipts, on every device. A counter would not do: it starts again after a reinstall.acceptedremembers for 30 days which sends this server has stored, so that a retry stores nothing new.
Elsewhere
| Data | Where | Key | Lives |
|---|---|---|---|
| Which gateway holds a device | Redis | conn:{user_id}:{device_id} → gateway, connection | while the device is connected |
| Public keys of each device: identity key, signed prekey, a batch of one-time prekeys | key directory | user and device | until replaced; a one-time prekey is deleted when handed out |
| Groups and their members | group service | group, then member | as long as the group exists |
| Group messages before their fan-out | Kafka topic, keyed by group | group | a few days, far longer than a fan-out takes |
| Last seen | key-value store | user | as long as the account |
| Photos, videos, files (encrypted) | blob store, behind a CDN | random id | 30 days |
API Design
A device keeps one long-lived connection to a gateway: a WebSocket over TLS, a standard any browser can open. WhatsApp's phones use their own protocol inside an encrypted channel built with the Noise framework, which hides even who is connecting from anyone watching the network. The frames are shown as JSON; a real client sends a compact binary encoding.
Device → Server
Send a message. One frame carries one envelope per receiving device: the recipient's devices, and the sender's own other devices so that they show the message too.
{
"type": "send",
"id": "4f1c9b2e-8d7a-4c3e-9b1f-2a6d5e8c7b90",
"to": "user:1290001",
"envelopes": [
{"user": "user:1290001", "device": 1, "ciphertext": "Mwohc2Fk…"},
{"user": "user:1290001", "device": 2, "ciphertext": "MwogE1Rx…"},
{"user": "user:1170042", "device": 2, "ciphertext": "Mwoh9fQa…"}
]
}
The server stores every envelope and answers with the first tick; for a group message the reply also carries the message's position in the group's log, by which every device orders the group:
{"type": "stored", "id": "4f1c9b2e-8d7a-4c3e-9b1f-2a6d5e8c7b90", "at": 1760433000123}
If the recipient linked or removed a device since the sender last looked, the server refuses the send and returns the devices the account has now. The sender fetches keys for the new device, encrypts again and resends with the same id; whatever was already stored is not stored again:
{"type": "devices_changed", "id": "4f1c9b2e-8d7a-4c3e-9b1f-2a6d5e8c7b90", "user": "user:1290001", "devices": [1, 2, 3]}
Read the mailbox. On every connect the device asks for what came after the last envelope it stored, then acknowledges up to the last one it has stored now:
{"type": "sync", "after": 8790}
{"type": "ack", "upto": 8812}
Receipts travel as envelopes, like messages: the receiving device sends "delivered" and later "read" for a list of message ids, to every device of the sender.
Presence. The device reports when the app comes to the foreground and goes to the background, since "online" means the app is open. While a chat is open, it asks for the other person's presence and reports typing:
{"type": "active", "on": true}
{"type": "watch", "user": "user:1290001"}
{"type": "typing", "to": "user:1290001", "on": true}
Server → Device
An envelope from the mailbox, in seq order:
{
"type": "envelope",
"seq": 8812,
"id": "4f1c9b2e-8d7a-4c3e-9b1f-2a6d5e8c7b90",
"from": "user:1170042",
"from_device": 1,
"ciphertext": "Mwohc2Fk…",
"at": 1760433000123
}
Presence of a watched user: {"type": "presence", "user": "user:1290001", "online": false, "last_seen": 1760432400000}.
HTTPS
| Method | Path | Does |
|---|---|---|
GET | /v1/keys/{user_id} | The key bundle of each of the user's devices: identity key, signed prekey with its signature, and one one-time prekey, which the server deletes as it hands it out |
PUT | /v1/keys | The device uploads a new batch of one-time prekeys when it runs low |
POST | /v1/media | Uploads an encrypted file; returns its id |
GET | /v1/media/{id} | Downloads an encrypted file, through the CDN |
POST | /v1/groups | Creates a group; PUT and DELETE on /v1/groups/{group_id}/members/{user_id} add and remove members |
High-Level Design
We build the design up one requirement at a time.
1. One Server
The simplest chat server keeps every connection in memory, in a map from user to socket. A message from Alice is a lookup of Bob's socket and a write to it.

Three things break at once:
- Size. One server holds about a million connections. At the peak we have about 630 million.
- Failure. When the server dies, every connection goes, and so does every message it was holding in memory.
- Offline. If Bob is not connected, the message has nowhere to go.
2. Gateways and a Registry
Connections move to a fleet of gateways. A load balancer gives each new connection to some gateway; it does not know who is connecting and does not need to. After the device has authenticated, the gateway writes one entry in a registry: device → gateway.
When Alice's message arrives, the message service looks up each of Bob's devices in the registry and asks those gateways to push it.

The registry is a Redis hash per device, conn:{user_id}:{device_id}, holding the gateway and the id of this connection. The gateway refreshes it with an expiry while the device is connected, so the entries of a gateway that dies run out on their own. When a connection closes, the gateway removes the entry only if it still names that connection:
-- KEYS[1] = conn:{user_id}:{device_id}, ARGV[1] = the id of the connection that closed
if redis.call('HGET', KEYS[1], 'connection') == ARGV[1] then
return redis.call('DEL', KEYS[1])
end
return 0A plain DEL would be wrong. A phone that switches from Wi-Fi to mobile data often reconnects through another gateway before the first one notices the old connection is dead. The late DEL would then erase the new entry, and the phone would be connected but unreachable. We ran both versions: DEL left no entry, the script kept the new one.
What is still broken: a message for an offline device has nowhere to wait, and one that a gateway is pushing when it dies is gone.
3. A Mailbox for Every Device
The message service now stores first and delivers second. Each device has a mailbox, an ordered queue on a mailbox server. A message is written into the mailboxes of the devices it is for, and only then does the sender get its first tick. Delivery reads from the mailbox; a device deletes from its mailbox by acknowledging.

- Alice's phone sends the message to the message service, through its gateway.
- The message service writes one envelope into each of Bob's devices' mailboxes, in one transaction, and replies "stored": Alice sees ✓.
- Bob's laptop is connected, so the registry names its gateway, and the service tells that gateway there is new mail. The gateway reads the laptop's mailbox and pushes the envelopes in order.
- The laptop stores them and acknowledges up to the last one:
{"type": "ack", "upto": 3302}. The mailbox deletes everything up to 3302. - The laptop sends a "delivered" receipt to Alice's devices: she sees ✓✓.
Bob's phone is off, so the message service sends it a push notification through Apple's or Google's service. It carries no message, only the news that there is mail; the phone wakes up, connects, asks for everything after the last envelope it stored (8809) and receives 8810 to 8812 in order. A push cannot carry delivery: Apple calls its service "best-effort", it "may reorder notifications", and for a device that is offline it keeps only one notification per app.
The mailbox is what makes delivery safe. A gateway can die while pushing: nothing was deleted, and the device reads the same envelopes again after it reconnects. The message waits as long as it has to, and leaves the server the moment every device has it. The Delivery Deep Dives show why each envelope has a number from its device's own sequence, how the device makes sure it shows each message once, and where the mailboxes live.
4. Many Devices, Encrypted End to End
Bob has a phone and a laptop; Alice has a phone and a tablet. Each device has its own keys, and the servers never see the text, so the sending device encrypts the message once for every device that must show it: Bob's phone, Bob's laptop and Alice's own tablet. WhatsApp calls this client fan-out.
![One message, one envelope per device. The sending phone encrypts for every device that must show it. 1: the first time Alice writes to Bob, her phone fetches the keys of Bob's devices from the key directory, once. 2: it encrypts the text once for each device: bob · phone, bob · laptop and alice · tablet, her own other device, and sends the three envelopes to the message service. 3: the service checks the devices each account has now: bob 1 and 2, alice 2 (besides the sending phone); both match. It writes two envelopes on Bob's mailbox server, for his phone and laptop, and one on Alice's, for her tablet. If Bob linked a third device a minute ago, the server refuses the send with devices_changed, bob, [1, 2, 3]; the phone encrypts for the new device too and resends with the same message id.](https://pub-a1b3030acfb94e84ba8a89fb182c53bc.r2.dev/public/chat-app/client-fanout.webp)
- Alice's phone asks the key directory for the key bundles of Bob's devices, the first time it writes to him. With them it sets up an encrypted session with each device, even while Bob is offline.
- It encrypts the text once per session: three envelopes, three different ciphertexts.
- It sends them in one
sendframe. The message service writes Bob's two envelopes on Bob's mailbox server and Alice's tablet's envelope on hers. - Each server checks that the envelopes cover exactly the devices the account has now. If Bob linked a third device a minute ago, it refuses the send with the new list, and Alice's phone encrypts for that device too and sends again.
Without that check, a message would silently never reach Bob's new laptop. With it, a newly linked device receives every message sent after it was linked. Its older history comes from Bob's phone, which encrypts a bundle of recent chats for it; the servers never had that history to give.
5. Groups
Encrypting a group message once per member device would cost up to 5,119 encryptions for a full group. Instead each member has a sender key for the group, which it gives once to every other member device over their encrypted sessions. After that, each message is encrypted once, and the same ciphertext goes to every member.

- Alice's phone encrypts the message once with her sender key and sends
{"type": "send", "to": "group:88231", …}with a single envelope. - The group service appends it to a Kafka topic keyed by group id: all of a group's messages go to one partition, in one order. Once Kafka has it on at least two replicas (
acks=all,min.insync.replicas=2), Alice sees ✓, and the reply tells her phone the message's position in the log. - A fan-out worker reads the partition. For each member, it writes the ciphertext into every one of that member's device mailboxes, batched by mailbox server, and then notifies their gateways.
- A member's devices receive and acknowledge it like any message. Each sends a "delivered" receipt to Alice's devices; her phone shows ✓✓ when every member has it.
Every device orders the group's messages by their position in the partition, so all members see the same order. Writing the copies after the tick keeps a full group's 5,119 mailbox writes off the sender's wait; the log keeps the message safe until they are done.
6. The Whole Design

| Component | What it does | Sized for the peak |
|---|---|---|
| Gateways | Hold the devices' connections; authenticate; push envelopes; send heartbeats | ~950, a million connections each, a third to spare |
| Registry | Device → gateway, with expiry | ~80 GB in a Redis cluster, replicated |
| Message service | Checks device lists; writes envelopes into mailboxes; tells gateways; sends push notifications | stateless; ~1.5 million sends a second |
| Mailbox servers | One ordered queue per device; delete on acknowledgement; 30-day expiry | ~3.1 million envelopes a second; split by user, each split replicated |
| Key directory | Each device's public keys; hands out one-time prekeys once | read once per new conversation |
| Group service and Kafka | Members; one log partition per group; fan-out workers | the group share of the traffic |
| Presence service | Online, last seen, typing, for the people watching | changes, not polls |
| Media service | Encrypted files in a blob store, behind a CDN | ~630 Gb/s out at the peak |
| Push service | Wakes devices through Apple's and Google's services | one per waiting device |
The life of one message, end to end:

- ✓ appears when the message is stored in every mailbox it is for (or, in a group, in the group's log).
- ✓✓ appears when one of the recipient's devices has stored it and sent its receipt; in a group, when every member has.
- Blue ✓✓ appears when the recipient opens the chat and their device sends a "read" receipt.
The three Deep Dives that follow take the design apart: delivery (how a message is shown exactly once, where messages wait, the order they arrive in, and what happens when connections drop), then groups and encryption (end-to-end encryption, many devices, groups, presence, media and the size of the fleet).