Bird
Raised Fist0
Kafkadevops~20 mins

Idempotent producer in Kafka - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kafka Idempotent Producer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output when sending messages with idempotent producer enabled?
Consider the following Kafka producer configuration and code snippet. What will be the output in the Kafka topic after running this code?
Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("enable.idempotence", "true");

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

for (int i = 0; i < 3; i++) {
    producer.send(new ProducerRecord<>("my-topic", "key1", "message"));
}
producer.flush();
producer.close();
AThe topic will contain 3 identical messages with key 'key1' and value 'message'.
BThe topic will contain 1 message with key 'key1' and value 'message' because idempotence prevents duplicates.
CThe topic will contain no messages because the producer is idempotent and suppresses all sends.
DThe code will throw a runtime exception because sending multiple messages with the same key is not allowed.
Attempts:
2 left
💡 Hint
Idempotent producer prevents duplicates caused by retries, not multiple sends in the same session.
❓ Predict Output
intermediate
2:00remaining
What error occurs if 'enable.idempotence' is set to false but 'acks' is set to 'all'?
Given the following Kafka producer configuration, what will happen when sending messages?
Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("enable.idempotence", "false");
props.put("acks", "all");

KafkaProducer<String, String> producer = new KafkaProducer<>(props);
producer.send(new ProducerRecord<>("my-topic", "key", "value"));
producer.flush();
producer.close();
AThe producer sends messages but with a warning logged about inconsistent configuration.
BThe producer throws a ConfigException because 'acks=all' requires 'enable.idempotence=true'.
CThe producer silently enables idempotence despite the configuration to ensure data safety.
DThe producer sends messages successfully with full acknowledgment but without idempotence guarantees.
Attempts:
2 left
💡 Hint
Idempotence and acks are related but not strictly dependent in Kafka configuration.
🔧 Debug
advanced
2:00remaining
Why does this idempotent producer code cause duplicate messages after a network failure?
Examine the code below. Despite enabling idempotence, duplicate messages appear in the topic after a network failure. What is the cause?
Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("enable.idempotence", "true");

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

for (int i = 0; i < 5; i++) {
    producer.send(new ProducerRecord<>("my-topic", "key", "message"));
    if (i == 2) {
        // Simulate network failure by closing producer abruptly
        producer.close();
        producer = new KafkaProducer<>(props);
    }
}
producer.flush();
producer.close();
AIdempotence only works within a single producer instance; closing and reopening resets sequence numbers causing duplicates.
BThe producer configuration is missing 'acks=all', so duplicates occur despite idempotence.
CThe topic is configured with multiple partitions, so duplicates are expected with idempotence enabled.
DThe key serializer is incorrect, causing messages to be treated as different keys and duplicated.
Attempts:
2 left
💡 Hint
Idempotence tracks sequence numbers per producer instance to avoid duplicates.
📝 Syntax
advanced
2:00remaining
Which Kafka producer configuration snippet correctly enables idempotence with safe retries?
Select the configuration snippet that correctly enables idempotence and configures retries safely.
Aprops.put("enable.idempotence", true); props.put("retries", -1); props.put("acks", "1");
Bprops.put("enable.idempotence", "true"); props.put("retries", Integer.MAX_VALUE); props.put("acks", "all");
Cprops.put("enable.idempotence", "false"); props.put("retries", 5); props.put("acks", "all");
Dprops.put("enable.idempotence", "true"); props.put("retries", 0); props.put("acks", "all");
Attempts:
2 left
💡 Hint
Idempotence requires 'acks=all' and retries enabled to be effective.
🚀 Application
expert
3:00remaining
How to ensure exactly-once semantics in Kafka producer with idempotence enabled?
You want to guarantee that messages are delivered exactly once to a Kafka topic using an idempotent producer. Which approach below correctly achieves this?
AEnable idempotence and set 'acks' to '1' to reduce latency and rely on retries for duplicates.
BDisable idempotence but use manual offset commits in the consumer to avoid duplicates.
CEnable idempotence, set 'acks' to 'all', configure infinite retries, and use transactions to commit messages atomically.
DEnable idempotence and set 'retries' to 0 to avoid resending messages and duplicates.
Attempts:
2 left
💡 Hint
Exactly-once semantics require atomic writes and idempotent producer with proper acknowledgments.

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