Quick revision
The short answer
Every batch from an idempotent producer carries its producer id, epoch and a sequence number per partition, and the leader writes a batch only if its sequence follows directly after the last one it wrote for that producer and partition. When batch 1 fails with NOT_LEADER_OR_FOLLOWER, the producer refreshes metadata and resends batch 1 to the new leader first, holding batches 2 to 5 behind it in sequence order; any copy the broker already wrote is recognised and acknowledged instead of written twice. Without idempotence, a retried batch 1 can land after batch 2, and a lost acknowledgement can create a duplicate.
What to remember
In this answer, we saw that idempotence is a small protocol: each batch carries the producer id, epoch and a per-partition sequence, and the leader appends a batch only when its sequence follows the last one written. When batch 1 fails with NOT_LEADER_OR_FOLLOWER, the producer refreshes metadata and resends in sequence order, batch 1 first, and any batch already written is recognised from the broker's memory of the last five batches, which is why five in-flight requests is the limit. Without idempotence, retries can both reorder and duplicate records.
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.
Every batch from an idempotent producer carries its producer id, epoch and a sequence number per partition, and the leader writes a batch only if its sequence follows directly after the last one it wrote for that producer and partition. When batch 1 fails with NOT_LEADER_OR_FOLLOWER, the producer refreshes metadata and resends batch 1 to the new leader first, holding batches 2 to 5 behind it in sequence order; any copy the broker already wrote is recognised and acknowledged instead of written twice. Without idempotence, a retried batch 1 can land after batch 2, and a lost acknowledgement can create a duplicate.
Why this question is asked
Kafka promises that records with the same key arrive in order, but that promise has to survive the messy moments: a leader moving in the middle of a burst, with five requests already on the wire. Interviewers ask this to see whether we understand that ordering and exactly-once writes per partition are not magic but a small protocol between the producer and the broker. The overview sets the scene; in this answer, we will recap what in-flight requests are, look at the identifiers each batch carries, replay the failure step by step, see how duplicates are recognised and why the limit is five, and finally see what goes wrong when idempotence is turned off.
1A Quick Recap: Batches and In-Flight Requests
The producer groups records for the same partition into batches and sends each batch in a produce request to the partition's leader, the broker that accepts writes for it. Waiting for each response before sending the next request would waste most of the network round trip, so the producer allows several requests per broker connection to be outstanding at once. max.in.flight.requests.per.connection says how many; the default is 5.
In our scenario, batches 1 to 5 for the same partition have all left the producer when the first response comes back with an error. Whatever happens next must still leave them in the log in order, each exactly once.
2What Each Batch Carries
When idempotence is enabled, the producer first obtains a producer id (PID) from the cluster, together with an epoch, a small number that is raised when the producer's state has to be reset. Then, for every partition, it numbers the records it sends. Each batch carries the PID, the epoch and the sequence number of its first record.
The leader keeps, for each PID and partition, the last sequence numbers it has written. Its rule is strict: it appends a batch only if the batch's first sequence is exactly one more than the last sequence it wrote. A batch whose numbers it has already written is a duplicate. A batch that leaves a gap is out of order and is rejected with OUT_OF_ORDER_SEQUENCE_NUMBER. This one rule gives us both properties, no duplicates and no reordering, as long as the producer keeps its side of the bargain.
Notice that the requirements follow from the mechanism: acks=all lets a new leader inherit the sequences the old one accepted, retries give the protocol something to repair, and the in-flight limit comes from the broker's memory, as we will see.
3Replaying the Failure Step by Step
Batch 1 reaches the old leader just as leadership moves, and comes back with NOT_LEADER_OR_FOLLOWER. Batches 2 to 5 were sent to the same broker, which is no longer leader, so they fail the same way. The producer marks the partition's metadata as stale, fetches fresh metadata to learn the new leader, and puts the failed batches back into its queue for that partition in sequence order, not in the order the errors arrived.
When it retries, the producer sends batch 1 first. It holds back later batches for the partition until earlier sequences are confirmed or are safely ahead of them on the connection, so the new leader sees 0..99, then 100..199, and so on. If a later batch did reach the new leader ahead of an earlier one, the leader would reject it as out of order, and the producer, which knows an earlier batch is still being retried, would simply retry it again behind that one. Either way, the log ends up in the original order.
8 messages in order. Play them, or step through them one at a time.
The retry order is driven by sequence numbers, not by the order in which errors came back.
4Recognising a Batch That Was Already Written
The more subtle case is a batch that was written but whose response was lost, for example because the connection dropped. The producer cannot tell this apart from a failure, so it retries. The leader sees a batch whose sequence numbers it has already written, does not append it again, and returns success with the original offset. To the application this looks exactly like a normal acknowledgement.
To recognise duplicates, the broker keeps metadata for the last five batches per producer and partition. That is precisely why idempotence allows at most five in-flight requests: with five, any batch the producer might retry is still in the broker's memory, so it can be matched. With six, a retried batch could be older than everything the broker remembers, and it could no longer tell a duplicate from a gap.
The epoch completes the picture. If the producer has to reset its sequence state, for example after a batch expired on the delivery timeout and the numbering can no longer be continued safely, it bumps its epoch, and the broker starts a fresh sequence for the new epoch while fencing anything still arriving with the old one.
5What Changes When Idempotence Is Off
Turn idempotence off and keep five requests in flight, and the broker has no sequence numbers to check. Now suppose batch 1 fails with a retriable error, but batch 2 succeeds because, say, the old leader handled it a moment later or the error was a timeout on one request only. The producer retries batch 1, and it is appended after batch 2. Records with the same key are now out of order in the log.
Duplicates come back too. A batch written but unacknowledged is retried and written a second time, because nothing tells the broker it has seen it. The classic workaround was max.in.flight.requests.per.connection=1, which keeps order at the price of throughput, but even that does not prevent duplicates.
| Situation | Idempotence on | Idempotence off |
|---|---|---|
| Batch 1 retried after batch 2 succeeded | Batch 2 held back or rejected; order kept | Batch 1 lands after batch 2; order broken |
| Response to batch 3 lost, batch 3 retried | Broker spots the sequence, returns success | Batch 3 written twice |
| Throughput | Full pipelining, 5 in flight | Same, but no safety; 1 in flight to keep order |
Idempotence has been on by default since Kafka 3.0, and we should leave it on. With a conflicting value, such as acks=1, the client either refuses the configuration or quietly disables idempotence, depending on whether it was set explicitly and on the version, so check the producer's startup log.
6Follow-up questions
Does idempotence give exactly-once delivery end to end?
No. It guarantees that one producer session writes each record to a partition at most once and in order, even with retries. It does not cover a producer that restarts and resends records from its own source, or a consumer that processes a record twice. For a read-process-write pipeline we need transactions, and for everything else consumers should deduplicate by an event id.
What is OutOfOrderSequenceException and should the application retry it?
It means the broker saw a gap in the sequence that the producer could not repair, typically because data was lost on the broker side, for example after an unclean leader election. The producer cannot continue its numbering safely, so the application should treat the affected records as possibly lost, close or reset the producer, and alert, rather than blindly retrying.
7Conclusion
In this answer, we saw that idempotence is a small protocol: each batch carries the producer id, epoch and a per-partition sequence, and the leader appends a batch only when its sequence follows the last one written. When batch 1 fails with NOT_LEADER_OR_FOLLOWER, the producer refreshes metadata and resends in sequence order, batch 1 first, and any batch already written is recognised from the broker's memory of the last five batches, which is why five in-flight requests is the limit. Without idempotence, retries can both reorder and duplicate records.
Open with
With idempotence, which has been the default since Kafka 3.0, the producer gets a producer id and epoch, and numbers its records per partition. Every batch carries those three things, and the leader only appends a batch whose first sequence follows directly after the last one it wrote for that producer and partition.
Then, if they want more
So in this case, batch 1 comes back with NOT_LEADER_OR_FOLLOWER, and batches 2 to 5, which went to the same old leader, fail too. The producer refreshes metadata, re-queues the batches in sequence order, and sends batch 1 to the new leader first.
If a later batch somehow arrives first, the broker rejects it as out of order and the producer retries it behind batch 1. If a retried batch had already been written and only the response was lost, the broker recognises its sequence and returns success with the original offset instead of writing it again.
The broker remembers the last five batches per producer and partition, which is exactly why idempotence allows at most five in-flight requests, and it needs acks=all so a new leader inherits the sequence history. With idempotence off and several requests in flight, a retried batch 1 can land after batch 2, and a lost acknowledgement leads to a duplicate.
About this answer
- Last updated
- 13 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
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 what the producer and broker do so that the records still end up in order...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/4aa85d3759/