AlgoViz

Design WhatsApp

hard 50 min

Build a messenger like WhatsApp: one-to-one and group chats, ticks, presence and several devices per person.

Video lesson · Chapter 1 of 5

A phone registers its new connection

0:00 / 3:50

Theory

Read or listen first. About 1 min.

What it is

A messenger carries short notes between people's devices. If the receiver is connected, the note arrives in a moment. If not, the service keeps it and hands it over later. This is called store and forward.

This page designs a service like WhatsApp as an exercise. It is our own design. It does not claim to describe how that company's systems work.

Why it exists

People trust a messenger with things they will not repeat. A lost message is far worse than a slow one, so the design starts from a promise about delivery.

It also brings together problems that appear in many systems: very many open connections, routing to a machine that holds state, retries without duplicates, and small writes at a huge rate.

A short history

  1. May 1993

    RFC 1459 describes Internet Relay Chat, where servers pass messages between connected users.

  2. February 2009

    WhatsApp Inc. is incorporated in California.

  3. December 2011

    RFC 6455 standardises WebSocket, a two-way connection that starts as an HTTP request.

  4. April 2016

    WhatsApp announces end-to-end encryption for all its users.

  5. July 2021

    Meta's engineering blog describes encrypted multi-device messaging, where the sender's app encrypts once for each device.

  6. July 2023

    RFC 9420 publishes Messaging Layer Security, an open standard for encrypted group messaging.

Words to know

Store and forward
Keep a message safely, then pass it on when the receiver can take it.
Inbox
The ordered list of messages waiting for one device.
Acknowledgement (ack)
A short reply that says: I have it, you may stop resending.
Fan-out
Turning one message into one delivery for each receiving device.
Session registry
The lookup that says which server holds a device's connection.
End-to-end encryption
Only the devices can read the message. The service carries sealed bytes.
Presence
Whether somebody is online, and when they were last seen.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Commit. Append the message to a replicated log, then show one tick

Understand it

The pigeon loft
LeeLOFTNoorLeeMaxA sealed note

The loft promises to keep the note, not to fly fast. Speed is a bonus when somebody is home.

Scope

What are we building?

Two people, or a group, exchange messages. Both may have several devices. Networks drop, phones die and servers restart, and still every acknowledged message arrives once on screen, in order.

  • Chat

    One-to-one and groups of up to 500 people.

  • Ticks

    Sent, delivered, read: three facts with three different meanings.

  • Presence

    Online, last seen and typing. Useful, but allowed to be wrong for a moment.

  • Many devices

    Each device has its own inbox, keys and position.

The hard part

One tick is a promise

A message's life

Read is sent only if the reader allows it. With read receipts off, the state stays at Delivered.

The finished system

Where the design ends up

This is the last version. The Design tab starts from one service and one database, and shows the number that forces each extra box.

The whole system

Phones connect through a load balancer to chat servers. Chat servers append messages to a Kafka message log. Fan-out workers read group members from PostgreSQL and append one entry per receiver to a Kafka inbox log. Inbox writers store entries in Cassandra, look up the session registry in Redis and hand the message to the right chat server. A push service wakes phones through APNs and FCM outside our system. Encrypted media goes straight to object storage.
Blue is sending, pink is delivery, orange is waking an offline phone. Dashed means it may not happen or is not waited for.

Summary

The whole topic in one minute.

A messenger is a set of inboxes and a very large number of open connections. A message is committed to a replicated log before the sender gets a tick. It is copied to an inbox for every receiving device, and removed only after that device says it has it.

Pushing to a live connection is only a shortcut to make delivery fast. If any server, cache or push fails, the inbox still holds the message and the next connection collects it.

  • Commit to a replicated log before showing one tick.
  • Each device has an inbox with a cursor, so an ack is one number and a reconnect is one range read.
  • The session registry is a hint for speed. It can be lost and rebuilt without losing a message.
  • Every retry carries the same message ID, and the receiver drops repeats.
  • Order is kept within one conversation, never across all of them.

Sources