Ch.54: WhatsApp System Design: When Your Phone Is Offline

Transcript

0:00 WhatsApp is a messaging app. People use it to exchange private messages, like telling a friend where to meet. We'll design a WhatsApp-inspired system for delivering those messages across devices you've approved, including how another device gets history and what changes when you remove one. Now imagine your phone fails and shuts down, but your already-linked online laptop still receives a message. We'll trace how that works without letting the delivery service read it. Now follow that laptop's delivery on the complete map.

0:29 The sender finds authorized devices, encrypts for each destination, then sends sealed messages through gateways into device mailboxes. So that delivery path never asks the phone to wake up. Recipients open their own copies; a separate authorized history transfer helps a device joining later catch up. That independence was the redesign. WhatsApp's published engineering account describes companions moving away from the phone as their middleman. We'll distinguish its documented behavior from our choices.

0:55 Source links are in the description. Next, that laptop has to receive while the phone's off. Functional requirements also include sending messages and catching up after a disconnection. Also link a new device, share permitted history with it, and remove it later. But linking still needs authorization from the account's primary device. So the dead-phone opening doesn't mean a stranger can register a laptop while your phone's off. We're using one that was already linked. For non-functional requirements, suppose a gateway restarts just after saying it accepted our envelope.

1:26 What must survive? That copy. Acceptance has to mean durable storage, not just a gateway hearing us. Keep its content private from the backend, and limit storage and retry work. Does an offline laptop count as a delivery failure even while its mailbox is working and keeping every pending envelope? I'd measure online delivery latency separately from disconnection time. Keep device-level progress visible; one closed laptop shouldn't make a healthy delivery path look broken. Next, independent endpoints need identities.

1:58 Our directory maps an account to authorized devices and their public setup material, safe to share with senders. Each device keeps private keys, secret cryptographic material used to establish protected conversations. The directory carries public material, not a spare key for everyone's messages. Wait, I'm not sure that directory is enough. Suppose a server quietly adds another recipient. What proves the account owner allowed it? A returned row isn't proof of permission. WhatsApp documents signed device lists: a cryptographic signature lets clients check that membership was authorized, rather than merely returned by a server.

2:30 But the signature still needs a trusted starting identity. Otherwise we're carefully checking permission from the wrong account owner. Exactly. Our design assumes the contact's account identity has been authenticated, meaning established as the intended person's identity. Membership checks build on that starting point; they don't create it. Give me a concrete trust check. If I'm first messaging a friend, how do we establish that starting identity rather than just believe the directory? Compare identity-verification codes with that friend in person, or over another independently trusted channel.

3:02 WhatsApp documents that option; the server isn't vouching for its own answer. Keep three records distinct: the human account, its authorized device identities, and today's network connections. Reconnecting a laptop doesn't make it a newly trusted device. That identity check leaves a timing problem. How can the sender establish encryption for a laptop that's offline and can't answer a handshake? The laptop has already published public setup material. The sender combines it with secret inputs kept on its own device to begin a session, protected state for that pair.

3:34 So we aren't waiting for two people to be online together. Yes. The recipient later combines the sender's public setup information with its own secrets to derive matching state. The service lacks those secret inputs. Message keys also change over time. A backend engineer might ask, "The connection's already encrypted. Why seal the message too?" That connection ends at the service. The server can read what arrives unless the message stays separately sealed for the recipient. Ah, the server needs the address on the outside, not the words inside. Routing a message doesn't require reading its content.

4:08 Think of separate locked mailboxes. The courier can put a sealed packet in the right slot without holding the key that opens the packet. And adding a master key at the depot would solve the courier's inconvenience by abandoning the privacy requirement. Well, it would certainly simplify the diagram. It would also be a different product promise. End-to-end encryption keeps that opening ability at the endpoints. Yeah, the courier can route sealed packets, but preparing them still belongs to the sender.

4:38 More devices means more packets to prepare. Now use those sessions for a concrete send. Let's say my phone sends to your phone and laptop. That's two destinations so far. Plus your own linked tablet. That 3rd copy puts the sent message on your other device, without asking my phone to send it back. Oh, right. I was counting the person I'm sending to, not all the places my own sent message needs to appear. The sender encrypts the readable message, the plaintext, separately for each destination's session.

5:07 WhatsApp's security paper calls this client fanout. So one human action expands into three protected deliveries. The service gets sealed envelopes, not a readable job it can re-encrypt itself. Yep. Your sending phone already has the local message. That one doesn't need a network copy addressed back to itself. I think we'd need destination counts alongside account counts in our capacity tests. A quiet account can still have a tablet with a growing backlog. And if my phone and your tablet are asleep, those two copies wait independently.

5:38 Adding devices creates delivery work without adding people. The three copies are ready before those devices reconnect. Let's follow the waiting envelope instead of treating the whole account as online or offline. Next, give those independent deliveries storage. In our design, each envelope goes into a durable mailbox owned by the destination device, separate from its live connection. That gateway restart we worried about has a precise rule: acknowledge only after durable mailbox storage. A connection server's memory doesn't satisfy the acceptance promise.

6:10 Let's follow my laptop's envelope. The service stores it and acknowledges acceptance. My phone's copy keeps waiting; the laptop can fetch its own. When the laptop reconnects, it authenticates as that device and fetches pending envelopes. We retain them until its receipt, subject to an explicit retention policy. It receives the envelope, then the receipt gets lost. I can see the retry turning one message into two bubbles. We give the logical message a stable identity and suppress duplicates locally.

6:40 Delivery attempts can repeat without the conversation acquiring another copy of the same message. Okay, that tracks. Though receiving encrypted bytes still doesn't prove the laptop opened them successfully, and opening them doesn't prove a person looked. Those are separate events: service acceptance, device delivery, successful decryption, and human reading. Our interface reports the states we actually know. A green socket indicator proves neither delivery nor reading. You can have a perfectly healthy connection and still be waiting on that envelope.

7:10 And one device's receipt doesn't empty another device's mailbox. The account can be partly caught up, which is an ordinary state here. For debugging, we'd follow the envelope to its missing receipt rather than treat every delayed device as a dead gateway. The failure states stay distinguishable. But those mailboxes serve already linked devices. Later, your phone's back and you're linking another laptop. Where does this new one's history come from? An authorized encrypted history transfer. WhatsApp's paper describes the primary device sharing recent history through an encrypted bundle during linking; the service can relay the bundle.

7:45 Before seeing this design, I'd pictured linking as "add a key, then read the old queue." But yesterday's copies weren't addressed to this new laptop. Exactly. An already authorized endpoint deliberately shares selected history it actually holds. A new private key doesn't magically open those older copies. Now make the handoff concrete. A message arrives while the history snapshot is copying. What stops an update falling between the two paths? Let's choose our storage protocol, not claim WhatsApp's internals.

8:14 First, my phone logs history changes in the same transaction as each write, then snapshots through position 100. Position means progress in your phone's ordered history feed, not the message's identity. The new laptop already has its ordinary mailbox for direct delivery too. Let's say Meeting moved arrives during copying. My new laptop gets its direct envelope; my phone records the same message at transfer position 101. Then disconnect the new laptop. It has the stored snapshot and that live message, but hasn't applied the history update.

8:48 What's its saved progress? 100. That's its progress marker, saved with the snapshot. On reconnect, my phone can still replay retained entry 101, encrypted for this laptop. But Meeting moved is already on your new laptop from direct delivery. Replaying that retained update would make a second bubble unless... ...we merge using that message's stable identifier. It identifies the same message in both copies; the saved position records which history updates we've applied. Before that replay, Meeting moved is visible but the history marker is still 100. If your phone disappears then, what can the new laptop show?

9:25 New messages can still arrive directly, but old-history catch-up remains unfinished. Resume when my phone returns; don't label a timer expiring as completion. Keep your phone's replay buffer until the new laptop acknowledges the final selected position. Right. My phone helped the new device catch up. It didn't become a forwarding service that every new message has to pass through. Now that history handoff has added a device, senders may still have yesterday's device list. What happens during that overlap?

9:55 In our design, acceptance checks the destination set against current membership. If it's stale, the client fetches an authorized update and prepares only the missing envelopes. Give that list a version, preserve the message identity, and put limits on retries. Otherwise rapid membership changes could keep one send working forever. Yes, with a visible failure if it can't converge. The public Sesame protocol describes this class of stale-device reconciliation and partial delivery; it isn't a license for endless retries.

10:25 For instance, removing a stolen laptop feels more urgent than adding a new one. I'd want the directory change to cut it off immediately. I want that too, but we need to separate future routing from knowledge it already has. Stop serving its mailbox and stop authorizing it in new recipient sets. The deletion request can't reach backward into an unlocked laptop and remove text someone already read or copied. Nor should we promise every cached sender instantly knows the change. Signed lists still need freshness rules; WhatsApp's paper explicitly describes update and expiry behavior.

11:01 Then our removal response should say which actions completed, while clients reject obsolete authorization according to those rules. "Device removed" isn't proof of remote erasure. Correct. Independent devices make the phone-offline promise possible, but they also enlarge the set of endpoints whose membership and local security matter. Next, scale that bigger endpoint set. Our mailboxes can be partitioned by recipient device, while connection gateways scale with live sockets rather than retained bytes.

11:28 Consider one returning laptop with 1,000 pending envelopes in our test. That's one connection but substantial stored delivery work. I'd keep those resources separate in production. And sender fanout follows the destination count. Capacity tests need devices per account and offline duration, not just messages per second. Storage needs a retention limit too. An abandoned device can't accumulate encrypted messages forever; expiry needs an honest catch-up outcome rather than pretending the backlog was delivered. For operations, suppose that laptop repeatedly fetches without acknowledging.

12:00 Connections look healthy, but its oldest pending message keeps aging and delivery attempts per original message climb. We can diagnose delivery without opening the message. Routing still reveals information about endpoints, timing and sizes. Protecting content doesn't hide every piece of metadata. So we can investigate that aging backlog without collecting message content. The missing receipt gives us a starting point, not the answer to every fault. Now return to the exact complete map with those failure paths in mind.

12:28 The authorized device directory supplies destinations and verification material, not a universal decryption key. Your device creates the protected copies. Gateways move them, durable mailboxes hold them, and independently connected recipients open and acknowledge their own deliveries. The encrypted history path carries the phone's selected snapshot and retained updates. Remember Meeting moved: replay progress closes the history gap, while shared identity merges its direct copy. Device-list refresh and removal sit around those paths.

12:56 They control future participation without pretending to erase a copy that already reached an endpoint. We can trace the original laptop's message without passing through that powered-off phone, and without drawing a server-side plaintext store into the middle. So the phone can be offline because the laptop has its own authorized identity, encryption state, connection and delivery backlog. The account isn't one decryption endpoint. The costs are per-device work, explicit history transfer and changing membership.

13:25 Keep those visible, and that small chat bubble no longer hides the design. Thanks for listening to Learning Podcasts.