WhatsApp System Design
WhatsApp-scale messaging: Erlang relays and end-to-end encryption
By Ankur NayakReviewed
How WhatsApp delivers messages for billions of users with a famously small team: Erlang servers hold millions of connections each, the server stores encrypted messages only until they are delivered, and every message is end-to-end encrypted per device.
Open in Studio· edit a copy- Users
- 2B+ users (2020)
- Connections per server
- 2M+
- Server stack
- Erlang on FreeBSD
- Companion devices
- Up to 4 per phone
Requirements
Functional
- One-to-one and group messages, delivered and read receipts
- Photos, video, voice notes and documents
- Works on slow networks and while the recipient is offline
- Use the same account on a phone and up to four companion devices
Non-functional
- End-to-end encryption by default: the server cannot read messages
- Messages delivered within a second when both users are online
- Huge concurrency per server, with a small operations team
- Messages are never lost before they are delivered
In depth
A relay, not a database
Most chat services keep your history on their servers. WhatsApp is built the other way around: the server is mostly a relay. It holds each device's connection, forwards encrypted messages, and stores them only while a recipient is offline. WhatsApp's 2021 write-up on multi-device says messages are not stored on the server after they are delivered. History lives on your devices.
That choice shapes everything: storage stays small, the server never needs to read content, and the hard problems move to delivery and encryption.
What it has to do
WhatsApp passed two billion users in 2020. Its requirements look simple, one-to-one and group messages with delivery receipts, plus photos, video, voice notes and documents, but the constraints are demanding. Messages must arrive within a second when both people are online, work on slow networks and wait safely when a recipient is offline, and never be lost before delivery. Everything is end-to-end encrypted by default, so the server cannot read content, and since 2021 one account can be used on a phone and up to four companion devices. All of this has historically been run by a remarkably small operations team, which is why concurrency per server matters so much.
Millions of connections per server
WhatsApp's backend is famously written in Erlang on FreeBSD. In his 2012 Erlang Factory talk, Rick Reed described a standard server (24 logical CPUs, about 100 GB of RAM, separate network cards for user traffic and internal distribution) and the work to push it from about 200,000 connections to 2 million, peaking at 2.8 million. Almost all of the gains came from removing lock contention in the Erlang VM, the kernel and the application, and from partitioning Erlang term storage and Mnesia tables into small clusters. Fewer, bigger, heavily tuned servers meant a tiny fleet for the user count.
End-to-end encryption, per device
Every message is encrypted on the sender's phone with the Signal protocol. At registration, each device uploads its public identity key, a signed prekey and a batch of one-time prekeys, so anyone can start an encrypted session with it even while it is offline.
With multi-device, each phone and up to four companion devices has its own keys. The sender encrypts a message once per recipient device and sends N ciphertexts, an approach the whitepaper calls client fan-out. The server checks the sender's view of the device list against its own records, so a sender with a stale list is told to refresh.
Groups would be expensive that way, so they use Sender Keys: each member distributes a sender key to the group over pairwise sessions, then encrypts each group message once. The server does server-side fan-out of that single ciphertext to all participants.
Media
Photos and videos never travel inside messages. The phone encrypts the file with fresh random keys, uploads the encrypted blob to a blob store, and then sends an ordinary encrypted message containing the keys, a SHA-256 hash and a pointer. Recipients download, verify and decrypt locally.
When things fail
A relay design makes failures cheap to recover from, because the server holds so little state.
- A chat server crashes. The connections on it drop and clients reconnect to another server. Messages that weren't acknowledged are resent by the client, and anything waiting for an offline recipient is already in the offline store, so nothing is lost. Users see a short "connecting" state, and a pending message shows a single tick until it is resent.
- The media store is unavailable. Text keeps flowing, because messages never touch the media path. Photo and video uploads and downloads fail and are retried in the background; media shows a retry button while text is unaffected.
The trade-offs
- Store-and-forward instead of server-side history. Keeping encrypted messages on the server only until they are delivered keeps server storage small and means less data exists there at all. The cost is that history lives on devices: new devices need sync or backups, and server-side search is impossible. Services that store history on the server, such as Slack and Discord, make the opposite choice and get search and instant multi-device access in return.
- Client fan-out for multi-device. Encrypting each message separately for every recipient device means each device has its own keys and no shared secret sits on the server, and adding a device doesn't weaken encryption. The sender pays for it with one encryption and upload per device, and has to keep device lists current.
- Erlang on a few large servers. Running fewer, bigger machines with Erlang on FreeBSD, heavily tuned, gave millions of connections per server and a small fleet and team. It required deep, specialised tuning of the VM and the kernel.
What to take away
- Store-and-forward keeps the server simple and small
- Concurrency per server can matter more than server count
- Encrypting per device lets the server stay blind
- Separate the media path from the message path
Components
Sender's Phone
Client. Encrypts every message on the device before it leaves, using the Signal protocol.
Compose
Client. The plaintext message exists only on the device.
Recipient Devices
Auth. The phone knows (or fetches) the recipient's list of device identities and their public keys.
Encrypt per Device
Service. One pairwise Signal session per recipient device: the message is encrypted N times, once for each device.
Send Ciphertexts
Service. Ciphertexts go to the chat server over the persistent connection.
Recipient's Devices
Client. Phone plus up to four companion devices, each with its own keys.
Load Balancer
inferredLoad balancer. Spreads incoming connections across chat servers.
Building block: Load Balancer
Chat Servers
Erlang / FreeBSDRealtime gateway. Hold each client's persistent connection and relay encrypted messages, receipts and presence between users.
Why: Erlang's lightweight processes and SMP scalability let one server hold millions of mostly idle connections.
Building block: WebSockets & Persistent Connections
Session Routing
Mnesia (partitioned)inferredNoSQL database. Tracks which chat server each connected user is on so messages can be routed between servers.
Building block: Database Sharding, Consistent Hashing
Offline Message Store
NoSQL database. Holds encrypted messages for devices that are offline, and deletes them once delivered.
Why: Store-and-forward keeps server storage small and limits what exists on the server.
Building block: Replication
Key Distribution
Auth. Stores each device's public identity key, signed prekey and one-time prekeys, plus each account's list of devices.
Group Service
Service. Knows group membership and fans a sender-key–encrypted group message out to every member.
Building block: Fan-out on Write vs. Read
Media Store
Object storage. Encrypted blobs. The sender encrypts each attachment with a random key, uploads it, and sends the key and pointer inside an end-to-end encrypted message.
Building block: Object Storage, CDN
APNs / FCM
inferredExternal API. Wakes an offline phone so it reconnects and fetches waiting messages.
Request flows
Send a message to an online contact
- If needed, the sender's phone fetches the recipient's device list and public keys to set up Signal sessions.
- It encrypts the message once per recipient device and sends the ciphertexts over its open connection.
- The sender's chat server receives them; it cannot read the content.
- It looks up which chat server each recipient device is connected to.
- The ciphertexts are relayed to each online device, which decrypts locally.
- A 'delivered' receipt travels back to the sender (two grey ticks).
Recipient is offline
- The sender sends encrypted messages as usual.
- The chat server finds no live connection for the recipient's phone.
- The ciphertext is stored in the offline queue.
- A push notification wakes the recipient's phone.
- The phone reconnects, receives the waiting messages and acknowledges them; the server then deletes them.
Send a photo
- The phone encrypts the photo with a fresh random key and uploads the encrypted blob.
- It sends a normal end-to-end encrypted message containing the key, a hash and the blob's location.
- The recipient receives that message like any other.
- It downloads the blob, checks the hash and decrypts it locally.
Send a group message
- The sender encrypts the message once with its Sender Key for the group.
- The chat server receives a single ciphertext.
- The group service fans it out to every member's devices (server-side fan-out).
- Each member decrypts it with the sender key they received earlier over pairwise sessions.
Failure scenarios
What if: a chat server crashes?
Connections on that server drop and clients reconnect to another one. Messages that weren't acknowledged are resent by the client; anything waiting for an offline recipient is in the offline store, so nothing is lost.
User impact: A short 'connecting…' state; pending messages show one tick until resent.
- Client-side retry until the server acknowledges
- Offline store replicated across servers
- Reconnect with backoff
What if: media store is unavailable?
Text messages keep flowing because they don't touch the media store; photo and video uploads and downloads fail and are retried in the background.
User impact: Media shows a retry button; text is unaffected.
- Separate media path from the messaging path
- Client-side retry queues
Trade-offs
Store-and-forward vs. server-side history
Keep encrypted messages on the server only until delivered.
- Pros
- Small server storage
- Less data exists on the server
- Cons
- History lives on devices; new devices need sync or backups
- Server-side search is impossible
Alternatives: Server-stored history (Slack, Discord) enables search and instant multi-device
Client fan-out for multi-device
The sender encrypts each message separately for every recipient device.
- Pros
- Each device has its own keys; no shared secret on the server
- Adding a device doesn't weaken encryption
- Cons
- N encryptions and uploads per message
- Senders must track device lists
Erlang on large servers
Fewer, bigger machines running Erlang on FreeBSD, heavily tuned.
- Pros
- Millions of connections per server
- Small fleet and team
- Cons
- Deep, specialised tuning of the VM and kernel
Frequently asked questions
Can WhatsApp's servers read my messages?
No. Messages are end-to-end encrypted with the Signal protocol on the sender's device and only decrypted on recipients' devices. The servers relay and temporarily store ciphertext, and WhatsApp's 2021 multi-device write-up says messages are not stored on the server after they are delivered.
Why does WhatsApp use Erlang?
Erlang was built for telecom switches: it handles huge numbers of lightweight concurrent processes and scales across CPU cores. In 2012 WhatsApp reported more than 2 million concurrent connections on a single FreeBSD server running Erlang, peaking at 2.8 million.
How do WhatsApp messages reach multiple devices?
Each linked device has its own identity keys. When you send a message, your phone encrypts it separately for every device of the recipient (and your own other devices), so the server just relays N ciphertexts. This is called client-side fan-out.
How are photos sent securely?
The phone encrypts the file with a random key and uploads the encrypted blob. It then sends an ordinary end-to-end encrypted message containing that key, a hash and the blob's location, so only the recipient can decrypt the file.
Building blocks used
Sources
- Scaling to Millions of Simultaneous Connections (Erlang Factory 2012) · Rick Reed, WhatsApp
- That's 'Billion' with a 'B': Scaling to the Next Level at WhatsApp · InfoQ (Erlang Factory 2014), Rick Reed
- 1 million is so 2011 · WhatsApp Blog
- Two Billion Users: Connecting the World Privately · WhatsApp Blog
- WhatsApp Encryption Overview (technical white paper) · WhatsApp
- How WhatsApp enables multi-device capability · Engineering at Meta
- The Double Ratchet Algorithm · Signal