AlgoViz

Design WhatsApp

hard 50 min

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

See it run

A messenger, one hop at a time: connecting, sending, waiting, ticks, retries, presence and groups.

Operations

Open the connection

An HTTPS request becomes a two-way connection, after the token is checked.

Step 1 of 8

Upgrade request

Our systemPhoneLoad balancerChat server 42Auth serviceRedisregistry

The phone sends an HTTPS request that asks to become a WebSocket, with its token. Why: it passes ordinary firewalls and balancers.

Step 1: Upgrade request

def connect(request):    user, device = auth.check(request.token)   # before keeping a socket    socket = request.upgrade()                  # 101 Switching Protocols    n = next_connection_number()    registry.set(device, (MY_SERVER, n))    for entry in inbox.after(device, request.cursor):        socket.push(entry)    return socket
Latency
once
Load
100M open
Per server
100,000
Servers
1,250

Check the token first. Each open socket uses memory and a file descriptor.

In the real world

The same design whatsapp doing real jobs. Pick one and step through it.

100,000 phones lose their connection at once.

Step 1 of 4

Server 2 stops

Our systemAsha's phoneLee's phoneChat server 1Chat server 2Kafkamessage logWorkersCassandrainboxRedisregistry

inbox

121

Sockets close. Entry 121 is in the inbox, not yet delivered.

Step 1: Server 2 stops

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Acknowledging before storing

    critical

    A crash loses a message the sender believes is safe.

    Wrong
    session.reply("accepted")log.append(msg)
    Right
    log.append(msg)      # 2 of 3 zonessession.reply("accepted")
  • Claiming the ID before the work

    critical

    If the server dies after the claim, the retry is told it was done.

    Wrong
    if cache.set_if_absent(id):    log.append(msg)
    Right
    if not cache.seen(id):    log.append(msg)    cache.add(id)# the receiver drops any repeat
  • One topic per user

    major

    Hundreds of millions of topics cannot be run.

    Wrong
    topic = f"offline_{user}"
    Right
    log.append(topic="inbox", key=user)
  • Deleting each entry after its ack

    major

    Each delete leaves a marker that later reads must step over.

    Wrong
    for n in acked: inbox.delete(device, n)
    Right
    inbox.move_cursor(device, last)inbox.delete_range(device, upto=last)  # now and then
  • Presence to every contact

    major

    Nearly all of those notices are never seen.

    Wrong
    for c in contacts(user): notify(c)
    Right
    for v in viewers(user): notify(v)

Interview tips

  • Start from the promise

    • Say what one tick means.
    • Commit before you reply.
    • Clear after the ack.
  • Bring two numbers

    • 700K sends a second at peak.
    • 4.8M deliveries a second at peak.
    • The gap is groups and devices.
  • Show a failure

    • Lost ack: same ID.
    • Dead server: lease and cursor.
    • Lost push: inbox.
  • Know where to stop

    • A million users need version 3.
    • Huge rooms need another fan-out.
    • Calls are a different system.