Quick revision
The short answer
A deduplication table records processed message ids in the same transaction as the side effect; it works for any operation but needs storage and cleanup. Natural idempotency uses operations that give the same result when repeated, such as setting a status or upserting by key. Version checks apply an event only when its version is newer than the stored one, which also handles reordering. External side effects such as emails and payment calls need idempotency keys the downstream system accepts, and we choose per operation, because increments and sends are never idempotent on their own.
What to remember
In this answer, we compared four ways to make consumers idempotent. Deduplication tables record processed ids in the same transaction as the side effect and fit increments, at the cost of storage and cleanup. Natural idempotency, such as setting a status or upserting by key, fits state changes with no extra table. Version checks apply only newer versions and also handle reordering for full-state events. External calls such as payments and emails need idempotency keys that the downstream system accepts. We choose per operation, and increments and sends always need extra help.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A deduplication table records processed message ids in the same transaction as the side effect; it works for any operation but needs storage and cleanup. Natural idempotency uses operations that give the same result when repeated, such as setting a status or upserting by key. Version checks apply an event only when its version is newer than the stored one, which also handles reordering. External side effects such as emails and payment calls need idempotency keys the downstream system accepts, and we choose per operation, because increments and sends are never idempotent on their own.
Why this question is asked
Every consumer of an at-least-once message system will see duplicates, and many will see events out of order. Treating idempotency as one technique, usually 'add a dedup table', leads to either unnecessary infrastructure or subtle bugs where the technique does not fit. Interviewers ask this comparison to see whether we can pick the right tool per operation. In this answer, we will recap what idempotent means for a consumer, then go through deduplication tables, natural idempotency, version checks and idempotency keys for external calls, each with an example where it fits, and finish with how to choose.
1A Quick Recap: What Makes a Consumer Idempotent
An operation is idempotent if doing it twice has the same effect as doing it once. 'Set the order status to SHIPPED' is idempotent: after the second time, the status is still SHIPPED. 'Add 50 to the balance' is not: after the second time, the balance is 100 higher. 'Send a confirmation email' is not either: the customer gets two emails.
A consumer is idempotent when processing the same message any number of times leaves the system as if it had been processed once. We can get there in different ways depending on what the operation is, and the rest of this answer is about those ways.
2Deduplication Tables: General, With a Cost
A deduplication table stores the id of each processed message. In the same database transaction as the side effect, we insert the id with a unique constraint; if it already exists, we skip. Because the two writes commit together, a message is either fully processed and recorded or neither.
This fits operations that are not naturally idempotent, such as increments: a balance credit, a loyalty points award, a counter. It is general, it works for any side effect in the same database. The cost is a write per message, a table that grows, a cleanup job with a retention longer than any redelivery window, and the requirement that the side effect lives in the same transactional store as the table.
3Natural Idempotency: Make the Operation Repeatable
Often we can rewrite the operation so repeating it is harmless. Instead of 'increment shipped count', we 'set status to SHIPPED for order 42'. Instead of inserting a row, we upsert by a business key, so the second delivery updates the row to the same values. Set-based updates such as 'add tag X to product 7' with a unique (product, tag) pair behave the same way.
This fits projections and state changes, which is most read model work. It needs no extra table and no cleanup. Its weakness is order: if an older AddressChanged arrives after a newer one, the upsert happily writes the old address. Natural idempotency handles duplicates but not reordering, and that is where version checks come in.
4Version Checks: Newer Wins, Old and Duplicate Are Ignored
If each event carries the version of the entity it describes, version 7 of customer 42, the consumer can store the version it last applied and apply an event only when its version is greater. A duplicate has the same version and is ignored; an old event that arrives late has a lower version and is ignored too.
| Technique | Handles | Fits | Cost |
|---|---|---|---|
| Deduplication table | duplicates | increments, any local side effect | extra write, storage, cleanup |
| Natural idempotency | duplicates | set status, upserts, set-based updates | redesign the operation |
| Version check | duplicates and reordering | full-state events per entity | producer must send versions |
| Idempotency key | duplicates downstream | emails, payments, external APIs | downstream must support keys |
This fits events that carry full state, or full state of the part they describe, such as ProductPriceChanged with the new price. It handles both duplicates and reordering with no extra table. It does not fit deltas: if events say 'add 5 to stock', skipping an older one loses information. The producer also has to supply a reliable per-entity version, typically from the aggregate's version column.
5External Side Effects Need Idempotency Keys
The techniques above work when the side effect is in our own database. A call to a payment provider or an email service is outside our transaction: we might call it, crash before recording that we did, and call it again. The fix is an idempotency key, a unique value we send with the request, which the downstream system uses to recognise a repeat and return the original result instead of acting twice. Many payment APIs accept an Idempotency-Key header for exactly this reason.
If the downstream system does not support keys, as with many email services, we combine a local record, 'email for order 42 sent', with acceptance that a rare duplicate can happen, or we use an outbox for outgoing emails so we at least never send without recording.
6Choosing Per Operation
The practical approach is to look at each operation a consumer performs and ask two questions: is it naturally idempotent, and can events arrive out of order for it? A projection that stores the latest address uses a version check. A balance credit uses a deduplication table. The shipped status uses a plain conditional update. The confirmation email uses an idempotency key or a sent-record. One consumer may use several techniques at once, and that is normal.
7Follow-up questions
Why not just use a dedup table everywhere?
Because it adds a write and a growing table to every consumer, and it does nothing for reordering. For projections, a version check is cheaper and more correct. A dedup table is the right tool for operations that cannot be made idempotent, and overkill for those that already are.
Do version checks need a dedup table as well?
Not for duplicates of the same event, since equal versions are ignored. But if a consumer applies both full-state events and deltas, or if versions are not strictly per entity, we may still need deduplication for the delta part.
What if two events have the same version?
Then the producer is not assigning versions correctly; each change to an entity must get a new, increasing version, typically from optimistic locking on the aggregate. We should fix that at the source rather than try to break ties in consumers.
8Conclusion
In this answer, we compared four ways to make consumers idempotent. Deduplication tables record processed ids in the same transaction as the side effect and fit increments, at the cost of storage and cleanup. Natural idempotency, such as setting a status or upserting by key, fits state changes with no extra table. Version checks apply only newer versions and also handle reordering for full-state events. External calls such as payments and emails need idempotency keys that the downstream system accepts. We choose per operation, and increments and sends always need extra help.
Open with
There are three main techniques inside our own database plus one for external calls. A deduplication table records each processed event id with a unique constraint in the same transaction as the side effect, and skips the event if the id is already there.
Then, if they want more
It works for anything, which is why I use it for increments like a balance credit, but it costs a write per message, storage and cleanup. Natural idempotency means designing the operation so repeating it does nothing new: set status to SHIPPED, or upsert the customer address by customer id.
That fits projections and needs no extra table, but it does not handle reordering, an old event can overwrite a newer one. Version checks fix that: the event carries the entity's version, and I update only where the stored version is lower, so duplicates and late events are both ignored. That suits full-state events like price changed, but not deltas.
Finally, emails or payment calls are outside our transaction, so I send an idempotency key derived from the event id, and the payment provider returns the original result on a retry. I pick per operation, and often one consumer combines two techniques.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation, Spring for Apache Kafka reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare ways to make consumers idempotent: deduplication tables, natural idempotency...": https://scrivio-interview-library.netlify.app/spring-boot/event-driven-patterns/6f4241db56/