Skip to main content

Chat App: Interview Guide

Editorial

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

AreaMid-LevelSeniorStaff
RequirementsSend, receive, offline delivery, groupsExactly once and in order; ticks; many devices; 30-day retention; end-to-end encryptionWhich promises the server keeps and which the device keeps; what the server may know
EstimateMessages a secondPeak over average from real data; deliveries over sends; connections and gateways with spare capacityStorage from how long messages wait (Little's law); what a confirmed message costs on the drives
ConnectionsA WebSocket per deviceGateways, a registry, heartbeatsThe registry race and compare-and-delete; reconnect storms and jitter; push notifications as wake-ups
DeliveryStore messages for offline usersA mailbox per device; store before the tick; acknowledge after storingPer-device sequence numbers and why a shared one loses messages; ids chosen by the sender; dedup on server and device
DurabilityA replicated databaseWhat the tick promises, and which replica has the message when it appearsFlush, replicate or both: what each survives and costs, measured
Encryption"Messages are encrypted"Keys per device; sessions with offline devices; client fan-outOne-time message keys and what they demand of the device; what the server still sees; abuse without content
GroupsSend to every memberSender keys; server fan-out through a log ordered per groupMembership changes and keys; receipts as sets; copies or one stored payload
PresenceOnline and offlineLast seen kept when the app closesPushing 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

DecisionChosenInstead ofBecause
Where messages waitA mailbox per deviceA shared message table or a flag per messageA device reads one ordered queue from its own position; a delivered message simply goes
NumberingA counter per device, locked until commitOne shared sequenceA shared sequence lets a late commit fall behind the reader and be lost
What the tick meansOn two machines in two zonesIn Redis's memoryAn asynchronous replica loses confirmed messages on failover
RetriesSender-chosen id, dedup on server and deviceServer-generated ids, auto-increment countersA retry carries its id; a counter repeats after a reinstall
GroupsSender keys, server fan-out from a logEncrypting per member, or members pollingOne encryption a message; one order; no polling
PresencePushed to people watchingBroadcast to every contactCost follows screens, not address books
ReconnectsExponential backoff with full jitterFixed exponential backoff36 s against 453 s for a million devices, in our simulation

Numbers to Remember

WhatValueSource
WhatsApp, 2014465M monthly users; 19B messages in, 40B out a day; 147M connectionsWhatsApp's talk at Erlang Factory
Busiest second over average1.56×the same
Connections per chat serverabout a million (over 2 million in 2012)the same; WhatsApp's blog
Our scale2B users; ~1.5M sends and ~3.1M deliveries a second at the peakestimated
Gateways~950630M connections, a zone of three to spare
Undelivered messages kept30 daysWhatsApp's privacy policy
Group size1,024WhatsApp's Help Center
Devicesa phone and up to four morethe same
A text envelopeabout 200 bytesSignal'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