Video in progress
How does the idempotent producer work internally, and which duplicates does it protect against?
The broker gives each producer a PID, and the producer sends batches for each partition with a sequence number. If the broker sees a sequence it has already written, it returns "OK" without writing it again. It removes only the duplicates caused by producer retries.
📖 Answer
The problem appears like this: the producer sent a batch, the leader wrote it, but the response got lost on the network. After a timeout the producer resends it, and without idempotence there are two copies in the log.
With enable.idempotence=true:
- when the producer starts it gets a
PIDand an epoch; - for each
(PID, partition)the sequence number grows from 0; - for each partition the broker remembers the sequence numbers of the last few batches of each PID and drops duplicates; if there's a gap, it throws
OutOfOrderSequenceException.
That's why with idempotence max.in.flight.requests.per.connection can be up to 5 and the order is still preserved.
What it doesn't protect against:
- the application calling
send()twice (for example, a retry in your own code): those are two different messages; - a producer restart: it gets a new PID, with no link to the old sequence numbers;
- duplicates on the consumer side.
If you need protection across producer restarts too, use transactional.id: it ties the PID to a stable identifier.
Sign up to mark studied questions and save your progress.