Skip to main content

Chat App: Groups and Encryption Deep Dives

Editorial

How does end-to-end encryption change the servers?

WhatsApp defines end-to-end encryption as "communications that remain encrypted from a device controlled by the sender to one controlled by the recipient, where no third parties, not even WhatsApp or our parent company Meta, can access the content in between". It uses the Signal protocol. For the design, three of its properties matter more than its mathematics: every device has its own keys, a session can start while the other device is offline, and every message has its own key.

The Keys of a Device

When the app is installed, the device makes three kinds of key pairs:

  • an identity key, for the life of the installation;
  • a signed prekey, signed with the identity key and replaced from time to time;
  • a batch of one-time prekeys, each used for a single session.

It uploads the public halves to the key directory. The private halves never leave the device.

Starting a Session With an Offline Device

Alice's phone writes to Bob's laptop for the first time, and the laptop is off:

  1. The phone asks the key directory for the laptop's identity key, signed prekey and one one-time prekey. The directory hands that one-time prekey out once and deletes it.
  2. The phone makes a fresh key pair of its own and combines four Diffie-Hellman agreements between its keys and the laptop's (three, if no one-time prekey was left) into a shared secret, from which it derives the session's first keys.
  3. It can encrypt and send at once. Its first messages carry the public keys the laptop needs to compute the same secret when it comes online.

Signal's specification of this step, X3DH, states the problem it solves: one user "is offline but has published some information to a server", and the other "wants to use that information to send encrypted data". The key directory is that server. It only ever holds public keys.

A session with a device that is offline: public keys wait on the server; every message gets its own key. 1: once, when it was installed, Bob's laptop uploaded to the key directory its identity key, its signed prekey and a batch of one-time prekeys, each handed out once. 2: while the laptop is offline, Alice's phone fetches its bundle; the directory hands out one one-time prekey and then deletes it. 3: the phone combines 4 Diffie-Hellman agreements into one shared secret, and from it root and chain keys. 4: a new key for every message: the chain moves forward from chain 0 to chain 1 to chain 2, each giving a key, key 1, key 2, key 3, that encrypts one envelope and is used once, then deleted. The server sees from alice · 1, to bob · 2, when and how big; not the text.

A New Key for Every Message

Inside a session each message is encrypted with its own message key (AES-256 in CBC mode, with HMAC-SHA256 to authenticate it). The keys come from a chain that moves forward with every message and gets new randomness from a fresh Diffie-Hellman agreement at every round trip: the Double Ratchet. A message key, once used, is deleted, and later keys cannot be worked back to earlier ones.

Three consequences reach the servers, and we checked each with Signal's own library:

  • The same text is a different ciphertext for every device. Alice's phone encrypted "see you at 7" for Bob's phone and for Bob's laptop: two different envelopes. Each device decrypted its own, and the laptop could not decrypt the phone's.
  • Order does not matter. Three messages decrypted in the order 3, 1, 2. WhatsApp's paper: messages "can arrive delayed, out of order, or can be lost entirely without any problems".
  • Each message decrypts once. The second attempt was refused (DuplicatedMessage). This is why a device stores a message and its session state in one transaction, and drops a repeat by its id before decrypting (Delivery Deep Dives).

Signal's library also showed a size worth knowing. A 40-byte text became a 137-byte envelope once the session was running. The first message to a new device was 1,792 bytes, because Signal's session setup now includes a post-quantum key exchange (PQXDH). WhatsApp's encryption paper of February 2026 describes the classic setup.

What the Server Still Sees

The servers route by account and device, so they know who writes to whom, when, and how much: the metadata. They do not know the text, what a photo shows, or who is replying to what. Between the device and the gateway sits a second, separate layer of encryption: WhatsApp's Noise Pipes, which hide even the metadata from anyone watching the network, and with which "the server only stores a client's public authentication key".

What It Changes

  • Search runs on the device. There is nothing for a server to index.
  • History lives on the devices. A new linked device gets its history from the user's phone, encrypted; the server never had it.
  • Abuse is fought without reading messages. WhatsApp bans "over 8 million accounts per month, 75% of them without a recent user report": its systems spot "accounts engaging in abnormal behavior". Users report what they received: their own device has it in the clear, and "other users … may also choose to report to us your interactions and your messages with them". And the app itself limits what spreads: a message goes to at most five chats at a time, and one that was "forwarded many times" to one chat at a time. WhatsApp says the first limit reduced forwarded messages by over 25% and the second the highly forwarded ones by over 70%.

How does a message reach every device of a person?

A person's phone and up to four linked devices each connect on their own, each with its own identity key. "The WhatsApp server maintains a mapping between each person's account and all their device identities."

A Server That Cannot Add a Device

If the server alone decided which devices an account has, it could add one of its own and read along. The phone prevents that: it signs the list of linked devices with its identity key, every linked device carries a signature from the phone (and the phone one from it), and a sender checks those signatures before it encrypts for a device. If a check fails, the sender "will not send any messages to that device". The security code that two people can compare covers all of their devices.

What Client Fan-out Costs

A message between two people with five devices each is encrypted nine times on the sending phone (the recipient's five devices and the sender's four others), sent as nine envelopes in one frame, and stored as nine mailbox rows on two servers. At about 200 bytes an envelope, that is 1.8 KB for a short text: cheap, and paid on the sender's own connection.

The check that the envelopes cover exactly the account's devices is what makes a newly linked device receive everything sent after it was linked. We ran it: a send for Bob's two devices was stored; Bob linked a third; the next send for two devices was refused with "device list changed" and the list 1, 2, 3; the send for three was stored. A retry of the first send, now with three envelopes, was still answered "already stored". Signal's server does the same, with HTTP 409 for missing or extra devices and 410 for devices that were reinstalled since.

A New Device

A linked device starts with an empty mailbox and receives every message sent after it was linked. Its history comes from the phone: "the primary device encrypts a bundle of the messages from recent chats and transfers them to the newly linked device". Two rules keep stale devices from piling up mail: linked devices "log out if your phone is unused for over 14 days", and are disconnected "after 30 days of inactivity". With the device, its mailbox goes.

Receipts and Read State

The second tick appears when one of the recipient's devices has the message: "delivered to the recipient's phone or any of their linked devices". When Bob reads on his laptop, the laptop sends a "read" receipt to Alice's devices and the same news to Bob's own phone, which then marks the chat read too. Settings such as a muted chat or a starred message travel between a person's devices the same way, encrypted.

How does a message reach a group of 1,024?

A WhatsApp group holds up to 1,024 members. With five devices each, one message has up to 5,119 destinations. Encrypting it 5,119 times on a phone would be slow and heavy; the Signal protocol's sender keys make it once.

Sender Keys

The first time a member writes to a group, their device makes a sender key: a random 32-byte chain key and a signing key pair. It sends the sender key to every other member device over their pairwise sessions, like any 1:1 message. From then on each message is encrypted once:

  1. The device derives the next message key from its chain key and moves the chain forward.
  2. It encrypts the message with that key (AES-256-CBC) and signs the ciphertext with its signing key.
  3. It sends that one ciphertext to the server, "which does server-side fan-out to all group participants".

We ran it with Signal's library: one ciphertext, decrypted by three member devices.

Sender keys: one ciphertext per message. The key travels once per member device; each message is encrypted once. 1, once, the key: Alice's phone sends her sender key over each pairwise session, one envelope per member device, the first time she writes to the group and after a change. 2, every message: it is encrypted once, and the server copies the same ciphertext to every member device. 3, someone leaves (three stayed, one left): everyone clears their sender key, and each member's next message sends a fresh one to those who stayed; the one who left cannot read on. A full group: 1,024 members with five devices each, up to 5,119 member devices. Run with Signal's library: one ciphertext, read by three member devices.

Membership changes the keys. "Whenever a group member leaves, all group participants clear their Sender Key and start over": each member's next message sends a fresh sender key to the remaining devices, so the one who left cannot read on. Someone who joins receives each member's sender key as it is at that moment; the chain only moves forward, so the messages before they joined stay closed to them.

Fan-out Through a Log

The group service appends each group message to a Kafka topic keyed by group id, with acks=all and min.insync.replicas=2: the sender's tick means at least two brokers have it. The producer is idempotent, so a retried append is not written twice: the broker "deduplicates messages using a sequence number". A fan-out worker per partition reads the group's messages in order and writes each into the mailboxes of every member device. If a worker dies mid-way, the next one starts from the last committed position and writes some envelopes again; deliver() answers those "already stored", member by member.

We measured a full group: one message to 1,024 members with five devices each is 5,119 mailbox rows and 722 KB of envelopes. Written one member per transaction, each waiting for its own flush, it took about 4 seconds on the laptop; written in one transaction, about 100 ms. The worker groups its writes by mailbox server, one transaction each.

One copy or many. Every row above carries its own copy of the 141-byte ciphertext. Signal's server stores a message for many recipients once and gives each recipient a view of it, deleted as that recipient receives it. In PostgreSQL a mailbox row took 134 bytes without its ciphertext and 282 with it, so storing the ciphertext once would save about half of a full group's mailbox bytes for a short text, and more for a long one.

Receipts in a Group

Every member device that stores the message sends a "delivered" receipt to the sender's devices, and later a "read" one; a device sends one receipt for many message ids at once. The sender's phone keeps the set of members that confirmed, not a count: a receipt that arrives twice changes nothing. The second tick appears "when everyone has received your message", and two blue ticks when everyone has read it.

Why Not Let Large Groups Pull

Some designs let members of large groups fetch messages from a shared store instead of receiving copies. With a cap of 1,024 members the copies are bounded, every member gets the message the moment it is fanned out, and a device reads one mailbox instead of polling every group it is in. Discord, where "a server with hundreds of thousands of people" is ordinary, keeps messages per channel in its database and members fetch them when they open the channel: a different design for a different product.

How do online, typing and last seen work?

"Online", "typing…" and "last seen today at 09:41" look like one feature. They are three kinds of data with three lifetimes.

Online Is an Open App

WhatsApp's definition is precise: "If a user is online, they have WhatsApp open on their device and are connected to the Internet." A phone in a pocket may hold a connection for incoming messages and still not be online. So each device tells its gateway when the app comes to the foreground and when it leaves ({"type": "active", "on": true}), and the gateway passes that, and every disconnect, to the presence service. A person is online while at least one of their devices is active.

Last Seen Is Written When the Last Device Leaves

When a person's last active device goes to the background or disconnects, or its registry entry expires because its gateway died, the presence service writes last seen to a durable key-value store. It cannot be a key that expires: we set presence:bob with a time-to-live, and once it ran out, Bob's last-seen time was gone with it, exactly when someone wanted to read it.

Pushed to the People Watching

Presence is sent only to devices that have the person's chat open. Opening a chat sends {"type": "watch", "user": …}; the presence service adds that device to the person's watchers and pushes them every change; closing the chat or dropping the connection ends the watch. "Typing…" takes the same path, as WhatsApp's help page puts it: "People online in a chat with you can see when you're typing." It is never stored: an event that reaches no watcher is dropped.

Presence goes to the people watching: online means the app is open; last seen is written when it closes. Bob's phone and laptop report when the app is active to the presence service, which keeps who is watching Bob: alice · 1. It pushes online and typing… to Alice's phone, which has Bob's chat open and shows Bob typing. Carol and Dan do not have the chat open: nothing is sent to them. When Bob's last device has left, the service writes his last seen, bob → 09:41, to a store where it is kept. First the privacy rule: Everyone, My contacts, My contacts except…, or Nobody. Typing is never stored: an event that reaches no watcher is dropped.

Broadcasting every change to every contact costs one push per online contact for every change. For one person with 200 contacts, a third of them connected (as 32% were at WhatsApp in 2014), that is about 64 pushes each time they open the app and again each time they leave it. If everyone were like that person, our peak of about 990,000 logins a second alone, with as many departures, would mean over a hundred million pushes a second, almost none of them to someone looking. Pushing to watchers costs a push only where a screen shows it.

Privacy settings come first. A person chooses who sees their last seen and online, "Everyone, My contacts, My contacts except…, or Nobody", and the presence service checks that before it adds a watcher. The rule is mutual: "If you don't share your last seen, you can't see other contacts' last seen."

How do photos and videos travel?

Media is where the bytes are: 146 Gb/s out of WhatsApp's media servers on Christmas Eve 2013, against about 5 Gb/s of text at our peak. It travels outside the chat connection, encrypted the same way.

Encrypt, Upload, Send the Key

WhatsApp's encryption paper gives the steps:

  1. The sending device makes a random 32-byte AES-256 key and a random 32-byte HMAC-SHA256 key for this one file.
  2. It encrypts the file with AES-256 in CBC mode and appends a MAC of the ciphertext.
  3. It uploads the encrypted file to a blob store.
  4. It sends an ordinary encrypted message holding the two keys, a SHA-256 hash of the encrypted file and a pointer to it.
  5. Each receiving device downloads the encrypted file, checks the hash and the MAC, and decrypts it.
A photo travels encrypted, outside the chat: a fresh key per file; the ciphertext goes up once, the key goes in the message. 1: Alice's phone makes fresh AES-256 and HMAC keys and encrypts the photo. 2: it uploads the ciphertext through the media service to the blob store, behind a CDN. 3: it sends a message through the mailboxes; inside, encrypted: the AES and HMAC keys, the SHA-256 of the blob and a pointer to the blob. 4: Bob's phone gets the ciphertext from the CDN, checks the SHA-256 and the MAC, and decrypts. The blob store and the CDN see random bytes; every recipient gets the same. A forward is a new message with the same keys and pointer: nothing is uploaded again. New Year's Eve 2013: one photo was downloaded 32 million times.

The blob store and the CDN in front of it see random bytes. Every recipient downloads the same bytes, so the CDN can cache them like any file. The message also carries a small thumbnail, as WhatsApp's did in 2014, so the chat shows the photo before it is downloaded; inside the message, the thumbnail is encrypted too.

Forwarding Without Uploading Again

A forwarded photo is a new message carrying the same keys and pointer: the file is not uploaded again. WhatsApp keeps forwarded media "temporarily in encrypted form on our servers to aid in more efficient delivery of additional forwards". A file that goes viral is downloaded many times and stored once; on New Year's Eve 2013 one photo was downloaded 32 million times.

Uploads

Media is uploaded in parts that can be resumed, which matters on mobile networks, and the blob store deletes files after 30 days, the same limit as a message that waits.

How big is the fleet?

What WhatsApp Ran

WhatsApp's 2014 talk listed its hardware: about 550 servers plus standby gear, of which about 150 were chat servers holding about a million phones each and about 250 served media; over 11,000 cores; FreeBSD and Erlang, with WhatsApp's own patches. The team: "Small (~10 on Erlang)". In 2015, Wired counted about 50 engineers for 900 million users.

The numbers come from engineering, not from one language. In 2012 WhatsApp raised FreeBSD's limits to 2.4 million sockets and 3 million files to fit over 2 million connections on one server; it cut its Erlang processes per connection from two to one; and it watched one gauge above all, the length of every process's message queue, alerting when a backlog passed a threshold. Any stack that holds connections faces the same questions: how much memory one idle connection takes, and how many the operating system allows.

Our Fleet

PartSized byFor the peak
Gatewaysconnections, a million each, a zone of three to spare~950
Registry126 bytes an entry in Redis, measured~80 GB, twice for a replica
Mailbox serversenvelopes a second, measured on the drives you buy3.1 million envelopes a second; a laptop managed about 8,400 with every flush and 110,000 without
Mailbox storageLittle's law: deliveries a second × average wait34 GB for a one-minute average, 49 TB for a one-day average
Group loggroup messages a second; one partition holds many groupspartitions by throughput, replicated three times
Mediabytes out~630 Gb/s on a peak day, mostly from the CDN

The laptop numbers are a floor for one machine, not a plan: production drives flush faster, and a memory tier in front removes most writes. What they show is how to size the store: measure envelopes a second with the durability you promise, then divide.