CHAT & PRESENCE

Who is online, and did my message arrive exactly once, in order?

L318 MINSYSTEM DESIGN
PREDICT

Alice sends "lunch?". The server stores it and pushes it to Bob, then Alice's connection drops before the ack reaches her. She reconnects and resends. Bob's app shows messages in arrival order. What does Bob see?

A chat client sends over a connection that can drop at any moment. When it drops, the client does not know which messages the server stored: a lost message and a lost ack look identical. So it resends everything unacked, and the transport becomes at-least-once. Arrival order stops matching send order too: a new message can go out before the retry timer resends an old one.

The fix lives at the edges. Each message gets a per-conversation sequence number, assigned once. Here Alice is the only sender, so her client can number her messages. With more than one sender the server assigns the seq when it stores the message, and a client message id lets it spot a resend and reuse the seq it already gave. The receiver shows the next number only, buffers anything past a gap, and drops any number it has already shown. Presence has the same problem in time: a late heartbeat looks like a user who left.

▶ SIMULATION
ALICE
conn 1 UP
outbox (unacked) —
idle
SERVER LOG (stored copies)
—
uplink — · acks —
pushes to Bob — · lost in drop 0
BOB'S SCREEN (arrival order)
—
—
✓ each message shown once, in send order per conversation✓ Bob never shows offline while his heartbeats still arrive
SCHEDULE
01[ALICE]Bob shows messages in arrival order
02[ALICE]ALICE: send #1 "hi"
03[SERVER]SERVER: receive the oldest message on the uplink; store, push to Bob, ack Alice
04[ALICE]ALICE: receive acks; drop acked messages from the outbox
05[ALICE]ALICE: send #2 "lunch?"
06[SERVER]SERVER: receive the oldest message on the uplink; store, push to Bob, ack Alice
07[ALICE]ALICE: send #3 "12:30 at Rosa's"
08[BOB]BOB: receive pushes
09[NET]NET: Alice's connection drops: everything queued on it is lost
10[ALICE]ALICE: reconnect (new connection)
11[ALICE]ALICE: send #4 "see you there"
12[SERVER]SERVER: receive the oldest message on the uplink; store, push to Bob, ack Alice
13[ALICE]ALICE: receive acks; drop acked messages from the outbox
14[ALICE]ALICE: retry timer fires: resend every unacked message in the outbox
15[SERVER]SERVER: receive the oldest message on the uplink; store, push to Bob, ack Alice
16[SERVER]SERVER: receive the oldest message on the uplink; store, push to Bob, ack Alice
17[ALICE]ALICE: receive acks; drop acked messages from the outbox
18[BOB]BOB: receive pushes
19[NET]report
Start of naive: drop + resend scenario. 19 steps.
// client: resend unacked after reconnect, on a timer
socket.on('reconnect', () => setTimeout(() => outbox.forEach(send), RETRY_MS));

// receiver: show whatever arrives
socket.on('message', (m) => screen.push(m));

Watch Bob’s screen and the first invariant. In NAIVE: DROP + RESEND the drop loses the ack for #2 and the message #3, so Bob sees #2 twice and #3 after #4. FIX: SEQ + DEDUP runs the identical drop: #4 waits in the buffer, the second #2 is dropped, and the screen reads 1, 2, 3, 4. The two PRESENCE scenarios replay the same slow heartbeats against a 6 s timeout and a 15 s grace.

BREAK IT NOT YET

NAIVE Bob, a calm network: every message arrives once and in order. Drop Alice's connection at the step where it does damage: Bob sees a message twice or out of order.

GOAL: Bob sees a duplicate or an out-of-order message

FIX IT NOT YET

Write deliver(msg, inbox), called once for every copy that arrives. msg is { conv, seq, text }; seq starts at 1 in each conversation. Append to inbox.shown so that each conversation shows each seq exactly once, in seq order, never skipping a gap. inbox.lastSeq and inbox.buffer start as empty objects for you to use.

Mental model:

the network gives you at-least-once, any order, and no amount of care in the transport changes that. Order and uniqueness are properties you build at the edges: the sender numbers, the receiver reorders and dedups. Presence is the same problem in time. Silence is ambiguous, so you choose how long to wait, and every second of grace is a second of showing a ghost as online.

IN PRODUCTION

WhatsApp, Slack, Discord, Messenger and any app with a socket and a green dot: per-conversation sequence numbers or server-assigned message ids, client-side dedup on reconnect, catch-up sync from the last seen seq, and heartbeat presence with a grace window (often longer on mobile).

IN INTERVIEWS

“Design a chat system with online indicators.” On the whiteboard: clients hold a WebSocket to a gateway; gateways publish to a conversation log. Then say what breaks: the socket drops, the client resends unacked messages, so delivery is at-least-once. Draw the fix: the server assigns a seq per conversation when it appends (the client sends a message id so the append is idempotent), and the receiver shows seq == last + 1, buffers past gaps, drops seq <= last, and on reconnect asks for everything after last. For presence, draw heartbeats into a TTL store with a grace of two or three intervals, explain the flap it prevents and the detection delay it costs, and mention fanning presence out only to users who have the conversation open.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: NEWS FEED: FAN-OUT →