Ordering, acks and delivery guarantees
We'll cover the following
Visual summary

Download imageFree to share, please link back to this lesson.
The problem: messages that arrive out of order or twice
Alice sends "dinner at 7?" and a moment later Bob replies "no, 8". On Carol's phone the two appear the other way round: "no, 8" first, then "dinner at 7?". The conversation now makes no sense. Or a different failure: Alice's message reaches the server, but the confirmation back to her phone is lost. Her phone sees no answer and sends it again. Does Carol now see "dinner at 7?" twice?
Any system that moves messages (chat, notifications, event streams) has to answer four questions: in what order do they appear, when does the sender see them as saved, when do they count as delivered, and what happens on a retry. Chat is the running example here, but the same answers apply to any per-account or per-partition stream.
Why timestamps cannot give you an order
The obvious idea is to stamp each message with the time and sort. It fails for three reasons:
- Device clocks can be wrong or set by the user. Server clocks drift by milliseconds between machines.
- Two messages can arrive within the same millisecond on different servers, so the timestamps tie or invert.
- A timestamp cannot tell you that a message is missing. If 10:00:01 and 10:00:03 arrived, you cannot know whether something in between never showed up.
Here is the inversion with two servers whose clocks disagree. Server A runs 200 ms fast:
real time event server stamps it
10:00:00.000 Alice: "dinner at 7?" -> A 10:00:00.200 (A is 200 ms fast)
10:00:00.100 Bob: "no, 8" -> B 10:00:00.100 (B is correct)
Sort by timestamp: "no, 8" (.100) comes before "dinner at 7?" (.200). Wrong.A sequence number from one owner
Do not ask clocks for an order. Instead give each unit that needs a shared order (one chat, one account, one partition) a single owner. Hash the unit's id to one owning shard. That shard is the only thing that assigns a growing seq (1, 2, 3...) for the unit. No clock is involved, so nothing can disagree.
Because the numbers are consecutive, a receiver can spot a gap. A gap is detectable, which a timestamp never is:
server sends seq 1, seq 2, seq 3, seq 5 (seq 4 was lost)
receiver has 1, 2, 3, then sees 5
receiver asks "I am missing 4"
server resends seq 4
receiver shows 1, 2, 3, 4, 5 in orderThis scales because load is spread across thousands of units, and each unit sends few messages per second, so one counter per unit is trivial. Kafka gives the same thing: a partition keyed by the unit id is an ordered log with offsets (see the queues lesson).
When does the sender see "saved"
The owner replicates the message to followers and acks the sender only after a majority has it (2 of 3). Take one leader L and two followers F1 and F2:
t0 Alice -> L message arrives, L writes seq 7 to its disk
t1 L -> F1, F2 replicate seq 7
t2 F1 acks L now has 2 of 3 copies (itself + F1)
t3 L -> Alice "saved" (one tick). F2 is slow, we do not wait.
t4 L crashes
t5 F1 or F2 wins the election; any winner needs a majority of votes,
and every majority of 3 includes at least one node with seq 7 (here F1)A crash right after the ack cannot lose the message: every majority overlaps with the set that holds it. Acking after only the leader's write is the dangerous version, because a crash would leave the sender with a tick for a message that exists nowhere.
Why not wait for all replicas? One dead or slow replica would block every write. A majority tolerates one failure out of three. The trade is the same one as in the cap lesson: this is CP for that one unit. During a leader election that unit pauses for a moment, while every other unit stays available.
Retries and the idempotency key
Back to the lost ack. The network can drop it, so the client retries, and the server must not store a second copy. The client generates an idempotency key (client_msg_id) once per message and reuses it on every attempt. The owner checks the key before assigning a seq:
try 1 client sends {client_msg_id: abc, text: "dinner at 7?"}
owner: key abc is new -> assigns seq 12, stores it, acks seq 12
ack is lost on the way back
try 2 client sends the same {client_msg_id: abc}
owner: key abc already has seq 12 -> no new seq, no new copy,
returns the same ack "seq 12"
result Carol sees the message once, at position 12On a retry the owner returns the same seq and the same ack as the first attempt, not an error. At-least-once delivery plus dedupe on the key gives effectively-once behaviour. The queues lesson covers why at-least-once is the safe default and why consumers must be idempotent; this is the same idea applied at the sender.
"Delivered" means the receiver acked
The order of steps matters: store first, ack the sender second, deliver third. Never skip the store because the receiver looks online. A socket can look alive while the phone is already off the network, and a message that was only handed to that socket is gone.
Handing a message to a socket is not delivery. Delivery is the receiver device sending back an ack. The server deletes its stored copy only after that ack (or when the retention TTL expires). If the receiver was offline, the copy is still there.
On reconnect the receiver sends a cursor per unit and the server replays what it missed. Each device keeps its own cursor, because your phone and laptop may be at different points:
phone reconnects, sends cursor {chat 9: last seq = 41}
server has seq 42, 43, 44 stored for chat 9
server replays 42, 43, 44 in order
phone acks 44, cursor moves to 44, server may now delete 42..44Check yourselfB's socket looks online but the phone has lost network. A already saw one tick. What protects the message?
It was stored and replicated to a majority before the tick, and it is only deleted after B's device acks. When B reconnects it sends its cursor and the server replays what it missed.
Check yourselfWhy not order chat messages by server arrival timestamp?
Server clocks drift, timestamps can tie within a millisecond, and a timestamp cannot reveal a missing message. Use a per-conversation sequence number from a single owner instead.
Check yourselfA client sends a message, the owner stores it as seq 12, and the ack is lost. The client retries. What must the owner do, and what breaks if it does not?
It must look up the idempotency key, find it already maps to seq 12, and return that same seq and ack without storing again. Without the key, the retry would get a new seq (13) and the recipient would see the message twice.
Summary: say it in the interview
- "One owner per conversation, a monotonic sequence number, majority ack. Store before deliver, delete on the device ack, replay from a cursor."