Scrivio Interview library
InternalsIntermediate7 min read · updated 13 November 2026

With enable.idempotence=true and max.in.flight.requests.per.connection=5, batch 1 to a partition fails with NOT_LEADER_OR_FOLLOWER while batches 2 to 5 are already in flight.

Explain what the producer and broker do so that the records still end up in order without duplicates, and what changes if idempotence is off.

Try it yourself

Consumer group: who reads which partition

Change the number of partitions and consumers, and see who owns what, who is idle, and where ordering holds.

Read this example as text

Who reads which partition

  1. 3 partitions, 2 consumers. C0 reads P0, P1; C1 reads P2.
  2. 4 partitions, 2 consumers. C0 reads P0, P1; C1 reads P2, P3.
  3. 3 partitions, 3 consumers. C0 reads P0; C1 reads P1; C2 reads P2.
  4. 6 partitions, 4 consumers. C0 reads P0, P1; C1 reads P2, P3; C2 reads P4; C3 reads P5.
  5. 2 partitions, 4 consumers. C0 reads P0; C1 reads P1; C2 is idle; C3 is idle.

A partition has exactly one reader in the group. A consumer beyond the number of partitions is idle.

Six records with 3 partitions and 2 consumers

  1. Record A1. Key A goes to P0, read by C0.
  2. Record B1. Key B goes to P1, read by C0.
  3. Record A2. Key A goes to P0, read by C0.
  4. Record C1. Key C goes to P2, read by C1.
  5. Record B2. Key B goes to P1, read by C0.
  6. Record A3. Key A goes to P0, read by C0.

A1, A2 and A3 sit in one partition and are read in that order. Nothing orders a record in one partition against a record in another.

What this example leaves out
  • One topic, and Kafka's range assignor, the first in the default list. Other assignors spread partitions differently.
  • A, B and C stand for key hashes 0, 1 and 2. The real client hashes the key bytes; the rule shown, same key to the same partition, is the same.
  • A rebalance is shown as instant. In a real group, consumers stop briefly while partitions move.

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.

Java
Properties p = new Properties();
p.put("enable.idempotence", true); // default since Kafka 3.0
p.put("acks", "all"); // required by idempotence, also the default
p.put("max.in.flight.requests.per.connection", 5); // must be 5 or fewer
p.put("retries", Integer.MAX_VALUE); // must be above 0
// Batches for partition orders-3 from PID 9001, epoch 0:
// batch 1: sequences 0..99, batch 2: 100..199, ... batch 5: 400..499

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.

Step throughBatch 1 fails while batches 2 to 5 are in flight
ProducerOld leaderNew leader
batches 1..5 (seq 0..499)
NOT_LEADER_OR_FOLLOWER for all five
metadata refresh, then batch 1 (seq 0..99)
seq 0 follows nothing: append
ok, offset 1000
batches 2..5 in sequence order
each follows the last: append
ok, offsets 1100..1499

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.

With and without idempotence, five requests in flight
SituationIdempotence onIdempotence off
Batch 1 retried after batch 2 succeededBatch 2 held back or rejected; order keptBatch 1 lands after batch 2; order broken
Response to batch 3 lost, batch 3 retriedBroker spots the sequence, returns successBatch 3 written twice
ThroughputFull pipelining, 5 in flightSame, 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.

What to say in the interviewAbout 80 seconds aloud

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/