cloudpatterns.appClassroomKatasWhiteboardBlog
Z3 · Messaging & Events

Idempotent consumer

Idempotent consumer

Gold

Since a message can arrive twice, the consumer makes the second time harmless. The easiest way is work that is naturally safe to repeat — setting a status rather than adding to a count. Otherwise every message carries a stable key for the operation, and the consumer keeps a record of the keys it has handled, written in the same transaction as the work, so a repeat is spotted and skipped. Side effects outside that transaction, like sending a push or charging a card, need the key passed on to the provider. Keys are kept for longer than the broker could still redeliver.

Use when
Consuming from any at-least-once queue or topic — the default for most brokers · A repeat would do harm — a second charge, a second order, a second push to a user · Several workers share one queue, so two copies can be handled at the same moment
Avoid when
The work is already naturally idempotent, such as an upsert by id — a dedup table only adds cost · An occasional repeat is cheaper than running the dedup store
Trade-offs
Natural idempotency costs nothing to run, but not every action can be phrased as a set or an upsert · A dedup record catches repeats, but needs a store, a transaction around the work, and a cleanup policy · A longer key window catches later repeats, but the store grows and every new message is checked against it · Broker deduplication removes producer resends cheaply, but only within its window and never consumer redeliveries
Requires
Delivery guarantees: at most, at least, exactly once
3 objectives · 7 misconceptions
  • explain Explain how a stable idempotency key and a record of handled keys, written together with the work in one transaction, turn at-least-once delivery into effectively-once processing — and why checking first and recording afterwards leaves a gap.
  • choose Choose between making the work naturally idempotent and keeping a dedup record, and spot the side effects — a push, a charge — that also need the key passed to the outside service.
  • predict Predict which repeats a deduplication window misses — a late redelivery, a dead-letter replay, a producer resend after the keys expired — and size the window from how long the broker can redeliver.
  • Not true: Checking "have I seen this id?" and then doing the work is enough, even when several workers share the queue.
  • Not true: Recording a message's id after the work is done is as safe as recording it in the same transaction.
  • Not true: Turning on the broker's duplicate detection means the consumer no longer has to handle repeats.
  • Not true: The id the broker attaches to each delivery is a good deduplication key.
  • Not true: Every consumer needs its own table of processed ids.
  • Not true: Keeping processed ids for a few minutes catches every duplicate.
  • Not true: Once the consumer is idempotent, the outside services it calls are safe from repeats too.

Stories here

Neighborhood

On the pathChapter 12 · Deliver once

Progress is saved on this device