Quick revision
The short answer
After each deployment the job restarts from its last committed offset or checkpoint, so every record processed after that point is processed again, and plain INSERTs add those results twice. The processor's own exactly-once guarantee does not reach an external database unless the sink takes part. We get exactly-once results by making writes idempotent (a deterministic key per merchant and minute with an upsert that replaces the value), by committing the consumed offsets in the same database transaction as the results, or by using a two-phase commit sink like Flink's that commits writes when a checkpoint completes.
What to remember
In this answer, we saw that the duplicates come from restarts resuming at the last committed offset or checkpoint, so records after it are processed again and plain inserts add them twice. Processor exactly-once does not cover an external database, so the sink must take part: idempotent upserts keyed by merchant and minute, offsets stored in the same transaction as the results, or a two-phase commit sink that commits when a checkpoint completes, at the cost of checkpoint-interval latency.
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.
After each deployment the job restarts from its last committed offset or checkpoint, so every record processed after that point is processed again, and plain INSERTs add those results twice. The processor's own exactly-once guarantee does not reach an external database unless the sink takes part. We get exactly-once results by making writes idempotent (a deterministic key per merchant and minute with an upsert that replaces the value), by committing the consumed offsets in the same database transaction as the results, or by using a two-phase commit sink like Flink's that commits writes when a checkpoint completes.
Why this question is asked
Duplicates after a deployment are a classic stream processing surprise. The job reports exactly-once, the dashboards look healthy, and yet the finance team finds revenue two percent too high. Interviewers use this question to see whether we understand where a processing guarantee ends and where our own responsibility begins. In this answer, we will recap how offsets and checkpoints work, reproduce the duplicates on a timeline, see why processor exactly-once stops at the sink, and walk through three fixes with what each costs.
1A Quick Recap of Offsets and Checkpoints
A Kafka consumer tracks its position in each partition as an offset, the number of the next record to read. Periodically it commits that offset, so after a restart it can resume where it left off. Flink does something similar with checkpoints: every so often it saves the offsets it has read together with its in-memory state, and on restart it restores the latest completed checkpoint.
The key word is periodically. Between two commits, the job keeps reading and producing output. If it stops in between, the commit does not cover the records processed since, so they will be read and processed again after the restart. This is called at-least-once delivery: nothing is lost, but some things happen twice.
2Reproducing the Extra Two Percent
Here is what a deployment does to our job, which commits offsets every 30 seconds and INSERTs one row per merchant per minute.
Notice that both inserts are correct on their own; the problem is that the database cannot tell the second one is a repeat. How large the error is depends on how much work happens between commits relative to the reporting period, which is why each deployment adds a similar percentage. Increments behave the same way: UPDATE ... SET amount = amount + :x applied twice adds twice.
3Why Processor Exactly-Once Does Not Reach the Database
Flink checkpoints and Kafka transactions give exactly-once state inside the processing system: after a restart, Flink's state and the consumer offsets are consistent with each other, and with Kafka transactions, records written back to Kafka are visible to read_committed consumers only once. But our INSERT went to PostgreSQL, outside that system. The processor cannot roll it back when it rewinds, so the side effect stays. Exactly-once for the whole pipeline requires the sink to take part, which leaves three options.
4Option 1: Idempotent Writes With Deterministic Keys
An operation is idempotent when doing it twice has the same effect as doing it once. We make each window's result a row with a deterministic key, the merchant and the minute, and write it with an upsert that replaces the value rather than adding to it.
This is usually the best choice: it is simple and needs nothing special from the processor. The condition is that the job emits the full value of the window, not increments, and that a replay computes the same value, which holds when windows are based on event time rather than arrival time.
5Option 2: Offsets in the Same Transaction as the Results
Here we stop committing offsets to Kafka and store them in the database instead, in the same transaction as the results. Results and position then commit together or not at all. On startup the consumer reads its offsets from the database and seeks to them.
This even works with plain inserts, because a replay can only start from a point that matches what is in the database. The cost is code: we manage offsets ourselves, including on rebalances, and it only works when results and offsets fit in one database transaction.
6Option 3: A Two-Phase Commit Sink
Flink offers sinks that take part in its checkpoints using two-phase commit. During a checkpoint interval, the sink writes into an open transaction. When the checkpoint is taken, the sink pre-commits; only when Flink confirms that the checkpoint completed everywhere does the sink commit. If the job fails before that, the open transaction is aborted and the records are replayed into a new one.
| Option | Needs | Cost |
|---|---|---|
| Idempotent upsert | Deterministic key, full window values | Little; the usual first choice |
| Offsets in the result transaction | Transactional sink, own offset handling | More code, rebalance care |
| Two-phase commit sink | Sink with committable transactions | Latency of one checkpoint interval |
It gives true exactly-once without changing the data model, but it needs a sink that supports transactions which can be committed later, such as Kafka transactions or XA in some JDBC setups (check your Flink connector version), and the output becomes visible only when a checkpoint completes, so latency grows to the checkpoint interval.
7Follow-up questions
Which option would you choose for a sink that only supports appends, such as files in object storage?
With files we cannot upsert, but we can make the file the unit of commit. Flink's file sink writes in-progress files and only finalises them, renaming or completing the upload, when a checkpoint completes, which is the two-phase pattern applied to files. Alternatively, we make writes idempotent by naming each output file deterministically, for example by window and partition, so a replay overwrites the same file rather than adding a new one. Readers then only read finalised files.
Why not just commit offsets more often?
Committing every few records shrinks the window of duplicates but never closes it, because a crash can still happen between the write and the commit. It also adds overhead. It reduces the error; it does not make results correct.
Does Kafka's enable.idempotence setting help here?
No. Producer idempotence prevents duplicates caused by producer retries when writing to Kafka. Our duplicates come from reprocessing input after a restart and writing to PostgreSQL, which producer settings do not touch.
8Conclusion
In this answer, we saw that the duplicates come from restarts resuming at the last committed offset or checkpoint, so records after it are processed again and plain inserts add them twice. Processor exactly-once does not cover an external database, so the sink must take part: idempotent upserts keyed by merchant and minute, offsets stored in the same transaction as the results, or a two-phase commit sink that commits when a checkpoint completes, at the cost of checkpoint-interval latency.
Open with
The duplicates come from replay. The job commits its offsets or checkpoints periodically, and when a deployment stops it, everything processed since the last commit is processed again after the restart.
Then, if they want more
Our sink does plain INSERTs, so those window results are added a second time, which is the extra two percent. The exactly-once guarantee of the stream processor only covers its own state and outputs that take part in its transactions; a write to PostgreSQL is an external side effect that it cannot roll back.
So the sink has to take part, and there are three ways. First, idempotent writes: key each row by merchant and minute and upsert the full window value, so a replay replaces the row with the same number. That is my default.
Second, store the consumed offsets in the same database transaction as the results and seek to them on startup, so results and position always commit together.
Third, a two-phase commit sink like Flink's, which writes in a transaction during the checkpoint interval and commits when the checkpoint completes; it needs a sink with committable transactions and adds latency equal to the checkpoint interval. For this aggregation I would use the upsert.
About this answer
- Last updated
- 13 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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 "Explain where the duplicates come from and how to get exactly-once results in an...": https://scrivio-interview-library.netlify.app/architecture/storage-and-pipelines/1a992573b8/