Skip to content

Scenario: chat and presence

Store every message until each device says it has it, and announce presence only when it changes.

Predict first

Before you read: predict

During ECON-201, 400 candidates' screens show whether proctor Mira is online, and their help button is on only while she is. Her console sends the presence service a heartbeat every 15 s from 09:00:00. At 10:30:07 her laptop loses its network for good. The exam ends at 11:00:00.

Which design turns every help button off by 10:31:00, and sends the screens the fewest presence messages over the exam?

Pick one

The 30-second version

Chat has two deep dives. Delivery: the server numbers messages, stores each in a per-device inbox before pushing, and deletes it only when that device acks; heartbeats find dead sockets and carry the latest number. Presence: a key refreshed by heartbeats, expiring after the longest gap between them, announced only when it changes; last seen is written on disconnect.

Store every message until each device says it has it, and announce presence only when it changes.

How it works

Step 1 of 13

Chat's two deep dives: delivery (order, acks, offline sync) and presence (who is online, and how many screens hear about it).

Analogy: A safari lodge's radio room and its game-drive vehicles

Vehicles spread over the reserve, and each guide radios the lodge on a schedule.

In the lodge's radio room In chat and presence
each guide radios "all well" every 20 minutes heartbeats (steps 8, 10)
a vehicle is posted "no contact" only once a call is well overdue, since calls drift an expiry longer than the longest gap (step 10)
the radio room rings the lodge manager when a vehicle's status changes, not after every call publish on change (step 11)
messages for a guide wait on the radio-room board until she reads each one back the inbox until the ack (steps 3, 5)
the radio room numbers each guide's messages and reads the latest number at every call; a guide who is behind asks for a repeat numbers on pings (step 9)
the board shows when each vehicle last called in before going out of range last seen on disconnect (step 13)

Breaks down when: a vehicle whose radio dies can still drive back to the lodge, while a dead socket can never tell the server anything (step 7).

Contrasting case: a postcard with no number and no signature: if it is lost, nobody ever knows (steps 3–5).

In real terms: keep every message until each device has stored it, and tell watchers only what changed.

Examples, run for real

4.1 ExamHall EXH-3900: help buttons that stayed on

ExamHall's presence service marked a proctor offline when her WebSocket's close event fired. In one hall, her laptop's battery died at 14:12:34, and 120 candidates' help buttons stayed on until the exam ended.

4.1 before: offline on the close event · battery dead at 14:12:34 · help buttons still on at the exam's end (15:00:00): 120 of 120 · presence messages 120
4.1 MR !4200: key expires 30 s after the last heartbeat (14:12:30) · help buttons off at 14:13:00: 120 of 120 · presence messages 240

MR !4200 keeps a key that expires 30 s after the last heartbeat and tells the screens only when it appears or expires (steps 7, 10, 11).

4.2 One online dot, many watchers

A streaming creator goes live for two hours; 50,000 followers' apps show her online dot, and her app sends a heartbeat every 30 s.

4.2 two hours, a heartbeat every 30 s, 50,000 followers · forward each heartbeat: 240 heartbeats x 50,000 = 12,000,000 messages · publish changes only: online, then offline = 100,000 messages

Forwarding heartbeats multiplies them by the watchers; publishing changes sends two messages per follower for the whole stream (step 11).

4.3 A weekend offline: a buffer forgets, an inbox does not

A support group chat gets 300 messages while one agent's phone is off for the weekend. Its socket service keeps each room's newest 50 for replay (the prerequisite's buffer).

4.3 phone last saw 1200 · 300 messages while off (1201–1500) · newest-50 buffer holds 1451–1500: needs 1201, gone, so replay 0 and reload the room
4.3 inbox for this phone: entries before connecting 300 · delivered 300 in number order (1201–1500) · entries after the acks 0

The buffer cannot cover the gap, so the phone would have to reload the room; its own inbox holds all 300 and empties as the phone acks (steps 3, 5, 6).

Try it

Answer first, then check. Nothing is saved.

Question 1 of 6

A payments company's fraud desk shows 300 analysts whether each of 12 on-call engineers is online. Each engineer's app sends a heartbeat every 20 s, and each heartbeat takes 0 to 8 s to arrive. An engineer's presence key expires a fixed time after her last heartbeat arrives, and every change goes to all 300 analysts.

Which expiry shows no engineer offline while her app is running, over an 8-hour shift, and greys her soonest once her app stops?

Pick one

Question 2 of 6

A video service's watch-party messaging keeps an inbox of undelivered messages for each user account. The server writes each message to the recipient's inbox, pushes it to each of her connected devices, and deletes the inbox entry when a device acks it; a device acks a message once it has stored it. Priya's phone is connected and her laptop is shut. Messages 1 and 2 reach her phone, which acks both. Her phone's connection then dies just as message 3 is pushed to it. Later she opens her laptop, which connects and reads her inbox.

Which messages does the laptop show?

Pick one

Question 3 of 6

A ticket marketplace's messaging app shows buyers whether a seller is online, and greys her dot as soon as its server closes her WebSocket. Her network can die silently, even while nothing is being sent to her.

Which server setting greys her dot within 30 s of the death, with the fewest pings an hour per connection?

Pick one

Question 4 of 6

An online store shows 200 shoppers whether its support agent Ana is online. Her phone sends a heartbeat every 15 s from 09:00:00, and her laptop every 15 s from 09:00:05. She closes the app on her phone at 09:30:07 and on her laptop at 10:00:00; each close reaches the server at once.

Which design never shows her offline while either device is connected, shows her offline within 5 s of her last close, and sends the shoppers the fewest presence messages between 09:00 and 10:30?

Pick one

Question 5 of 6

A payments app's chat stores each message in the recipient's inbox, then pushes it to her phone; the push can be lost. Each message to a user takes the next number of a counter kept for that user. At 14:00:03 message 41 for Lina is stored and its push is lost; nothing else is sent to her in the next hour. Her phone holds messages up to 40, and its connection stays open.

Which design shows her message 41 by 14:00:20, with the fewest extra requests from her phone in the hour from 14:00:00?

Pick one

Question 6 of 6

ExamHall shows the exam office each invigilator's "last seen" time. Invigilator Omar's phone and tablet each send a heartbeat every 15 s from 13:30:00, each to a different chat server. His phone disconnects at 16:05:10 and his tablet at 16:07:40; each server learns of its own device's disconnect, and the tablet's server reaches the store first.

Which design shows 16:07:40 with the fewest writes to the store?

Pick one

Go deeper

Pro tips
  1. Size the heartbeat budget out loud before defending one. When: the moment you add heartbeats to a chat design (step 8). Verify:
    tip 200M connected users ÷ one heartbeat every 10 s = 20,000,000 ping/pong exchanges a second · at every 30 s: 6,666,667
    Status: RAN.
Source

quote-check.mjs finds every passage below verbatim in its saved text. The row names exactly these two sources. Valid as of 2026-10-13.

  • Hello Interview, Design WhatsApp: https://www.hellointerview.com/learn/system-design/problem-breakdowns/whatsapp (hellointerview-whatsapp.txt). The row's first source. Quoted: "Users should be able to receive messages sent while they are not online (up to 30 days)."; "we write to the Inbox and Message tables before publishing to Pub/Sub"; "Redis Pub/Sub is "at most once""; "By forcing clients to ack, we can know for certain that the message has been delivered all the way to the client."; "The Chat Server will then delete the message from the Inbox table."; "We'll need to update our Inbox table to be per-client rather than per-user."; "TCP keepalives can take minutes to detect a dead connection, which is far too slow for a chat app."; "This only detects failures when we're actively trying to send messages."; "if your heartbeat interval is 10s and timeout is 5s, you'll detect any dead connection within 15 seconds."; "with 200M connected users and a 10-second heartbeat interval, that's 20M ping/pong exchanges per second"; "When the server sends a heartbeat ping, include the user's current sequence number."; "Gap detection only works when you do receive a message."; "This creates massive write amplification."; "only update if the new timestamp is greater than the existing one".
  • IETF, RFC 6455: The WebSocket Protocol, §5.5.2 and §7.1.5: https://www.rfc-editor.org/rfc/rfc6455.html (rfc6455.txt). The row's second source. Quoted: "A Ping frame may serve either as a keepalive or as a means to verify that the remote endpoint is still responsive." and "If The WebSocket Connection is Closed and no Close control frame was received by the endpoint (such as could occur if the underlying transport connection is lost), The WebSocket Connection Close Code is considered to be 1006." Supports steps 7–8 and the opening question.

Hello Interview's "200M connected users" and "10-second heartbeat interval" are the tip's inputs.

Checked against the tools and docs as of 2026-10-13.