Delivery Semantics at Scale
Every asynchronous arrow on the board carries three questions the interviewer asks sooner or later: can this message arrive twice, can it be lost, can it arrive out of order? A broker can promise at-most-once or at-least-once delivery and nothing more, because between doing the work and saying so there is always a moment to crash in.
"Exactly once" is not a way a message is delivered. It is an effect you build from at-least-once delivery and a consumer that makes the second copy harmless. The strong answer names the guarantee on each arrow, the place the duplicate is caught, and what the order costs.
The Gap Between Work and Acknowledgement
A consumer does two things with a message: the work, and the acknowledgement that tells the broker the work is done. Do the work, then crash before the acknowledgement, and the broker hands the message out again: a duplicate. Acknowledge, then crash before the work, and the message is gone: a loss. No order of the two steps closes the gap between them.
The producer has the same gap. A send times out, the producer sends again, and the broker had stored the first copy after all. Kafka's producer numbers its own retries by default, so the broker drops a retry it already holds; an event the application itself sends again is a new message, and that resend still makes a second copy. At scale the gap opens with no crash at all. A consumer group rebalances, and a partition's unacknowledged messages go to a new reader. A slow worker's lease runs out, and a second worker takes a message the first is still working on.
Why no protocol can close that gap is the two generals problem, which Computer Science Deep Dive works through in its chapter on reliable communication. Here it is a fact to design around.
At-Most-Once and At-Least-Once
The order of the two steps picks the guarantee. Acknowledge first, and each message is delivered at most once. That fits a message the next one replaces: a location report every few seconds, a typing indicator. A lost report is overwritten a moment later, and a duplicate one changes nothing, so both prices are close to zero. The cost of at-most-once is silent loss, so it never carries what a user paid for or asked for.
Work first, and each message is delivered at least once. That is the default for anything that matters, and its cost is that duplicates are normal traffic, so every consumer is built for them. The trigger between the two is the price of a loss against the price of a duplicate. A lost payment instruction costs a customer their order, and a duplicate one charges them twice: the loss is the worse price, so the arrow is at-least-once and the duplicate is made harmless.
Exactly Once Is an Effect
A duplicate can be made harmless in three ways, each with its condition. The first is an operation that is idempotent by nature: set a status, or put a value by its key, and the second copy writes what the first already wrote. Adding one to a counter is not idempotent, and neither is appending a row.
The second is a dedup record: the message's ID is written in the same transaction as the effect, so a second copy finds the ID and stops. The third keeps the read position with the result: the consumer commits its offset in the same database transaction as its output, so a restart resumes from what the database says, not from what the broker remembers.
Inside one log system, transactions can tie read positions to writes. Kafka's exactly-once processing commits a consumer's offset in the same transaction as the output it wrote to Kafka, and Kafka's own documentation says that exactly once to any other destination generally needs that system's cooperation. The promise ends at the system's edge.
A text message sent or a card charged is outside every transaction you hold. There the answer is the provider's idempotency key, or a duplicate the product accepts, and the notification and payment problems later in this book design both. One service's idempotency keys and idempotent jobs are Backend Deep Dive's subject, in its chapters on failure and on background work.
Dedup Across a Fleet
A dedup ID only works if both copies carry the same one, so the ID comes from the origin. An ID given where the event is born, such as a click ID made when the click happens, catches a producer's resend and a broker's redelivery alike. An ID the broker gives on arrival misses the resend, because the resend arrives as a new message with a new ID. Making IDs without a coordination point is the last lesson of Chapter 5, and ad-click aggregation in Chapter 21 is built on this.
The record has to last as long as a duplicate can arrive. Assume 5,000 messages a second and a one-day window. A day is about 100,000 seconds, so the window holds 500 million IDs. At 16 bytes each, the size of a UUID, that is 8 GB before overhead. That is small against the 100 GB of memory in one cache node, so size is not the problem.
Placement is. A check in a cache and a write somewhere else are two steps, and the gap from the first section opens between them: a crash, or a second consumer holding the same message, lets the duplicate through. So the check lives with the effect, in the same transaction, and a cache in front of it is only a fast first filter.
Order per Key, and What Global Order Costs
The key picks the partition, so one account's events stay in order while other accounts move in parallel. Global order means one partition and one reader. Assume one consumer applies 2,000 messages a second. Global order caps the whole stream at 2,000 a second. Fifty partitions keyed by account carry 100,000, as long as no single account is hot, which is the subject of the hot partitions lesson in Chapter 3.
Per-key order costs twice. A message that keeps failing, a poison message, blocks its partition, and every key behind it waits; parking it on a side queue keeps the partition moving but breaks that key's order. And a log that hashes keys over the partition count moves keys when partitions are added, so for a while a key's old messages and its new ones sit in different partitions. Where a reader must detect a gap, a sequence number per key does it, as the chat problem in Chapter 18 does for each conversation.
At-most-once acknowledges before the work, so a crash loses the message and never repeats it. Use it when the next message replaces this one and a loss costs nothing a user notices.
At-least-once acknowledges after the work, so a crash repeats the message and never loses it. Use it whenever a loss costs money or trust, with a consumer that makes the duplicate harmless.
How Interviewers Probe This
This is where the course's method shows most plainly. The guarantee is a requirement agreed in Scope, where you ask a question such as "Can a notification ever arrive twice?" It is written on the arrow in First Design, and it changes only when a probe forces it. You might say: "This arrow is at-least-once. The consumer writes the click ID in the same transaction as the count, so a redelivered click changes nothing."
The most common push is "The consumer writes to the database and crashes before it acknowledges. What now?" A good answer says the message comes back, names the dedup ID or the idempotent write that makes it harmless, and puts the check in the same transaction as the write.
If you said exactly once, expect "Exactly once where?" A good answer draws the boundary. Inside one system, a transaction holds it. At a provider or a user's inbox, it is at-least-once plus an idempotency key, or a duplicate the product accepts.
Then, often, "Do these events need to arrive in order?" A good answer names the key the order matters for and partitions by it, prices global order in throughput, 2,000 a second against 100,000, and says what a poison message does to its partition.
- "The broker supports exactly once, so the consumer can ignore duplicates." The promise ends at the broker's edge, and a database write or a provider call lies outside it.
- "Duplicates are rare enough to ignore." One rebalance redelivers every message not yet acknowledged, and in a billed count each one is money.
- "Checking a dedup cache before the work makes it exactly once." The check and the effect are two steps, and a crash or a second consumer between them lets the duplicate through.
- "A log keeps messages in order." Only inside one partition. Global order is one partition read by one consumer, at that consumer's speed, and adding partitions can move a key.
- Write the guarantee on every asynchronous arrow, with the price of a loss and the price of a duplicate.
- Give every event an ID where it is born, and store it in the same transaction as its effect.
- Partition by the key whose order the product needs, and say what the hottest key does to its partition.
- Decide before the incident what a message that keeps failing does: block its key and keep the order, or park it and keep the partition moving.
Knowledge Check
A consumer acknowledges a message first and does the work second. It crashes between the two. What happens?
- The message is lost for good: the broker never sends it again
- The message comes back and its work runs a second time
- Another worker takes the message once the lease runs out
- The broker undoes the acknowledgement and delivers it again
Where should the check that drops a duplicate message live?
- In a shared cache that every consumer asks before it starts the work
- In the same transaction as the effect, as a stored record of the message ID
- In the broker, which can be configured to drop every repeat of a message
- In the producer, which can remember what it sent and never send it twice
Why is a click's dedup ID given where the click happens rather than by the broker on arrival?
- An ID made at the origin is shorter, so the dedup record is smaller
- An ID made at the origin keeps the clicks in order across partitions
- A resend reaches the broker as a new message and would get a new ID
- A broker cannot number messages, so the ID has to come from elsewhere
One consumer applies 2,000 messages a second. What does global order cost compared with fifty partitions keyed by account?
- Very little: global order runs at about 90,000 a second
- Half: 2,000 a second against 4,000 for the fifty
- Fiftyfold: 2,000 a second against about 100,000
- Per-account order, which fifty partitions cannot keep
A topic keyed by account goes from 50 to 80 partitions. What happens to per-account order?
- Nothing changes, because a key always maps to the same partition
- Some accounts move partition, so their old and new messages can interleave
- Every message is moved to its new partition, so order is kept exactly
- Order is lost for every account, since partitions are never ordered
You got correct