Quick revision
The short answer
I would give every event a stable unique id at the source, created once and kept through every retry, and then protect each hop in the way that fits it: idempotent and transactional producers and exactly-once processing for the part inside Kafka, and at the database sink an upsert by event id or a processed-ids table written in the same transaction as the ledger change. On top of that, regular reconciliation compares totals with the source systems and alerts on differences, and the design documents where duplicates are prevented and where they are detected and repaired.
What to remember
In this answer, we designed end-to-end exactly-once behaviour for a ledger. A stable event id created once at the source, ideally through an outbox, ties the pipeline together. Idempotent and transactional producers and exactly_once_v2 processing cover the Kafka part, and the database sink upserts by event id or records processed ids in the same transaction as the ledger change. Regular reconciliation against the source systems detects anything that slips through, and documentation records where duplicates are prevented and where they are detected and repaired.
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.
I would give every event a stable unique id at the source, created once and kept through every retry, and then protect each hop in the way that fits it: idempotent and transactional producers and exactly-once processing for the part inside Kafka, and at the database sink an upsert by event id or a processed-ids table written in the same transaction as the ledger change. On top of that, regular reconciliation compares totals with the source systems and alerts on differences, and the design documents where duplicates are prevented and where they are detected and repaired.
Why this question is asked
A ledger that double-counts is worse than one that is slow: balances are wrong, and someone has to find out which entries to remove. Kafka's exactly-once features help, but a real pipeline also has external producers and a database at the end, and each hop has its own way of creating duplicates. Interviewers ask this design question to see whether we can reason about the whole chain rather than quoting one feature. In this answer, we will map the pipeline and its duplicate risks, then design each hop: the source id, the producers, the stream processor and the database sink, and finally the reconciliation and documentation that keep the guarantee honest.
1Map the Pipeline and Where Duplicates Come From
Our pipeline has three parts. Producers in several services publish transaction events, such as card payments and transfers, to Kafka. A stream processor enriches and aggregates them, for example converting currencies and assigning each event to a ledger account. A sink writes the results into the ledger database.
Each hop can duplicate in its own way. A producer can retry a send whose acknowledgement was lost, or a whole service can re-run a job and publish the same business event again. The stream processor can crash after writing output but before committing input offsets. The sink can write rows and crash before committing its offset, then write them again. No single mechanism covers all three, so we design each one and connect them with one idea: a stable event id.
- Source serviceassigns event_id once, e.g. from an outbox row
- Produceridempotent + transactional: no retry duplicates
- Kafka topicsread_committed consumers only
- Stream processorexactly_once_v2: outputs and offsets atomic
- Database sinkupsert by event_id / processed_ids in same transaction
- Reconciliation jobdetects anything that slipped through
6 layers, from the top down. Step through them one at a time.
2A Stable Id Created Once at the Source
Everything downstream depends on being able to tell 'the same event again' from 'a new event that looks the same'. Two 40.00 payments by the same customer a second apart may be genuine. So the source creates a unique id for each business event, once, at the moment the business fact is recorded, and never regenerates it.
The safest way is to create the id in the same database transaction that records the fact, for example in an outbox table: the payment row and an outbox row with event_id are committed together, and a relay publishes the outbox row to Kafka. If the relay publishes twice, both copies carry the same event_id. If the source used a random UUID generated at send time instead, a retry of the whole send would produce a new id and the duplicate would be invisible.
3Producers and the Stream Processor
Inside Kafka, the built-in features do most of the work. The idempotent producer, enabled by default since Kafka 3.0, removes duplicates caused by its own retries. The relay can also use a transactional producer so that a batch of outbox rows is published atomically.
The stream processor runs with exactly-once processing, for example Kafka Streams with processing.guarantee=exactly_once_v2, or a Spring Kafka listener with a KafkaTransactionManager. Each read-process-write cycle commits outputs and input offsets in one transaction, so a crash leads to a clean re-run with no visible half-written output. Every downstream consumer reads with isolation.level=read_committed. The processor must carry the source event_id into every output record; if it aggregates several events into one, the output gets its own deterministic id, derived for example from the window and key, so a re-run produces the same id.
4The Database Sink
The sink is where Kafka's guarantees end, because the ledger database is outside Kafka. A crash between writing ledger rows and committing the consumer's offset will cause the same records to be delivered again. So the sink must be idempotent, and there are two common ways.
The first is an upsert keyed by event id: the ledger entry's primary key, or a unique column, is the event_id, and the insert does nothing if it already exists. The second is a processed-ids table: in the same database transaction as the ledger change, we insert the event_id into processed_event with a unique constraint. A second delivery fails the insert or finds the id and skips. Because both happen in one transaction, either the change and the id are stored together or neither is.
Notice that the balance update is not idempotent on its own: adding 40.00 twice gives 80.00. It becomes safe only because it shares a transaction with the processed-id insert. An alternative is to store the consumed offset per partition in the same transaction and seek to it on startup, which also gives exactly-once into the database.
5Reconcile and Document
Even a careful design can be broken by a bug, a manual replay or a source system that re-sent an event with a new id. So we detect as well as prevent. A reconciliation job compares the ledger with the source systems on a schedule, for example every hour: count and total of payments per account and day in the payment service against the ledger. Any difference raises an alert with the affected accounts, and a repair process posts corrections as new, auditable entries rather than editing history.
Finally, we document the guarantee: for each hop, whether duplicates are prevented, detected or repaired, and by what. That document is what an auditor or a new engineer needs, and it makes clear that the end-to-end promise comes from the combination, not from one Kafka setting.
| Hop | Prevented by | Detected by |
|---|---|---|
| Source re-sends an event | Same event_id from the outbox | Sink dedupe on event_id |
| Producer retry | Idempotent producer | - |
| Processor crash mid-cycle | exactly_once_v2 transaction | - |
| Sink crash before commit | processed_event in same DB transaction | - |
| Bug or manual replay | - | Hourly reconciliation with sources |
6Follow-up questions
Why not use the Kafka offset alone as the deduplication key at the sink?
Offsets identify a position in one topic, not a business event. If the same event is published twice upstream, it gets two different offsets, and a replay into a new topic changes offsets entirely. Offsets are fine for exactly-once from one topic into the database, but the event id is what protects against duplicates from earlier hops.
How long do we keep processed ids?
At least as long as a duplicate can still arrive, which is bounded by topic retention plus any replay window, for example 30 days. Older ids can be purged by partition or date. For a ledger, the ledger entry itself often carries the event_id as a unique key, so it is kept forever at no extra cost.
Is reconciliation really necessary if every hop is idempotent?
Yes, for money. Idempotency protects against the failures we thought of; reconciliation catches the ones we did not, such as a source bug that creates two ids for one payment. Auditors also expect evidence that totals agree, not only an argument that they should.
7Conclusion
In this answer, we designed end-to-end exactly-once behaviour for a ledger. A stable event id created once at the source, ideally through an outbox, ties the pipeline together. Idempotent and transactional producers and exactly_once_v2 processing cover the Kafka part, and the database sink upserts by event id or records processed ids in the same transaction as the ledger change. Regular reconciliation against the source systems detects anything that slips through, and documentation records where duplicates are prevented and where they are detected and repaired.
Open with
I'd design it hop by hop around one idea: a stable event id. The source creates a unique id once, when the business fact is recorded, ideally in an outbox row in the same database transaction, so any re-send carries the same id.
Then, if they want more
Inside Kafka I'd rely on the built-in features: idempotent producers, which are the default, a transactional relay if it publishes batches, exactly-once processing in the stream processor, so exactly_once_v2 in Kafka Streams or a transactional listener in Spring Kafka, and read_committed on every consumer. The processor carries the event id into its outputs, or derives a deterministic id for aggregates.
The database sink is outside Kafka's guarantee, so it must be idempotent: either the ledger entry has a unique event id and I upsert, or I insert the id into a processed-events table in the same database transaction as the balance change, so a redelivery is skipped.
Then, because it's money, I'd add reconciliation: an hourly job comparing counts and totals per account against the source systems, alerting on differences and posting corrections as new entries. And I'd document, per hop, whether duplicates are prevented, detected or repaired, so it's clear that the end-to-end guarantee comes from the combination, not from one Kafka setting.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Kafka 3.0
- 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 "You are asked to guarantee that a ledger built from Kafka events never double-counts...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/2ed5e586c1/