Kafka Idempotent Producer
The idempotent producer prevents duplicate messages caused by producer retries within a single session. This document covers configuration and usage patterns.
Overview
Section titled “Overview”
The idempotent producer assigns sequence numbers to records in each partition batch, allowing brokers to detect and deduplicate retries.
Configuration
Section titled “Configuration”# Explicitly require idempotenceenable.idempotence=trueIdempotence is enabled by default when no conflicting configuration is present. If it is not set explicitly, a conflicting acks, retries, or max.in.flight.requests.per.connection value disables it. If enable.idempotence=true is explicit, the same conflict causes a ConfigException during producer construction.
When enabled, these settings are enforced:
| Configuration | Required Value | Reason |
|---|---|---|
acks | all | Acknowledge after all current in-sync replicas have the record |
retries | > 0 | Allow retries |
max.in.flight.requests.per.connection | ≤ 5 | Maintain sequence ordering |
How It Works
Section titled “How It Works”| Component | Purpose |
|---|---|
| Producer ID (PID) | Unique identifier assigned on startup |
| Sequence Number | Per-partition counter advanced by the number of records in each batch |
| Epoch | Fences zombie producers |
The broker validates sequences:
| Condition | Result |
|---|---|
seq == expected | Accept, increment expected |
seq < expected | Detect the duplicate and return the prior batch outcome |
seq > expected | Reject the gap with an out-of-order sequence error |
Scope and Limitations
Section titled “Scope and Limitations”| Covered | Not Covered |
|---|---|
| Internal retries within one producer session | Producer restart or a new PID |
| Network timeouts | Multiple producer instances |
| Transient broker failures | Calling send() again for the same logical record |
Application Retries Are New Sends
Idempotence deduplicates retries performed internally by one producer instance. If application code handles an error by creating and sending the record again, Kafka treats it as a new send and it can create a duplicate.
For cross-session or cross-partition atomicity, use transactions.
Java Example
Section titled “Java Example”Properties props = new Properties();props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka:9092");props.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true);props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
try (KafkaProducer<String, String> producer = new KafkaProducer<>(props)) { for (int i = 0; i < 100; i++) { producer.send(new ProducerRecord<>("events", "key-" + i, "value-" + i), (metadata, exception) -> { if (exception != null) { log.error("Send failed", exception); } }); }}Related Documentation
Section titled “Related Documentation”- Producer Guide - Producer overview
- Transactions - Transactional producer
- Delivery Semantics - EOS details
- Protocol APIs - Protocol internals