Case study · Communication · complexity 4/5
How a chat app for one-to-one and group conversations grows from one server with long polling to millions of open connections, partitioned message storage and end-to-end encryption — with WhatsApp's, Signal's and Discord's published material as the documented examples
This is an educational reference architecture, not the design of any one company. Where we cite WhatsApp's, Signal's or Discord's own published material, it is marked as documented.
Asha sends "Running 5 min late". Ben's phone is in his pocket with the app closed. The server stores the message in Ben's inbox and tells Asha it was sent: one tick.
Architecture evolution
One app server and one PostgreSQL database. Sending a message is a POST that inserts a row. Each open app keeps a long-poll request waiting; the server answers it when a message arrives for that user, and the app immediately asks again.
Components
What breaks next
Every online user ties up a waiting request, timeouts produce empty answers all day, and a closed app hears nothing until someone opens it. One server is also one point of failure for everyone.
Your phone
score page open
App server
scores.example
Score feed
at the ground
Swipe the diagram sideways to see every column →
Where the time goes
30738 ms total
Your phone shows 120/2. In the next 30 seconds there is a four, a wicket and a six. Compare three ways of keeping the screen up to date. In the time bar, Screen out of date is time a new score sat on the server while your screen still showed the old one. Timings are an illustrative model.
Illustrative timings. Real numbers vary with distance, network quality and server load.
Three messaging companies have published unusual detail about how their systems work. WhatsApp publishes an encryption white paper, Signal publishes its protocol specifications, and Discord's engineering blog describes how it holds connections, fans out messages and stores trillions of them.
Three documented sources, three hard problems
Sending a chat message looks like inserting a row and reading it back. The hard part is the other end: the recipient's phone may be asleep, offline, on a flaky train, or one of several devices, and it must still get every message once, in order, readable only by the people in the conversation.
Six ways a naive chat app lets a message down
A new chat app can run on one server and one PostgreSQL database, with long polling standing in for push: each open app keeps one request waiting, and the server answers it as soon as a message arrives. This is our educational design, not any company's documented system.
Stage 1: one server, one waiting request per app
The bottleneck moves from one server's waiting requests, to finding the one gateway that holds a user's connection and storing a fast-growing history, and finally to hot conversations, huge groups and keeping message content private. The traffic numbers are illustrative.
The bottleneck moves at each stage
In Stage 2 every open app keeps one WebSocket to a gateway server, and a session registry records which gateway holds each device. To deliver a message, the chat service looks up the recipient's devices and sends the message to the right gateways.
Stage 2: gateways hold sockets, a registry finds them
The server always stores a message before it tries to deliver it, and keeps a per-device inbox of what each device has not yet acknowledged. Receipts travel back the same way: "sent" when the server has stored it, "delivered" when a recipient device has acknowledged it, "read" when the person has seen it.
Three ticks, three different facts
Two ids on every message solve two different problems. A client message id, created by the sending app, lets the server recognize a retry; a per-conversation sequence number, assigned by the server, gives every member the same order and reveals gaps.
Two ids, two jobs
When the app is closed, the phone's operating system cuts its connections, so only Apple's or Google's push service can reach it. The server sends a to wake the app, and the app then fetches its messages over its own connection.
Push wakes the app; the app fetches the message
Discord has published how its message storage grew: from one MongoDB replica set, to Apache Cassandra partitioned by channel and time bucket, to ScyllaDB behind Rust data services. The partition key, the problems and the numbers below are Discord's own.
Discord's partition: one channel, one 10-day bucket
A group message is stored once, but it must reach every member's devices. Small groups copy it to each member's inbox when it is sent; very large groups store it once and push it only to members who are online, letting everyone else fetch it when they open the group.
Small groups copy; large groups store once
"Online", "typing" and "last seen" are , and presence is : it expires unless refreshed by heartbeats, it may be wrong for a few seconds, and losing it is harmless. Treating it like messages, stored durably and sent to everyone, would cost more than the messages themselves.
Presence expires unless it is refreshed
With , only the devices in a conversation hold the keys. WhatsApp's white paper says the Signal Protocol is the basis of its encryption and is designed to prevent third parties and WhatsApp from having plaintext access to messages or calls.
Keys stay on devices; servers carry ciphertext
Messaging moved from polling over HTTP to a standard two-way socket in 2011, from servers that could read everything to the Signal protocol in the mid-2010s, and from one database to partitioned stores holding trillions of messages.
From long polling to trillions of encrypted messages
At global scale, a thin connection tier holds sockets, a stateless routing and delivery tier decides where each message goes, and the state lives in a partitioned message store, per-device inboxes, an expiring presence store and a key server. This is a typical educational design, not a diagram of any company's system.
Connections, routing and state, kept apart
Every choice in a chat system trades cost, freshness or privacy: copying messages makes reads cheap and writes expensive, strict order needs one sequencer per conversation, precise presence costs traffic, and end-to-end encryption removes the server from features it used to provide.
Four dials for a chat system
On a bad day, a gateway dies and its clients reconnect at once, acknowledgments are lost, a phone stays offline for weeks, one group goes viral and push tokens go stale. Each needs a response designed before it happens.
A bad day for a chat system, five ways
Three simulations show the mechanisms this case study depends on: pushing over an open connection instead of polling, keeping one conversation's messages in order with a key, and a single hot key overloading one shard. All rates and timings are illustrative models.
Your phone
score page open
App server
scores.example
Score feed
at the ground
Swipe the diagram sideways to see every column →
Where the time goes
30738 ms total
Your phone shows 120/2. In the next 30 seconds there is a four, a wicket and a six. Compare three ways of keeping the screen up to date. In the time bar, Screen out of date is time a new score sat on the server while your screen still showed the old one. Timings are an illustrative model.
Illustrative timings. Real numbers vary with distance, network quality and server load.
Press Start: once an order has produced all three events it is highlighted here and in the logs.
Produced
24/s
Consumed
24/s
max 40/s now
Total lag
0 events
Out of order
0
events after a later one of the same order
Hottest partition
—
even share would be 25%
Busiest consumer
C1 · 0%
each finishes 10 events/s
Every order produces three events — created, paid, shipped — and the shipping service must see them in that order. 24 events per second go to 4 partitions, each read by its own consumer (10 events/s each). One example order is highlighted. Rates are illustrative.
Illustrative model: rates are tiny so single events can be watched (real consumers handle thousands per second), arrivals are random but seeded, and the rebalance pause is shortened.
Swipe the diagram sideways to see every tier →
Busiest shard
0%
Errors
0%
p95 latency
0 ms
Served
0/s
Survive the celebrity
With the celebrity getting 40% of traffic, keep the busiest shard under 70% and errors under 0.5% for 15 seconds.
A social app shards posts by a hash of the user id across 4 shards, each good for about 2,500 queries per second. One celebrity account gets 40% of all reads — and all its rows live on one shard. All numbers are an illustrative model.
Illustrative model: rates, queues and latency follow simple queueing rules, not a production benchmark.
The connection counts, Discord's storage history and fan-out numbers, WhatsApp's encryption design and receipts, Signal's protocols and the push platforms' behavior are documented in the sources. The three stages, the registry and inbox design, the sequence-number scheme, presence handling and all traffic numbers are our educational assumptions.
Where the evidence stops