Topic 116 / Core
Idempotent consumers and deduplication
Chapter 7.3 · Event-Driven Architecture and Delivery Guarantees
What and why
Topic 115 established at-least-once as the practical default, which means every consumer will eventually see some message more than once — the consumer, not the broker, is where an "exactly-once effect" actually gets built. An idempotent consumer is one whose processing of the same message twice produces the same end state as processing it once; deduplication is the mechanism of recognizing "I have already handled this one" before applying its effect again. Builds on: → 115.
Diagram
| Route | When it applies | Mechanism |
|---|---|---|
| Natural idempotency | Operation is set-to-value (e.g. "set status = shipped") | None needed — redoing it changes nothing |
| Deduplication key | Operation is not naturally idempotent (e.g. "increment balance by $10") | Unique key stored in the same transaction as the effect |
How it works
- Check first whether the operation is already naturally idempotent — setting a field to an absolute value, upserting by a stable key. If so, redelivery is harmless by construction.
- If not (increments, "send one email," "charge once"), carry a unique ID identifying this specific logical operation — not the broker's own delivery count, which resets on redelivery.
- Before applying the effect, check a durable store — a unique constraint on that ID in the same database the effect writes to, or a dedicated dedup table — for whether it has already been processed.
- Apply the effect and record the ID as processed in the same transaction (→ 70) — splitting this into two steps reopens the exact race being closed.
- On a retried delivery of the same ID, the check short-circuits: skip the effect, acknowledge, move on.
Practical example
(illustrative). A payment-capture consumer receives
"CapturePayment, payment_id=P1" twice because the broker redelivered after a slow ack. With
a unique constraint on payment_id in the capture-ledger table, the first delivery inserts
a capture row; the second delivery's insert fails the uniqueness check, the consumer
confirms a capture row for P1 already exists, and acknowledges without charging twice — one
charge, two deliveries.
When to use it
- Use whenever the semantic is at-least-once (the default, → 115) and the effect is not already naturally idempotent — most business-meaningful consumers.
- Skip the extra machinery only when the operation is provably naturally idempotent.
- Failure scenarios: checking for a duplicate and applying the effect as two separate, non-transactional steps — a crash between them either double-applies or permanently skips; a dedup key scoped to in-process memory that doesn't survive a restart or a different instance picking up the redelivery after a rebalance (→ 108).
- Simpler alternative: redesign the operation to be naturally idempotent (upsert instead of increment) wherever the business logic allows — no key, no extra table, nothing to get wrong.
Practice
(pattern — implement minimal, spot the misuse; 20–30 min, lab Postgres + Kafka). Implement a consumer that applies "credit account by $X" using a dedup table keyed by event ID, inside the same transaction as the credit; feed it the same event three times and confirm the balance changes once. Then, given a version that checks the dedup key and applies the credit as two separate transactions, reproduce a double-credit by simulating a crash between them, and fix it. Done when the three-times-delivered event changes the balance exactly once, and the two-transaction bug is both reproduced and fixed.
Shared lab setupKey takeaway
Idempotency is what turns at-least-once into exactly-once in effect, and it must live in the same transaction as the effect it protects. Done when you can: implement a transactional dedup check and explain why checking and applying can't be two separate steps. Used next in → 117 (poison messages are what's left after idempotent retries still fail), → 118 (outbox consumers rely on exactly this).
References
Designing Data-Intensive Applications — see references.md.
Source directory