Bird
Raised Fist0
Kafkadevops~5 mins

Idempotent producer in Kafka - Commands & Configuration

Choose your learning style10 modes available
Introduction
When sending messages to Kafka, sometimes duplicates happen if the network or system retries. An idempotent producer makes sure that even if it sends the same message multiple times, Kafka stores it only once. This helps keep data clean and consistent.
When you want to avoid duplicate messages in Kafka caused by retries.
When your application needs exactly-once message delivery guarantees.
When network issues cause message resending and you want to prevent duplicates.
When processing financial transactions or orders where duplicates cause errors.
When you want Kafka to handle message uniqueness automatically without extra code.
Config File - producer.properties
producer.properties
bootstrap.servers=localhost:9092
key.serializer=org.apache.kafka.common.serialization.StringSerializer
value.serializer=org.apache.kafka.common.serialization.StringSerializer
enable.idempotence=true
acks=all
retries=5
max.in.flight.requests.per.connection=5

This configuration file sets up a Kafka producer with idempotence enabled.

  • bootstrap.servers: Kafka server address.
  • key.serializer and value.serializer: How to convert keys and values to bytes.
  • enable.idempotence=true: Turns on idempotent producer to avoid duplicates.
  • acks=all: Waits for all replicas to confirm message receipt for safety.
  • retries=5: Number of times to retry sending on failure.
  • max.in.flight.requests.per.connection=5: Controls how many messages can be sent without waiting for acknowledgment, balancing speed and ordering.
Commands
Starts a Kafka producer using the configuration file with idempotence enabled to send messages to 'example-topic'.
Terminal
kafka-console-producer --broker-list localhost:9092 --topic example-topic --producer.config producer.properties
Expected OutputExpected
No output (command runs silently)
→
--broker-list - Specifies Kafka server addresses.
→
--topic - Specifies the topic to send messages to.
→
--producer.config - Loads producer settings from the given file.
Sends a single message 'Hello Kafka' to the topic using the idempotent producer configuration.
Terminal
echo "Hello Kafka" | kafka-console-producer --broker-list localhost:9092 --topic example-topic --producer.config producer.properties
Expected OutputExpected
No output (command runs silently)
→
--broker-list - Specifies Kafka server addresses.
→
--topic - Specifies the topic to send messages to.
→
--producer.config - Loads producer settings from the given file.
Reads the first message from 'example-topic' to verify that the message was sent once without duplicates.
Terminal
kafka-console-consumer --bootstrap-server localhost:9092 --topic example-topic --from-beginning --max-messages 1
Expected OutputExpected
Hello Kafka
→
--bootstrap-server - Specifies Kafka server addresses.
→
--topic - Specifies the topic to read messages from.
→
--from-beginning - Reads messages from the start of the topic.
→
--max-messages - Limits the number of messages to read.
Key Concept

If you remember nothing else from this pattern, remember: enabling idempotence in the Kafka producer prevents duplicate messages even if retries happen.

Common Mistakes
Not setting enable.idempotence=true in the producer configuration.
Without this setting, Kafka does not guarantee no duplicates on retries.
Always include enable.idempotence=true in your producer properties to activate idempotent behavior.
Setting acks to 0 or 1 instead of all.
Lower acks values reduce reliability and can cause duplicates or lost messages.
Set acks=all to ensure all replicas confirm the message for safe delivery.
Using max.in.flight.requests.per.connection higher than 5.
Higher values can cause message reordering which breaks idempotence guarantees.
Keep max.in.flight.requests.per.connection at 5 or less to maintain ordering.
Summary
Create a producer configuration file with enable.idempotence=true to avoid duplicate messages.
Use kafka-console-producer with the config file to send messages safely.
Verify message delivery with kafka-console-consumer to confirm no duplicates.

Practice

(1/5)
1.

What is the main purpose of enabling enable.idempotence=true in a Kafka producer?

easy
A. To enable message encryption
B. To prevent duplicate messages from being sent to Kafka topics
C. To compress messages before sending
D. To increase the speed of message delivery

Solution

  1. Step 1: Understand idempotence in Kafka producer

    Idempotence means sending the same message multiple times results in only one message stored.
  2. Step 2: Identify the effect of enabling idempotence

    Setting enable.idempotence=true ensures no duplicate messages are produced even if retries happen.
  3. Final Answer:

    To prevent duplicate messages from being sent to Kafka topics -> Option B
  4. Quick Check:

    Idempotence prevents duplicates = D [OK]
Hint: Idempotence means no duplicates sent [OK]
Common Mistakes:
  • Confusing idempotence with message speed
  • Thinking it compresses messages
  • Assuming it encrypts data
2.

Which of the following is the correct way to enable idempotence in Kafka producer configuration?

producerProps = new Properties();
// Which line correctly enables idempotence?
easy
A. producerProps.put("enableIdempotence", true);
B. producerProps.put("idempotent.enable", "true");
C. producerProps.put("enable.idempotence", "true");
D. producerProps.put("enable.idempotence", true);

Solution

  1. Step 1: Check Kafka producer config key for idempotence

    The correct key is exactly "enable.idempotence" as a string.
  2. Step 2: Check value type and syntax

    Values in Properties must be strings, so "true" as a string is correct.
  3. Final Answer:

    producerProps.put("enable.idempotence", "true"); -> Option C
  4. Quick Check:

    Correct key and string value = C [OK]
Hint: Use exact key "enable.idempotence" with string "true" [OK]
Common Mistakes:
  • Using wrong key name like "idempotent.enable"
  • Passing boolean true instead of string "true"
  • Using camelCase key instead of dot notation
3.

Given the following Kafka producer configuration snippet, what will be the effect when sending messages?

props.put("enable.idempotence", "true");
props.put("acks", "all");
props.put("retries", Integer.MAX_VALUE);

Choose the best description of the behavior.

medium
A. Messages are guaranteed to be delivered exactly once without duplicates
B. Messages may be duplicated if retries happen
C. Messages will be lost if broker is down
D. Producer will not retry sending messages

Solution

  1. Step 1: Analyze idempotence and acks settings

    Enabling idempotence with acks=all ensures leader and replicas confirm message receipt.
  2. Step 2: Consider retries setting

    Unlimited retries mean producer will keep trying until success, avoiding message loss or duplicates.
  3. Final Answer:

    Messages are guaranteed to be delivered exactly once without duplicates -> Option A
  4. Quick Check:

    Idempotence + acks=all + retries = exactly once [OK]
Hint: Idempotence + acks=all + retries = no duplicates [OK]
Common Mistakes:
  • Thinking duplicates still happen with idempotence
  • Assuming messages can be lost with retries set
  • Ignoring the importance of acks=all
4.

Identify the error in this Kafka producer configuration snippet that aims to enable idempotence:

Properties props = new Properties();
props.put("enable.idempotence", true);
props.put("acks", "all");
props.put("retries", 5);
medium
A. The value for "enable.idempotence" should be a string "true", not boolean true
B. "acks" should be set to "1" instead of "all"
C. Retries cannot be set to 5 when idempotence is enabled
D. Properties object cannot be used for Kafka configuration

Solution

  1. Step 1: Check the type of value for "enable.idempotence" key

    Kafka Properties expect string values, but here boolean true is used.
  2. Step 2: Understand impact of wrong value type

    Using boolean instead of string may cause config to be ignored or error.
  3. Final Answer:

    The value for "enable.idempotence" should be a string "true", not boolean true -> Option A
  4. Quick Check:

    Config values must be strings = A [OK]
Hint: Config values in Properties must be strings [OK]
Common Mistakes:
  • Using boolean true instead of string "true"
  • Changing acks to 1 which weakens guarantees
  • Thinking retries=5 is invalid with idempotence
5.

You want to ensure your Kafka producer sends messages exactly once even if the network is unstable. Which combination of settings should you use?

// Choose the best configuration combination
hard
A. enable.idempotence=false, acks=1, retries=0
B. enable.idempotence=false, acks=all, retries=Integer.MAX_VALUE
C. enable.idempotence=true, acks=0, retries=3
D. enable.idempotence=true, acks=all, retries=Integer.MAX_VALUE

Solution

  1. Step 1: Identify settings for exactly-once delivery

    Idempotence must be enabled to avoid duplicates on retries.
  2. Step 2: Check acknowledgement and retry settings

    Setting acks=all ensures full commit, and unlimited retries prevent message loss.
  3. Final Answer:

    enable.idempotence=true, acks=all, retries=Integer.MAX_VALUE -> Option D
  4. Quick Check:

    Idempotence + acks=all + max retries = exactly once [OK]
Hint: Enable idempotence + acks=all + max retries for exactly once [OK]
Common Mistakes:
  • Disabling idempotence but expecting no duplicates
  • Using acks=0 or 1 which weakens delivery guarantees
  • Setting retries to 0 causing message loss
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard