Producer idempotence configures KafkaProducer to deliver records in order and only once within a topic partition, preventing retries from creating duplicates or reordering batches. section 4.4 section 4.4.1
Configure the producer with these related settings:
- Set enable.idempotence=true. section 4.4.1
- Set acks to all, keep retries greater than 0, and keep max.in.flight.requests.per.connection at no more than 5. section 4.4.1
- Use unique producer IDs and monotonically increasing sequence numbers assigned per partition and producer session. section 4.4.1
Idempotent KafkaProducer configuration
A Java producer sets ACKS_CONFIG to "all", ENABLE_IDEMPOTENCE_CONFIG to true, RETRIES_CONFIG to Integer.MAX_VALUE, and MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION to 5.
Listing 4.6 KafkaProducer configured for idempotence
Map<String, Object> producerProps = new HashMap<>();
//Standard configs
producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "somehost:9092");
producerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ...);
producerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ...);
//Configs related to idempotence
producerProps.put(ProducerConfig.ACKS_CONFIG, "all"); #1
producerProps.put(
ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true); #2
producerProps.put(
ProducerConfig.RETRIES_CONFIG,
Integer.MAX_VALUE); #3
producerProps.put(
ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION,
5); #4
Inspect the complete Java configuration listing and its numbered idempotence settings. section 4.4.1


