Try
↑ ↓ move↵ openesc close
In this lesson · topic 2 of 7
  1. 1. The ack point
  2. 2. Three orders, and what exactly-once means
  3. 3. Crash a consumer around the ack
  4. 4. Slow is not dead: duplicates without a crash
  5. 5. Retry limits and the dead-letter queue
  6. 6. Publishing without losing the event
  7. 7. Duplicates, timeouts and exactly once: three checks

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
receiveworkacktwo steps left, and the order is your choice
steps left
work and ack
  1. 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.

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.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms