Quick revision
The short answer
The broker gives each idempotent producer a producer id, and the producer numbers its records per partition with sequence numbers. When an internal retry resends a batch that the broker already wrote, the broker sees a sequence number it has already accepted and does not write it again, so retries cannot create duplicates, and per-partition order is kept with up to 5 in-flight requests. It does not protect against the application calling send() twice, against a restarted producer that gets a new producer id, against partial writes across partitions, which need transactions, or against duplicates on the consumer side, which need idempotent consumers.
What to remember
In this answer, we saw that the idempotent producer gets a producer id and numbers records per partition with sequence numbers, so the broker recognises a retried batch it already wrote and does not append it again, keeping order with up to 5 in-flight requests. It does not stop duplicates from the application sending twice or from a restarted producer with a new id, it does not make writes across partitions atomic, which needs transactions, and it does not remove the need for idempotent consumers.
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.
The broker gives each idempotent producer a producer id, and the producer numbers its records per partition with sequence numbers. When an internal retry resends a batch that the broker already wrote, the broker sees a sequence number it has already accepted and does not write it again, so retries cannot create duplicates, and per-partition order is kept with up to 5 in-flight requests. It does not protect against the application calling send() twice, against a restarted producer that gets a new producer id, against partial writes across partitions, which need transactions, or against duplicates on the consumer side, which need idempotent consumers.
Why this question is asked
Retries are the main way a Kafka producer survives brief failures, and without idempotence they are also the main source of duplicates. Since Kafka 3.0 idempotence is on by default, so most of us use it without thinking about it, which makes it important to know exactly what it covers. Interviewers ask this to see whether we can explain the mechanism and, more importantly, its limits. In this answer, we will see how a retry creates a duplicate, how producer ids and sequence numbers prevent it, how ordering is kept, and the four kinds of duplicate it does not stop.
1How a Retry Creates a Duplicate
A producer sends a batch to the partition leader. The leader writes it to the log and sends back an acknowledgement, but the acknowledgement is lost, perhaps because of a network blip or a timeout. From the producer's point of view, the request failed, so it retries. The leader receives the same batch again and, without idempotence, has no way to know it is the same data. It appends it a second time, and the partition now holds two copies of every record in the batch.
Nothing went wrong in the usual sense; both sides behaved correctly. The problem is that 'did my write succeed?' cannot be answered by the producer alone after a lost response.
2Producer Ids and Sequence Numbers
With enable.idempotence=true, the producer first asks the cluster for a producer id, a unique number for this producer session, together with an epoch. For each partition it writes to, the producer numbers its batches with a sequence number that starts at 0 and increases with every record. Every batch carries the producer id, the epoch and the sequence number of its first record.
The broker remembers, for each producer id and partition, the sequence numbers of the most recent batches it has written. When a batch arrives, the broker checks its sequence. If it is the next expected number, the batch is written. If it matches a batch the broker already wrote, it is a retry: the broker does not append it again and simply acknowledges it, returning the original offset. If there is a gap, meaning records went missing in between, the broker rejects the batch with an out-of-order sequence error rather than silently accepting a hole.
6 messages in order. Play them, or step through them one at a time.
The duplicate check is per producer id and per partition.
3Ordering With Up to 5 In-Flight Requests
A producer can have several requests in flight to the same broker, 5 by default, to keep the network busy. Without idempotence, if request 1 fails and is retried after request 2 succeeded, the records of request 1 end up after those of request 2, out of order. With idempotence, the broker accepts batches only in sequence order. Batch 2 arriving while batch 1 is missing would leave a gap, so the producer and broker coordinate to retry in order, and the partition keeps the order in which records were sent.
The broker keeps sequence state for the last 5 batches per producer and partition, which is why idempotence requires max.in.flight.requests.per.connection to be 5 or less. Idempotence also requires acks=all and retries greater than 0; these are the defaults, and an explicit conflicting setting disables it.
4What It Does Not Protect Against
The guarantee is narrow on purpose: it removes duplicates caused by the producer's own internal retries, within one producer session, for one partition. Four kinds of duplicates remain.
First, the application sending twice. If our code catches a timeout and calls send() again, the producer treats it as a new record with a new sequence number, and the broker writes it. Idempotence knows nothing about our intent. Second, a restart. When the service restarts, a new producer instance gets a new producer id, so the broker cannot connect its records with those of the old session; if the old session had unacknowledged sends that the application replays after restart, they are written again. Transactions with a stable transactional.id address this by reusing an identity and fencing the old one.
| Source of duplicate | Prevented by idempotence? | What handles it |
|---|---|---|
| Producer's internal retry | yes | producer id and sequence numbers |
Application calls send() twice | no | business keys, dedupe downstream |
| Producer restarts, new producer id | no | transactions with a stable transactional.id |
| Partial writes across partitions | no | Kafka transactions |
| Consumer reprocessing after a crash | no | idempotent consumers |
Third, atomicity across partitions. Idempotence is per partition, so if a producer writes an invoice to one partition and an audit record to another and crashes in between, one may exist without the other. Making several writes all-or-nothing needs Kafka transactions. Fourth, consumer-side duplicates. Even with a perfect log, a consumer that processes a record and crashes before committing its offset will process it again after restart. Consumers must be idempotent, for example by storing processed event ids or by using upserts keyed on a business id.
5Follow-up questions
Does idempotence slow the producer down?
Very little. The extra work is a few bytes of producer id and sequence per batch and a small amount of state on the broker. That is why it became the default in Kafka 3.0.
What is the epoch for?
The epoch distinguishes generations of the same producer id. It matters most for transactional producers: when a new instance initialises with the same transactional.id, the epoch is bumped and the broker rejects writes from the older instance with a fencing error, so a zombie cannot keep writing.
6Conclusion
In this answer, we saw that the idempotent producer gets a producer id and numbers records per partition with sequence numbers, so the broker recognises a retried batch it already wrote and does not append it again, keeping order with up to 5 in-flight requests. It does not stop duplicates from the application sending twice or from a restarted producer with a new id, it does not make writes across partitions atomic, which needs transactions, and it does not remove the need for idempotent consumers.
Open with
When idempotence is on, which is the default since Kafka 3.0, the producer gets a producer id and an epoch from the cluster, and for each partition it numbers its records with increasing sequence numbers.
Then, if they want more
Each batch carries the producer id and its starting sequence. The broker remembers the recent sequence numbers per producer and partition.
So if the producer sends a batch, the broker writes it, and the acknowledgement is lost, the retry arrives with sequence numbers the broker has already written; it does not append them again and just acknowledges with the original offsets. If there is a gap, it rejects the batch with an out-of-order sequence error.
Because the broker tracks the last five batches, ordering per partition is kept with up to five in-flight requests, and idempotence needs acks all and max in-flight at most five. What it does not cover: if my application calls send twice, for example after a timeout, those are two records with different sequence numbers, so both are written.
If the producer restarts, it gets a new producer id, so the broker cannot match its records with the old session. It is per partition, so writing to several partitions atomically needs transactions with a transactional id.
And consumers can still process a record twice after a crash before committing, so consumers must be idempotent, for example by tracking event ids or using upserts.
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 "How does the idempotent producer prevent duplicate records when a send is retried, and...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/cff97d3a2b/