Chapter 42
Private messenger
Ana wants a chat app in which not even the company that runs it can read what she says. The trunk’s server keeps everything and understands everything. What has to change?
This chapter is a first draft. The author is still checking it, and its text and numbers may change.
Ana wants to switch chat apps. She has one requirement: what she says to Ben can be read only on their own devices. Nobody else can read it, including the company that runs the app.
The trunk’s server cannot do that. Since chapter 1, the server has read every message it passes on. Even with the connection encrypted with TLS, which protects it on the way, the server itself decrypts it, stores it and understands it. This chapter starts from v3 and asks: once the server cannot read messages, which of the trunk’s decisions must change?
1. What the product needs
First, what end-to-end encryptionend-to-end encryption端到端加密A message is encrypted on the sending device and only the recipients’ devices can open it; the server passes on bytes it cannot read. Each device has its own keys, so the unit of encryption is the device, not the person.See the glossary means. Each device makes its own pair of keys. The public key goes to the server, and anyone may fetch it. The private key stays on the device and never leaves it. A message encrypted with a public key is called ciphertext: it looks like random bytes, and only the matching private key can turn it back into the original. To send to Ben’s phone, Ana’s phone fetches that phone’s public key from the server and encrypts the message with it; the server just passes on bytes it cannot decrypt. The real protocol (the Signal protocol) also keeps changing keys, which we skip here. Remember one thing only: a piece of ciphertext can be opened only by the device it was encrypted for.
There is also a simpler kind of key: the same key both encrypts and decrypts, so whoever has it can open the message. Groups and photos will use it later.
“Encryption” comes in several levels, ordered by how much the server gives up:
| Level | What the server can see | Examples |
|---|---|---|
| Transport encryption only | All content; it keeps all history | The trunk; Telegram’s ordinary (cloud) chats |
| End-to-end, the server keeps encrypted history | Who talks to whom, and when; it cannot read the content, but keeps it | Matrix (an open chat protocol); Facebook Messenger (since 2023) |
| End-to-end, deleted once delivered | Who talks to whom, and when; content stays only while in transit | WhatsApp, Signal |
| Also hide who sends and who is in a group | Who a message is for; as little as possible about who sent it | Signal (section 8) |
This branch takes the third level: only devices can read the content, and the server keeps as little as it can. Section 8 covers the fourth.
2. Which of the trunk’s decisions read content
Each of the trunk’s decisions made sense when it was made, but once the server cannot read messages, six of them no longer hold:
| The trunk’s decision | What the server must do | With end-to-end encryption |
|---|---|---|
| History kept on the server forever; new devices pull it (chapters 6, 10, 16) | Store every message | Each copy is for the devices that existed then; a new device has no key and cannot open it |
| One message pushed to every device (chapter 16) | Store one, push many | Each device has its own key, so each needs its own copy |
| The server counts unread, skipping system messages and your own (chapter 14) | Know what each message is | It cannot tell which is which |
| Notification text, and a badge from the server’s unread count (chapter 17) | Read content, count unread | Neither is possible |
| Recall, with the server checking the time limit (chapter 18) | Know which message is a recall | A recall is ciphertext too; only clients can check |
| Photos in object storageobject storage对象存储A storage service that keeps whole files by name (S3, for example); images and files go here, not into the message table. The phone gets a short-lived upload address and uploads to it directly; downloads usually go through a CDN.See the glossary; whoever has the address can see them (chapter 8) | Store files | They must be stored encrypted, with the key inside the message |
The first row is the easiest to get wrong. If the server keeps the ciphertext forever, can’t a new device pull it? No. A message Ana sent last year was encrypted separately for her phone and laptop of that time and for Ben’s two devices; the tablet she bought today had no keys then and cannot open any of those copies. With end-to-end encryption, history kept on the server is useless to a new device.
The trunk has no server-side search, but the workplace branch (chapter 39) does. A server that cannot read has nothing to index.
The next three sections change three things: how to send, what the server stores, and how history reaches a new device.
3. Sending: one copy per device
In the trunk, Ana uploads one copy and the server pushes it to every device (chapter 16). Now every device has a different key, so Ana’s phone encrypts a copy for each device:
- Ask the server which devices Ben has and their public keys, and which devices Ana has besides this phone.
- Encrypt one copy for each device, her own other devices included. (Otherwise her laptop would not see what she sent from her phone: chapter 16’s problem.)
- Hand them to the server in one go. The server puts each into that device’s inbox (next section).
In a 1:1 chat where Ana has two devices and Ben has two: Ben’s 2 copies plus 1 for Ana’s laptop make 3 copies. The trunk uploads 1.
WhatsApp’s multi-device post (2021) describes exactly this: each device has its own keys, and the sending client encrypts and transmits a message N times, to N devices. Besides the phone, up to four devices can be linked.
The device list must be right. Ana’s idea of Ben’s devices may be stale: Ben logged in on a new laptop yesterday. Signal’s server checks this. If the list does not match, it refuses the message with error code 409, described as “Incorrect set of devices supplied for recipient” (MessageController). The reply lists the missing devices and the extra ones; the client adds and removes copies and sends again.
Groups get expensive. A group of 100 people with 3 devices each is 300 devices; minus the sending one, 299 copies. Encryption adds a key number, a counter and a check code to the 200-byte message, and each copy carries its device’s address: call it 300 bytes a copy, as side trip W4 does. One message uploads 89.7 KB; on chapter 8’s weak 2 Mbit/s uplink that takes 0.36 s, where the trunk uploads 200 bytes. The trunk’s 500-member hiking group means 750 devices, 749 copies, 224.7 KB and 0.9 s.
So groups use another method: sender keyssender keys发送者密钥In an end-to-end encrypted group, each device makes a chain key of its own and first sends it to every device in the group over pairwise sessions; after that each message is encrypted once and uploaded once. The cost: when someone leaves, every device makes a new key and sends it out again.See the glossary. Each device has a key of its own for the group (the simpler kind from section 1: whoever has it can open the message). It is sent once to every device of every member, encrypted for each device as in a 1:1 chat. After that, each message is encrypted only once with it, handed to the server, and the server delivers it to every device in the group. They all hold the key already, so one copy is enough. WhatsApp’s security whitepaper works this way; whenever a member leaves, all participants clear their Sender Key and start over. 299 copies become 1. The cost is distributing and replacing keys; the details are in side trip W4: Group encryption.
A photo cannot go out 300 times. A 200 KB photo encrypted once per device is 299 × 200 KB = 59.8 MB, 239 s on the weak uplink. The whitepaper’s method: the phone makes a random key, encrypts the photo once and uploads it to storage. Then it sends an ordinary encrypted message carrying that key, the file’s address and its hash (a “fingerprint” of the content, used to check that the downloaded file was not swapped). The upload stays 200 KB, 0.8 s; only the small message is sent 299 times. Chapter 8’s “the address is the permission” problem goes away too: someone who gets the address but not the key cannot open the file.
4. Storing: an inbox per device, deleted once fetched
The trunk’s server writes each message into the conversation’s timeline and keeps it. Devices bring their seqseq序号(seq)The number of each message in a conversation, assigned in order by the message service that owns it. It sets the order, lets a client see which message is missing, and tells a returning phone which conversations it is behind on.See the glossary and ask “what comes after this?” (chapter 6). Now each device’s copy is different, so what the server holds is ciphertext for specific devices. The simplest shape:
- One inbox per device, in order of arrival.
- A device fetches when it connects. Once a copy is in its local databaselocal database本地数据库The phone’s own copy of messages, conversations and the sync cursor, usually SQLite. The screen reads only it, and everything from the network is written into it first; a cursor is committed in the same transaction as the messages it stands for.See the glossary it sends an ACKACKACK(确认)The receiver’s “got it” to the sender. In this series, the server’s ACK means the message is in the message log, not that anyone has received it.See the glossary, and the server deletes that copy.
- If a device never comes to fetch, the copy is deleted after a while anyway.
This is store and forwardstore and forward存储转发The server holds a message only until the device fetches it: deleted once fetched, and after a while if never fetched (30 days at WhatsApp). History lives on the users’ own devices, not on the server.See the glossary, not an archive. In Signal’s server code, a delete is by recipient, device and message ID (MessagesManager). Its privacy policy says the server queues end-to-end encrypted messages for delivery to devices that are temporarily offline, and that your message history is stored on your own devices. WhatsApp’s privacy policy says messages are deleted from its servers once delivered; a message that cannot be delivered is kept in encrypted form for up to 30 days, then deleted.
Among chapter 16’s other answers was “an inboxinbox信箱A per-person index of “new messages for you”, with its own per-user sequence. Write fan-out writes it, and devices pull after their position in it. Chapters 6 and 10 weigh it against asking every conversation; this series’ v2 does not use it.See the glossary per device”, rejected then: every message is written once per device, 750 writes for one message in the hiking group. Here it comes back, because those 750 copies are 750 different ciphertexts anyway. They have to be written somewhere.
How much is stored. At v3, 400 million messages a day:
- The trunk keeps all history: 400 million × 200 bytes = 80 GB a day, 29.2 TB a year, 87.6 TB with three replicas, and still growing.
- Inboxes: each message reaches 10 devices on average (the series’ assumption since chapter 1: 1:1 chats reach few, groups many), so 4 billion copies of 300 bytes, 1.2 TB, flow through the server each day. What is held at any moment = flow × how long each copy stays. Assume copies stay 1 hour on average (online devices fetch at once; offline ones wait for a push to wake them): that is one hour’s flow, 50 GB. Also assume 1% of devices never come back, so their copies stay the full 30 days: 1.2 TB × 1% × 30 = 360 GB. Together 410 GB, 1.23 TB with three replicas, and it stops growing.
After one year, the inboxes hold about 1/70 of what the trunk holds (87.6 TB ÷ 1.23 TB = 71). Notice one more thing: 88% of those 1.23 TB are copies nobody fetches. The inboxes’ size is set mostly by “when do we give up”, not by the devices that behave normally.
But writes do not go down. At v3 peak the trunk writes 13,900 messages a second, plus chapter 11’s record update for each recipient (6.67 recipients per message on average, chapter 6’s assumption), 92,700 a second at two rows each: 199,300 rows a second in all, or 10 storage nodes at chapter 29’s 20,000 rows a second each. Inboxes take in 139,000 copies a second (13,900 × 10 devices) and delete 139,000 after their ACKs, and a delete is a write too: 278,000 a second, 13.9 nodes, about 1.4 times as much. Storing less is paid for by writing more.
Inboxes also change the shape of sync. The server no longer keeps a timeline per conversation, so there is no “latest seq of each conversation” to sweep (chapter 6), and no conversation recordsconversation record会话记录One row per person per conversation: pinned, muted, the conversation’s latest seq, and later how far they have read. Written on join, leave or a settings change, and from chapter 11 on also when a message arrives (with the latest seq). Each change bumps that person’s record version. The messages are not here.See the glossary on the server (chapters 10 and 11). A device that comes back asks only: “what is in my inbox?” The phone builds the conversation list itself from the messages it receives: chapter 10’s “built by the client” answer.
5. History: moved between your own devices
With no history on the server, Ana’s new tablet gets nothing at its first login. The fix is to let her old device provide it:
- WhatsApp: as soon as a new device is linked, the primary device packs the messages of recent chats into a bundle, encrypts it and transfers it; the key goes to the new device in an end-to-end encrypted message (multi-device post).
- Signal (January 2025): when linking a new Desktop or iPad, the user can choose to bring their chats along. The phone compresses them into one archive, encrypts it with a random one-time key and sends it to the new device through the server; the last 45 days of media can come too.
With chapter 16’s numbers, Ana sends and receives about 110,000 messages a year, 22 MB of text; the old phone packs them, encrypts them once and passes them to the tablet through the server. This assumes the old device is still there and still turns on.
If every device is lost, the history is gone, unless the user turned on an encrypted backup. Since 2021 WhatsApp can encrypt backups end to end, with the key protected by the user’s password or a 64-digit key. In 2025 Signal launched secure backups, first in its Android beta, unlocked by a 64-character recovery key that only the user holds. Forget the key and the backup cannot be opened; the server cannot help with that.
6. The rest moves to the phone
Unread counts. The phone counts them (as chapter 14 said). It has every message (it fetched them all from its inbox), so counting is easy. The hard part is keeping “read up to here” the same across devices: it becomes one more encrypted message, sent to your own other devices.
Notifications. The notification carries nothing; it only wakes the app, which fetches from its inbox, decrypts and then shows the message. Signal’s server code shows this (APNSender, FcmSender). An iPhone gets a notification with mutable-content, whose text is only a placeholder, APN_Message; the app’s notification extension replaces it with the real content on the phone. An Android phone gets an FCM data message with the key newMessageAlert and an empty value. Chapter 17 described another use of the same extension: the notification carries the ciphertext, and the extension decrypts it on the phone without a trip to the inbox. Either works; Signal puts no content in the notification at all. The badge is computed on the phone too.
Recall and edit. Chapter 18’s design: a recall is one more message in the conversation and travels to every device like any other. That barely changes. A recall is an encrypted message, “delete the message with msg_id X”, sent to every device. What changes is who checks it. Chapter 18 had the server check the time limit; now the server cannot tell that a message is a recall, so only the receiving phone can check “did the sender write it, and is it within the limit?” A modified client can ignore it.
Search. Only over the local database. Chapter 16 said search on a new device is incomplete; here it is the only search there is.
Duplicates. Chapter 4’s msg_idmessage ID消息 IDA unique ID the client gives each message before sending it, unchanged on resends. The server uses it to recognise a resend.See the glossary is inside the ciphertext, where the server cannot see it. The receiving phone removes duplicates by “sender + msg_id” after decrypting; chapter 7’s unique index in the local database already does that.
Order. Chapter 5’s seq is given by the server; a server that cannot read the content cannot vouch for the order either. Telegram’s secret chats put two counters inside the ciphertext, and the two clients count for themselves: a number lower than expected is a repeat and is dropped; a higher one means something is missing in between, so the client holds the message and asks the other side to resend the missing ones; if they cannot be filled, the secret chat is ended (Sequence numbers in secret chats). It is chapter 5’s “fill the gap when a number is skipped”, moved to the phones.
7. The cost
- Lose your devices and you lose your history, unless you turned on an encrypted backup and remember its key.
- A new device starts empty. It has only what an old device gives it; without the old device at hand, it starts from today.
- Each message costs more to send. One copy per device, with sender keys in groups; device lists must always be right; about 1.4 times the writes.
- The server cannot help. No server-side search, no badge from the server, no content in notifications; moderation relies on users reporting messages and handing them over themselves.
- Recall and edit depend on clients following the rules.
- Keys must be managed. A new phone or a reinstalled app means new keys, and contacts need to see that the safety number changed (a number computed from the two people’s public keys: if they compare it in person, they know nobody swapped a key in between); and the device list must show that the user really added each device.
8. Other answers
- The server keeps encrypted history. Matrix keeps room history on the server, encrypted messages included. So that a new device can read old messages, a client can back up its decryption keys to the server, encrypted, and a new device opens them with a key only the user knows (server-side key backups). When Facebook Messenger moved to end-to-end encryption by default in 2023, it did not choose “deleted once delivered”. Its apps had always kept only a small local cache and relied on history on the server, so Meta built an encrypted storage system, Labyrinth: ciphertext stays on the server, and devices load it on demand (post).
- Two kinds of chat side by side. Telegram’s ordinary chats use transport encryption only and live on the server, readable from any device. Its “secret chats” are end-to-end encrypted, exist only on the device that started them, and are not in the cloud. Its FAQ says that restoring chat history on a new device has no elegant solution with end-to-end encryption.
- Hide even who sent it. The server must know who a message is for, but not necessarily who sent it. Signal’s sealed sender (2018) puts the sender inside the ciphertext. Its private group system (designed in 2019, the basis of its new groups since 2020) stores the member list encrypted on the server: the server can check “you are a member” without knowing who the members are.
- The trunk: transport encryption only. All six decisions from section 2 stay. Most chat products are at this level.
9. This chapter’s decision
That covers a product whose server cannot read. The next chapter is the opposite: customer service, where a message is read not only by the other person but by a second and a third agent at the company.
Comments
Comments live in GitHub Discussions and need a GitHub account.