Key takeaways
- ✓A live connection forgets: when the server holding 1,000 clients was killed, reconnecting and asking for nothing lost 195 of 11,999 notifications, for 183 of those users, because a channel delivers only to whoever is listening at that moment.
- ✓Resending after the last acknowledgement lost none, but it showed 1,540 notifications twice to 805 users: acks went out in batches of ten, and before the kill each user had received only about 1.5 notifications, so no batch had completed and the server resent everything.
- ✓Numbering each notification per user and keeping the last 200 in an inbox lost 0 and repeated 0 of 11,999, at a price: the replayed notifications were slower: the slowest one in a hundred (the p99) took 750 ms after the kill, against 3.7 ms for the ones that arrived without a replay.
- ✓The client asks for everything after the highest number it has with no gap before it, and the server starts listening before it replays, so a notification sent during the replay arrives twice rather than not at all; the client drops the second copy.
- ✓Presence is a timer that each heartbeat resets: a client that vanished silently stayed online for a median of 2.4 s with a heartbeat every second, 12.3 s every five seconds and 37.4 s every 15 seconds, each inside its predicted window of 2–3, 10–15 and 30–45 seconds, while a clean close showed offline within one quarter-second check.
- ✓Faster presence costs the same at every user: at one million users, a 2–3 second window means 1,000,000 heartbeats a second, and a 30–45 second window means 66,667, which is arithmetic from the heartbeat rule, not something the lab measured.
A server with a thousand clients on it was killed in the middle of a run. Reconnecting lost 195 notifications. Asking the server to resend everything after the last acknowledgement lost none, but showed 1,540 notifications twice. Numbering every notification lost none and showed none twice. This chapter takes the three reconnect strategies apart, then asks how a server knows a phone has gone, and what that knowledge costs.
The examples come from Lab 15 (Real-Time Delivery and Fan-out) of the system design labs: script 03_restart.py for the server kill, with the client logic in delivery.py, and script 05_presence.py for the online dot. Both run on the same laptop setup as the earlier chapters, an Apple M4 Max with Docker Desktop and 16 virtual CPUs. The servers, the HAProxy load balancer and Redis all ran in Docker, and a separate client container simulated the users. Each configuration is one recorded run. The README puts the whole lab at about 35 minutes.
What a reconnect loses
A WebSocket is a connection that stays open, so the server can push a message down it the moment it arrives. The server gets each notification through Redis, a fast shared store that also carries a publish-subscribe channel: one channel per user, named u:<user> in the lab. Whichever server holds the user's connection listens on that channel and passes the message on.
A channel reaches only whoever is listening right now. A client that is reconnecting is not listening, so what is published in the gap is gone. Think of a live radio broadcast: step out of the room, and what played meanwhile is gone unless the station keeps recordings.
The first test kills a server and does nothing else. In the lab, 3,000 clients were spread across three servers behind HAProxy, a load balancer, and a publisher sent 300 notifications a second for 40 seconds. Fifteen seconds in, one server (ws2) was killed. It held a third of the clients, 1,000 of them, and they reconnected through HAProxy, which picks a server for each.
- Reconnect and ask for nothing: 195 of 11,999 notifications were lost, for 183 users.
Those 1,000 users received about 100 notifications a second between them. 195 lost is therefore about two seconds' worth each, on average. That is an estimate. The time away was not measured.
Sequence numbers: ask for what you missed
The second strategy is the one most apps reach for first: remember the last message you acknowledged, and ask for everything after it. In the lab, the client acknowledges every ten messages, so the acknowledgement is a bookmark that moves in steps of ten.
- Resend after the last ack: none lost, but 1,540 notifications were shown twice, to 805 users.
Why so many repeats? Before the kill, each user had received only about 1.5 notifications, so almost none had reached the tenth message that triggers an acknowledgement. Nobody had moved the bookmark, and the server resent everything the users had already seen. The client had no number to tell a repeat from a new message, so it showed them all. This is how the test was set up. With busier users, the repeats would be at most the last nine, because the bookmark would have moved most of the way.
The third strategy gives every message a number. The server assigns each user's notifications the next sequence number, one, two, three, and so on, and keeps the last 200 in an inbox: a per-user list in Redis (inbox:<user>). The client remembers the numbers it has. On reconnect, it says what it has, not what it hasn't: "I have everything up to 37." The client sends the highest number it holds with no gap before it, so if it holds 1, 2, 3 and 5, it says 3, and 4 comes again. The client drops repeats by number.
- Sequence numbers with an inbox: 0 lost and 0 shown twice, out of 11,999.
The client reconnects and asks for nothing. Whatever was published while it was away is gone.
Published 11,999 notifications over 40 s; 11,804 arrived. Median delivery 1.2 ms in every mode. One run per mode.
The price is time. Messages that were replayed from the inbox had waited while the client reconnected. Their p99 latency after the kill was 750 ms, against 3.7 ms for messages that arrived without a replay. The fast figure is not a fair comparison, because the messages that would have been slow in the first strategy were the ones lost.
Replay order, and the window it covers
The order of operations in the server matters. On connect, the server must start listening on the user's channel first, then replay the inbox after the client's number, and only then forward live messages. If it replayed first and started listening afterwards, a notification published in between would be missed. Listening first means the same notification may arrive twice, and the client drops the second copy.
The replay window is the inbox. The lab keeps the last 200 notifications per user, and a client that has fallen further behind than that cannot be replayed. It must be told to resynchronize: reload the full state, the way a station with no archive would tell a listener to fetch the current schedule instead. The lab does not implement this step. Its test clients never got close to the limit: over the whole 40-second run each user received about four notifications, far below 200. The README lists the resync path as an exercise, and any real system needs it, because a phone that is off for a day will hit the limit.
Presence: knowing who is online
The green dot needs the server to know which clients are still there. A client that closes its connection cleanly says goodbye, and the server can remove it at once. A phone that loses signal never says goodbye. Its connection just goes quiet.
So the lab uses heartbeats. Each connected client sends a small message on its connection every few seconds. The server handles each one by writing a key for that user, presence:<user>, in Redis, with a time-to-live (TTL): a timer that deletes the key when it runs out. Each heartbeat resets the timer, and the lab sets the TTL to three heartbeat intervals.
Think of a hallway light on a timer: each motion turns it on for three minutes, and it goes off after the last motion when nobody moves again. A clean close is someone turning the light off by hand. A silent phone is someone walking off without touching the switch.
The timing follows from the rule. A phone vanishes at a random moment between two heartbeats, so its timer has somewhere between two and three intervals left. A closed connection is removed immediately; a silent one is seen as online until the timer runs out.
| Heartbeat | Heartbeats/s, 2,000 users | At 1M users (arithmetic) |
|---|---|---|
| every 1 s | 2,000 | 1,000,000 |
| every 5 s | 400 | 200,000 |
| every 15 s | 133 | 66,667 |
The workflow and the results
Reconnects. The 03_restart.py script starts 3,000 WebSocket clients through HAProxy, starts publishing 300 notifications a second, waits 15 seconds, and then kills ws2. It runs three times, once per client mode, and counts what each client received against what was published. Each user's notifications are numbered, so the script can match them exactly and count losses and repeats.
| Client mode | Published | Delivered | Lost | Shown twice | Users affected | p50 | p99 after the kill |
|---|---|---|---|---|---|---|---|
| Reconnect, ask for nothing | 11,999 | 11,804 | 195 | 0 | 183 | 1.2 ms | 3.7 ms |
| Resend after the last ack | 11,997 | 13,537 | 0 | 1,540 | 805 | 1.2 ms | 621 ms |
| Sequence numbers and inbox | 11,999 | 11,999 | 0 | 0 | 0 | 1.1 ms | 750 ms |
Presence. The 05_presence.py script starts 2,000 clients, each heartbeating every 1, 5 or 15 seconds, with a TTL of three intervals. At 20 seconds in, 10% of them go silent (no more heartbeats, but the connection stays open) and 10% close cleanly. An observer asks Redis who is online four times a second. The script records, for each vanished client, how long after going silent it was last seen online.
| Heartbeat every | TTL | Predicted window | Vanished, last seen online: median (range) | Clean close | Heartbeats/s, 2,000 users |
|---|---|---|---|---|---|
| 1 s | 3 s | 2–3 s | 2.4 s (1.9–2.9) | within one check | 2,000 |
| 5 s | 15 s | 10–15 s | 12.3 s (10.0–14.8) | within one check | 400 |
| 15 s | 45 s | 30–45 s | 37.4 s (30.0–44.8) | within one check | 133 |
Every vanished client went offline inside its window, and a clean close showed offline at the next quarter-second check. The 1 s row's shortest value, 1.9 s, is just under two seconds because the observer checks every 0.25 s and records the last check that still saw the client online.
What presence costs
Accuracy is paid for by everyone online. Every online user sends one heartbeat per interval, and each heartbeat is a write to Redis. Halve the detection time and you double the heartbeats, for every connected user at once.
At one million users, a 2–3 second window means one million heartbeats a second. A 30–45 second window means about 67,000. This is arithmetic from the rule (users divided by interval), and the lab did not load a million clients to measure it. It is also why many apps show "last seen" rather than a live dot: the cost of a fast dot falls on every user, including the ones not looking at it.
Lab 15 does not test fan-out of presence to friends. Pushing every change to every friend is the same fan-out problem as the feed, which the next episode of this track (part 36) takes up.
Caveats
- One run per mode, and the numbers are small. The three reconnect runs and the presence runs are single recordings. The published count differs slightly between runs (11,997 in the resend run, 11,999 in the others), because the publisher's random sequence differs.
- Time away was not measured. The "about two seconds away" figure is an estimate from the loss rate. HAProxy's least-connections balancing can also send reconnects to the dead server until its health check marks it down, which adds to the time away.
- The p99 "after the kill" figures are approximate. They mix all 3,000 users. The kill time is taken on the host's clock and the message times come from the client container's clock, so read them as orders of magnitude, not exact milliseconds.
- The resend result depends on the setup. The lab's README says the repeats are "up to nine" messages per client, which is true in general. In this run, each user had about 1.5 messages before the kill, so the server resent everything, and the 1,540 is a feature of the test, not of the strategy.
- No resync was tested. Inbox overflow never happened (200 per user, about four per user over the run), so the "too far behind" path is untested.
- Presence was tested on one laptop with 2,000 clients. The observer's quarter-second check sets the resolution, and the clean-close times in the results are negative because they are measured against the close time, which sits just after the observer's last online check. The heartbeat rates at a million users are arithmetic, not a load test.
- Not covered: multi-region routing, TLS, the cost of heartbeats on Redis under load, and presence fan-out to friends. The lab lists fan-out to friends as an exercise.
The checklist
- Assume a live connection forgets. Keep an inbox per user for the last N messages, and let a reconnecting client replay from it.
- Number every message per user. The client says the highest number it has with no gap before it, and drops any number it has already shown.
- Listen first, then replay. A notification sent during the replay is then delivered twice, never zero times, and the client drops the second copy.
- Size the inbox from how long a phone can be away, and have an answer for a client that is older than the inbox: resynchronize from full state, never skip silently.
- Treat presence as a timer, and price its heartbeats. The detection time you pick is paid for by every connected user, every interval.
Quiz
1.A client drops its connection when its server dies, reconnects a moment later and asks for nothing. What happens to the notifications published while it was away?
2.Resending after the last acknowledgement lost nothing, yet 1,540 notifications were shown twice. What explains the repeats?
3.A client has numbers 1, 2, 3 and 5 when it reconnects. What should it ask for, and why?
Check your understanding
Try each question before opening its answer.
Why did "resend after the last ack" show 1,540 notifications twice, when the lab's acknowledgements were sent every 10 messages?
Because before the kill, each user had received only about 1.5 notifications, so almost no client had reached the tenth message that triggers an acknowledgement. The server had no bookmark and resent everything. The client had no sequence number to recognize a repeat, so it showed them again. With busier users, the repeats would have been at most the last nine.
The server starts listening before it replays the inbox. What goes wrong if it replays first?
A notification published between the end of the replay and the start of listening is missed, because it was neither in the replay nor sent live. Listening first can deliver a notification twice, once in the replay and once live, but the client drops the second copy, so nothing is lost.
A client has notifications 1, 2, 3 and 5. What does it ask for, and what does the server send back?
It asks for everything after 3, the highest number with no gap before it. The server replays 4 and 5, and the client drops the 5 it already has. Asking after 5 would never produce 4.
A phone's signal drops, and its presence timer has three heartbeats of TTL. Why did the 1-second heartbeat show it online for between 1.9 and 2.9 seconds, not exactly 3?
The timer is reset by each heartbeat, so when the phone goes silent it has between two and three intervals left, depending on where in the interval it went quiet. The observer checks every 0.25 seconds, so the recorded last-online time is the last check that still saw the phone online, which is also quantized.
Presence at 2 to 3 seconds costs a million heartbeats a second at a million users. Why is that the wrong place to put the cost in most apps?
Because the cost falls on every connected user, including the ones who never look at the dot. Most people only need to know whether a friend is online when they open a conversation. Fetching presence for the friends on screen costs one request, and fast updates can be pushed only for the conversation that is open.