Challenge - 5 Problems
Kafka Idempotent Producer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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();
Attempts:
2 left
💡 Hint
Idempotent producer prevents duplicates caused by retries, not multiple sends in the same session.
✗ Incorrect
Idempotent producer ensures that retries of the same message do not result in duplicates. However, sending the same message multiple times intentionally will produce multiple messages in the topic.
❓ Predict Output
intermediate2: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();
Attempts:
2 left
💡 Hint
Idempotence and acks are related but not strictly dependent in Kafka configuration.
✗ Incorrect
'acks=all' means the leader waits for all replicas to acknowledge. Idempotence is optional and disabled here, so messages send successfully but without idempotent guarantees.
🔧 Debug
advanced2: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();
Attempts:
2 left
💡 Hint
Idempotence tracks sequence numbers per producer instance to avoid duplicates.
✗ Incorrect
When the producer is closed and reopened, it loses its sequence number state. This causes Kafka to treat messages as new and duplicates can occur after retries.
📝 Syntax
advanced2:00remaining
Which Kafka producer configuration snippet correctly enables idempotence with safe retries?
Select the configuration snippet that correctly enables idempotence and configures retries safely.
Attempts:
2 left
💡 Hint
Idempotence requires 'acks=all' and retries enabled to be effective.
✗ Incorrect
Option B correctly sets 'enable.idempotence' to string "true", retries to max integer, and 'acks' to 'all'. Option B uses invalid retries (-1) and 'acks' to '1'. Option B disables idempotence. Option B disables retries.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Exactly-once semantics require atomic writes and idempotent producer with proper acknowledgments.
✗ Incorrect
Exactly-once delivery requires idempotence, 'acks=all', infinite retries to handle failures, and transactions to atomically commit messages. Other options either reduce reliability or do not guarantee exactly-once.