What is a Kafka idempotent producer and why does it matter?
A Kafka idempotent producer is a producer configured with `enable.idempotence=true` that guarantees exactly-once delivery to a single partition within a single producer session. Without idempotence, when a producer retries a failed send (due to a timeout or transient network error), the broker may have already written the message but failed to acknowledge it — resulting in a duplicate.
The mechanism works through two identifiers. The broker assigns each producer a unique Producer ID (PID) on initialization. Each message batch sent by that producer carries a monotonically increasing sequence number per partition. When the broker receives a batch, it checks the sequence number: if the batch was already written (sequence number already seen), the broker discards it and returns success without writing again. If the sequence number is out of order (gap detected), the broker returns an `OutOfOrderSequenceException`.
Enabling idempotence automatically forces `acks=all`, `retries=Integer.MAX_VALUE`, and `max.in.flight.requests.per.connection` to at most 5. These constraints are required to preserve ordering: with `acks=all`, the broker only acknowledges after durably writing; with bounded in-flight requests and sequence numbers, the broker can detect and reject out-of-order duplicates from retries.
Idempotence has important scope limits. It deduplicates within a single producer session — if the producer process restarts, it gets a new PID, and the broker has no way to correlate the new producer with the old one. It also only covers a single partition. For multi-partition atomic writes (e.g., writing to multiple topics transactionally), you need Kafka transactions (`transactional.id` + `initTransactions()` + `commitTransaction()`), which build on top of idempotent producers.
In practice, enabling idempotence is a low-cost correctness improvement with no application-level code changes required — it should be the default for any producer where duplicate messages would cause problems.
Explains the PID + sequence number mechanism, knows that retries without idempotence cause duplicates, and understands that acks=all is forced when idempotence is enabled.
Articulates the scope limits (single session, single partition), explains why max.in.flight is capped at 5, distinguishes idempotent producer from Kafka transactions, and gives a concrete retry scenario demonstrating why the deduplication is needed.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment