0
0
KafkaConceptBeginner · 4 min read

Idempotent Producer in Kafka: What It Is and How It Works

An idempotent producer in Kafka is a producer configured to avoid duplicate messages during retries by assigning unique sequence numbers to each message. This ensures that even if a message is sent multiple times due to network issues, Kafka will store it only once, guaranteeing exactly-once delivery semantics on the producer side.
⚙️

How It Works

Imagine you are sending important letters through a mail service that sometimes loses or delays mail. To be safe, you might send the same letter again. But what if the mail service delivers the same letter twice? That would be confusing.

Kafka's idempotent producer solves this by giving each message a unique number called a sequence number. When the producer sends a message, Kafka remembers this number. If the producer tries to send the same message again because it didn't get a confirmation, Kafka checks the number and ignores duplicates.

This way, even if the network is slow or unreliable, your messages won't be stored twice. The idempotent producer keeps your data clean and consistent without extra work on your side.

💻

Example

This example shows how to enable the idempotent producer in Kafka using Java. The key setting is enable.idempotence=true.

java
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.ProducerConfig;
import java.util.Properties;

public class IdempotentProducerExample {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        props.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true);

        KafkaProducer<String, String> producer = new KafkaProducer<>(props);

        ProducerRecord<String, String> record = new ProducerRecord<>("my-topic", "key1", "Hello, Kafka!");

        producer.send(record, (metadata, exception) -> {
            if (exception == null) {
                System.out.println("Message sent successfully to partition " + metadata.partition() + " with offset " + metadata.offset());
            } else {
                exception.printStackTrace();
            }
        });

        producer.flush();
        producer.close();
    }
}
Output
Message sent successfully to partition 0 with offset 15
🎯

When to Use

Use an idempotent producer when you want to make sure your messages are not duplicated even if retries happen. This is important in financial transactions, order processing, or any system where duplicate messages can cause errors or inconsistencies.

It is especially useful when network issues or broker failures might cause the producer to resend messages. Enabling idempotence helps maintain data accuracy without complex manual checks.

Key Points

  • Idempotent producer prevents duplicate messages during retries.
  • It uses sequence numbers to track messages uniquely.
  • Enabling it requires setting enable.idempotence=true in producer config.
  • It guarantees exactly-once delivery on the producer side.
  • Ideal for critical systems needing data consistency.

Key Takeaways

Enable idempotent producer in Kafka by setting enable.idempotence=true to avoid duplicate messages.
Idempotent producer uses sequence numbers to ensure each message is stored only once.
It is essential for systems where duplicate messages can cause errors or inconsistencies.
Idempotent producer guarantees exactly-once delivery on the producer side even with retries.
Use it in critical applications like financial transactions or order processing for data accuracy.