← Back to System Design, page 3

system-design-0058 · Hard senior

Design end-to-end encrypted messaging like WhatsApp or Signal, where the server delivers messages to all of a user's devices but can never read them.

Each device has its own identity key and uploads a batch of one-time prekeys to a key directory. A sender fetches the recipient's device keys, sets up a session with an X3DH-style handshake and encrypts each message with a ratchet that changes keys per message. The server only stores and forwards ciphertext per device. Multi-device means encrypting once per recipient device, so deliveries are about 580,000 per second for 290,000 messages per second.

Full answer

Requirements

Functional:

  • One-to-one and group messages delivered to every device a user owns (phone, laptop).
  • Offline devices receive messages when they reconnect.
  • Users can verify a contact's keys (safety numbers).

Non-functional:

  • The server cannot read message content, even if its database leaks.
  • Forward secrecy: stealing today's keys does not reveal past messages.
  • Low latency delivery and high availability.

Capacity estimates

Assumptions: 500M daily users, 50 messages each per day, 2 devices per user on average, 1 KB average ciphertext, 1B registered devices, 100 one-time prekeys per device at 32 bytes each.

  • Messages: 500M x 50 / 86,400 = about 290,000 per second.
  • Device deliveries: x 2 devices = about 580,000 encrypted copies per second.
  • Prekey storage: 1B x 100 x 32 bytes = 3.2 TB in the key directory.
  • Undelivered message queue: kept only until delivery (or 30 days), so storage stays small compared with a chat archive.

API

PUT /v1/keys/{device_id} {"identity_key", "signed_prekey", "signature", "one_time_prekeys": [...]} GET /v1/keys/{user_id} -> list of devices, each with identity key, signed prekey and one one-time prekey (consumed) WS /v1/messages send {"to_device": "...", "ciphertext": "base64", "msg_id"} ; receive same shape POST /v1/messages/ack {"msg_ids": [...]} -> server deletes delivered ciphertext

Data model

  • devices: user_id, device_id, identity_public_key, signed_prekey, registered_at.
  • one_time_prekeys: device_id, key_id, public_key (each handed out once, then deleted).
  • pending_messages: recipient_device_id, msg_id, sender_device_id, ciphertext, created_at. Deleted on ack.

The server never stores private keys or plaintext.

High-level design

mermaid
flowchart LR
  A[Sender device] -- "fetch keys" --> KD[(Key directory)]
  A -- "ciphertext per device" --> GW[Connection gateways]
  GW --> RT[Routing service]
  RT --> PRES[(Device presence)]
  RT --> PQ[(Pending messages)]
  RT --> GW2[Gateway of recipient]
  GW2 --> B1[Recipient phone]
  GW2 --> B2[Recipient laptop]
  RT --> PUSH[Push notification service]

How encryption works

  1. Session setup (X3DH idea). The sender combines its identity key and a fresh ephemeral key with the recipient device's identity key, signed prekey and one one-time prekey, using several Diffie-Hellman operations, to derive a shared secret. The recipient can compute the same secret from the first message even if it was offline.
  2. Double Ratchet. Every message advances a key chain, and every reply mixes in a new Diffie-Hellman result. Old keys are deleted, which gives forward secrecy and recovery after a compromise.
  3. Groups. Small groups can encrypt to each member device; large groups use sender keys: each sender shares a group key once with every member device, then encrypts each message once, which reduces work from one encryption per device to one per message.

The server routes by device ID and sees metadata (who talks to whom, when), but not content. Reducing metadata exposure (sealed sender) is an extra step.

Bottlenecks and trade-offs

  • Key directory trust. A malicious server could hand out fake keys. Safety numbers let users compare keys out of band; key transparency logs make substitution detectable.
  • Prekey exhaustion. If one-time prekeys run out, fall back to the signed prekey alone (slightly weaker) and ask the device to upload more.
  • Multi-device cost doubles delivery work and complicates adding a new device, which cannot read old history unless an existing device transfers it.
  • Server-side features such as search or spam filtering on content are impossible; they move to the device.
  • Backups must be encrypted with a user-held key, or they break the end-to-end promise.

By level

Fresher: Explain that messages are locked on the sender's phone and only the receiver's device holds the key to open them. A fresher should describe the server as a post office that cannot open envelopes.

Mid: Describe identity keys, prekeys and the handshake at a high level, plus per-device delivery and pending queues. Do the delivery math with multiple devices.

Senior: Cover the Double Ratchet, sender keys for groups, key transparency, prekey exhaustion, device linking, encrypted backups and metadata leakage. Expect clear reasoning about what the server can and cannot do.

Key points

  • Per-device identity keys and one-time prekeys in a key directory
  • X3DH-style handshake works even when the recipient is offline
  • Double Ratchet gives forward secrecy
  • Server stores and forwards ciphertext per device and deletes it on ack
  • Sender keys make large groups efficient

Common mistakes

  • Encrypting with one key per user instead of per device
  • Letting the server hold private keys "for backup"
  • Ignoring key substitution by the server
  • Assuming the server can still do content search or moderation

Follow-ups

  • How would you add a new device to an existing account?
  • How do encrypted backups work?
  • What metadata does the server still see and how can it be reduced?

Tips

  • Separate routing (server) from cryptography (devices)
  • Explain prekeys with the offline recipient case
  • Mention safety numbers for trust

Evaluation rubric

  • Correct key distribution model
  • Explains forward secrecy and session setup at a high level
  • Per-device delivery with capacity math
  • Discusses trust, groups and lost features