CHAT & PRESENCE
Who is online, and did my message arrive exactly once, in order?
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.
// 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));// server assigns the seq once, when it appends; a resend carries the same
// client msgId, so it gets the seq it was given the first time
function append(conv, msgId, text) {
if (seen[msgId]) return seen[msgId];
return (seen[msgId] = { conv, seq: ++nextSeq[conv], text });
}
// receiver: dedup and reorder on seq
function deliver(msg, inbox) {
const last = inbox.lastSeq[msg.conv] ?? 0;
if (msg.seq <= last) return; // duplicate
if (msg.seq > last + 1) return buffer(msg); // gap: wait
show(msg); drainBufferFrom(msg.seq + 1);
} 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.
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
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.
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).
“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.