In this lesson · topic 2 of 7
Topic 2
Three orders, and what exactly-once means
7 min read
One crash, three orders
Step through the same crash with each order.
One crash, three orders
Step 1 / 5- steps left
- work and ack
A consumer takes one message
The consumer has the message. Two things are still to happen: the work, which changes the world, and the ack, which tells the broker to forget the message. The question is the order, and a crash can land between the two.
Ack first: at most once
Acking before the work means the broker never hands the message out twice. But if the consumer dies after the ack and before the work, nobody will ever retry it.
Ack last: at least once
Doing the work first and acking after it means a crash in between leaves the message unacknowledged. After the ack timeout the broker redelivers it, and the work happens again.
Ack last, with an idempotency key
Same order, plus a record of which keys were applied. The key is stored in the same transaction as the effect. The redelivered message finds its key and skips the effect. The delivery was repeated, the effect was not.
All three side by side
There is no free option. Delivery cannot be made both lossless and unrepeated by choosing an order alone. The way out is to accept repeats and make them harmless.
- A consumer takes one message. The consumer has the message. Two things are still to happen: the work, which changes the world, and the ack, which tells the broker to forget the message. The question is the order, and a crash can land between the two. steps left: work and ack.
- Ack first: at most once. Acking before the work means the broker never hands the message out twice. But if the consumer dies after the ack and before the work, nobody will ever retry it. lost: yes; repeated: no.
- Ack last: at least once. Doing the work first and acking after it means a crash in between leaves the message unacknowledged. After the ack timeout the broker redelivers it, and the work happens again. lost: no; repeated: yes.
- Ack last, with an idempotency key. Same order, plus a record of which keys were applied. The key is stored in the same transaction as the effect. The redelivered message finds its key and skips the effect. The delivery was repeated, the effect was not. lost: no; repeated effect: no.
- All three side by side. There is no free option. Delivery cannot be made both lossless and unrepeated by choosing an order alone. The way out is to accept repeats and make them harmless. choices: 3.
At most once
The consumer acks, then works. The broker never hands a message out twice. If anything fails after the ack, the message is gone. This fits data where an occasional loss is cheaper than a repeat: a metrics sample, a log line, a position update that the next one will replace.
At least once
The consumer works, then acks. The broker keeps the message until it hears the ack, and redelivers it when an ack timeout passes with no word. Nothing is lost. But a crash after the work and before the ack leads to a repeat.
The timeout matters in its own right. If it is too short, a consumer that is slow but alive will have its message redelivered while it is still working. Now two consumers hold the same message, and the work happens twice without any crash. If it is too long, a crashed consumer’s message sits unclaimed, and the delay is longer than it needs to be.
At least once, with an idempotency key
An operation is idempotent if doing it twice has the same effect as doing it once. Setting a value is idempotent. Adding to a counter is not.
When the operation is not idempotent by nature, you make it so with a key. Each message carries an identifier that is stable across redeliveries. The consumer keeps a store of the identifiers it has already applied. Before acting, it checks the store. If the key is there, it skips the work and just acks.
One condition matters more than the rest. The key has to be recorded in the same transaction as the effect, so that either both happen or neither does. If they can come apart, a crash between them brings the duplicate back. The lab has a switch to show this.
What “exactly once” can mean
You cannot make a message cross an unreliable system exactly once. What you can do is make its effect happen once. Say the message may be delivered several times, and arrange for the repeats to do nothing. That is sometimes called effectively-once processing.
There are two common ways to build it.
An idempotent consumer. At-least-once delivery, plus a deduplication store or a write that is naturally repeatable, such as “set this row to this value”. The original Kafka paper takes this view. It says its system guarantees at-least-once delivery, that a crash can lead to some duplicates, and that an application which cares about them should deduplicate itself, using offsets or a unique key in the message.
A transaction across the effect and the progress. The consumer writes the result and the record of how far it has read in one atomic step, in a store that supports it. If the step fails, neither is saved, and the retry starts from the old position. Stream processors that checkpoint their state use this idea. The MillWheel paper describes one that checks each record against stored deduplication data, commits its state changes atomically, and only then acknowledges the sender. It also says that side effects on external systems must be made idempotent by the application, because the framework cannot cover them.
Both are examples of the end-to-end argument. A guarantee that the application needs can only be completed at the endpoints, because only the endpoints know what “the same effect” means. A lower layer can reduce failures, but it cannot finish the job.