Chat App: Interview Guide
Staff-Level Discussion Topics
These are the open questions a staff-level interview moves on to once the design stands.
Keeping History on the Server
Facebook Messenger, as it was built in 2014, and Discord keep messages on the server, so that any device can scroll back to the beginning. That turns the mailbox design around: the server stores every message for good, per conversation, and devices read pages of it.
- Two stores, one stream. Facebook built Messenger's sync around Iris, "a totally ordered queue of messaging updates … with separate pointers into the queue indicating the last update sent to your Messenger app and the traditional storage tier". "A week's worth of messages" is served from the queue's own store when an app has been offline for a while; older history comes from the long-term store.
- Partitions that do not grow forever. Discord stores a channel's messages under the key (channel, time bucket), because "a single Discord channel can exist for years and perpetually grow in size"; it sized buckets at "about 10 days of messages" to stay under 100 MB. By 2022 that database had 177 Cassandra nodes holding trillions of messages; on 72 ScyllaDB nodes, reading history went from a p99 of 40–125 ms to 15 ms.
- And encryption? History on the server and end-to-end encryption pull against each other. WhatsApp keeps history on the devices and moves it between them encrypted; a server-side history would have to hold ciphertext only the user's devices can read.
Many Regions
Users write to people on other continents. Each person's mailboxes live in a home region; a gateway near the sender takes the message and the message service writes it into the recipient's home region, so the tick waits for one cross-region write. What happens when a region fails, whose mailboxes move where, and how a device finds its mailbox after a move are the questions to settle.
The Language of the Gateways
WhatsApp built its servers on Erlang and FreeBSD and held about a million phones per chat server in 2014. The lesson of its numbers is not a language: it is that a connection server is sized by the memory one idle connection takes and by what the operating system allows, and that both are measured, then tuned. A team picks the stack it can operate, and measures.
Abuse Without Reading Messages
With end-to-end encryption, the server sees behavior, not content. WhatsApp bans over 8 million accounts a month, 75% of them without a recent user report; it counts on reports, which carry the reporter's own copy of the messages; and its app limits forwarding. Which signals, which limits, and how to appeal a ban are product decisions with system consequences.
Level Expectations
| Area | Mid-Level | Senior | Staff |
|---|---|---|---|
| Requirements | Send, receive, offline delivery, groups | Exactly once and in order; ticks; many devices; 30-day retention; end-to-end encryption | Which promises the server keeps and which the device keeps; what the server may know |
| Estimate | Messages a second | Peak over average from real data; deliveries over sends; connections and gateways with spare capacity | Storage from how long messages wait (Little's law); what a confirmed message costs on the drives |
| Connections | A WebSocket per device | Gateways, a registry, heartbeats | The registry race and compare-and-delete; reconnect storms and jitter; push notifications as wake-ups |
| Delivery | Store messages for offline users | A mailbox per device; store before the tick; acknowledge after storing | Per-device sequence numbers and why a shared one loses messages; ids chosen by the sender; dedup on server and device |
| Durability | A replicated database | What the tick promises, and which replica has the message when it appears | Flush, replicate or both: what each survives and costs, measured |
| Encryption | "Messages are encrypted" | Keys per device; sessions with offline devices; client fan-out | One-time message keys and what they demand of the device; what the server still sees; abuse without content |
| Groups | Send to every member | Sender keys; server fan-out through a log ordered per group | Membership changes and keys; receipts as sets; copies or one stored payload |
| Presence | Online and offline | Last seen kept when the app closes | Pushing only to people watching; privacy rules |
Interview Cheatsheet
The Design in 60 Seconds
"Every device keeps one connection to a gateway, and a registry maps each device to its gateway. Each device has a mailbox, an ordered queue on a sharded, replicated store. A send writes one envelope per receiving device into those mailboxes and the sender sees one tick; the gateways push, the device stores and acknowledges, the mailbox deletes. A device that was away reads from its last position. Ids are chosen by the sender, so retries are harmless; devices store before they acknowledge. Messages are encrypted per device on the sending phone; groups use sender keys, one ciphertext appended to a log per group and fanned out to every member device. Messages wait up to 30 days."
Key Trade-offs
| Decision | Chosen | Instead of | Because |
|---|---|---|---|
| Where messages wait | A mailbox per device | A shared message table or a flag per message | A device reads one ordered queue from its own position; a delivered message simply goes |
| Numbering | A counter per device, locked until commit | One shared sequence | A shared sequence lets a late commit fall behind the reader and be lost |
| What the tick means | On two machines in two zones | In Redis's memory | An asynchronous replica loses confirmed messages on failover |
| Retries | Sender-chosen id, dedup on server and device | Server-generated ids, auto-increment counters | A retry carries its id; a counter repeats after a reinstall |
| Groups | Sender keys, server fan-out from a log | Encrypting per member, or members polling | One encryption a message; one order; no polling |
| Presence | Pushed to people watching | Broadcast to every contact | Cost follows screens, not address books |
| Reconnects | Exponential backoff with full jitter | Fixed exponential backoff | 36 s against 453 s for a million devices, in our simulation |
Numbers to Remember
| What | Value | Source |
|---|---|---|
| WhatsApp, 2014 | 465M monthly users; 19B messages in, 40B out a day; 147M connections | WhatsApp's talk at Erlang Factory |
| Busiest second over average | 1.56× | the same |
| Connections per chat server | about a million (over 2 million in 2012) | the same; WhatsApp's blog |
| Our scale | 2B users; ~1.5M sends and ~3.1M deliveries a second at the peak | estimated |
| Gateways | ~950 | 630M connections, a zone of three to spare |
| Undelivered messages kept | 30 days | WhatsApp's privacy policy |
| Group size | 1,024 | WhatsApp's Help Center |
| Devices | a phone and up to four more | the same |
| A text envelope | about 200 bytes | Signal's library and real text messages |
Follow-up Questions
- "What does the first tick promise?" That the message is stored where it survives the loss of any one server: on two machines in two zones.
- "The recipient's phone is off for a week. What happens?" Its mailbox keeps the envelopes; a push notification tries to wake it; when it connects it reads from its last position. After 30 days what is left is deleted.
- "How do you avoid showing a message twice?" The sender's id makes a retried send a no-op on the server; the device skips positions it stored and drops a second copy by its id, before decrypting.
- "How do you keep messages in order?" A counter per mailbox; the sender sends its outbox in order; a group's messages go through one log partition.
- "How do read receipts work across devices?" As envelopes to the sender's devices and to the reader's own other devices; in a group, the sender keeps the set of members that read.
- "What changes with end-to-end encryption?" The sending device encrypts per receiving device; the server checks the device list; search and history stay on the devices; abuse is handled from behavior, reports and app limits.
- "A gateway dies. What happens?" Its devices reconnect elsewhere with jittered backoff; their registry entries expire; nothing is lost, because nothing lived only on the gateway.
Common Mistakes
- Treating the registry as the delivery mechanism: a message must be stored before anyone looks up where to push it
- Deleting a registry entry without checking that it still names the connection that closed
- Numbering mailbox entries from one shared sequence
- Ordering messages by timestamp
- Keeping undelivered messages only in Redis
- Generating message ids on the server, or from a counter on the device
- Acknowledging a batch before it is stored on the device
- "Encrypt with Bob's public key": each device has its own sessions, and every message its own key
- Pushing presence to every contact
- Retrying with backoff but without jitter