Why Delivery Guarantees Affect Correctness in Kafka
📖 Scenario: You are building a simple message processing system using Kafka. Messages represent orders that must be processed exactly once to avoid errors like double billing or lost orders.
🎯 Goal: Learn how different Kafka delivery guarantees (at-most-once, at-least-once, exactly-once) affect the correctness of message processing.
📋 What You'll Learn
Create a Kafka producer with a list of order messages
Set a delivery guarantee configuration variable
Simulate message sending with the chosen delivery guarantee
Print the final processed orders to observe correctness
💡 Why This Matters
🌍 Real World
Kafka is used in real systems to send messages like orders, logs, or events. Delivery guarantees help ensure these messages are processed correctly without loss or duplication.
💼 Career
Understanding delivery guarantees is important for software engineers working with distributed systems, data pipelines, and event-driven architectures to build reliable applications.
Progress0 / 4 steps
1
Create a list of order messages
Create a list called orders with these exact string values: 'order1', 'order2', 'order3'
Kafka
Hint
Use square brackets to create a list and separate items with commas.
2
Set the delivery guarantee configuration
Create a variable called delivery_guarantee and set it to the string 'at-least-once'
Kafka
Hint
Assign the string 'at-least-once' to the variable delivery_guarantee.
3
Simulate sending orders with delivery guarantee
Create an empty list called processed_orders. Use a for loop with variable order to iterate over orders. Inside the loop, append order to processed_orders. If delivery_guarantee is 'at-least-once', append order a second time to simulate possible duplicates.
Kafka
Hint
Use a for loop to add each order to processed_orders. Add it twice if delivery_guarantee is 'at-least-once'.
4
Print the processed orders
Write a print statement to display the processed_orders list.
Kafka
Hint
Use print(processed_orders) to show the list.
Practice
(1/5)
1. What does the at-least-once delivery guarantee in Kafka ensure?
easy
A. Messages might be lost but never duplicated.
B. Messages are delivered exactly once with no duplicates.
C. Messages are never lost but may be delivered more than once.
D. Messages are delivered only once, but some may be lost.
Solution
Step 1: Understand at-least-once guarantee meaning
At-least-once means Kafka tries to deliver messages one or more times, so no message is lost but duplicates can occur.
Step 2: Compare options with guarantee definition
Messages are never lost but may be delivered more than once. matches this exactly: no loss but possible duplicates. Others describe different guarantees.
Final Answer:
Messages are never lost but may be delivered more than once. -> Option C
Quick Check:
At-least-once = No loss, possible duplicates [OK]
Hint: At-least-once means no loss, duplicates allowed [OK]
Common Mistakes:
Confusing at-least-once with exactly-once
Thinking at-least-once means no duplicates
Assuming messages can be lost with at-least-once
2. Which Kafka producer configuration enables exactly-once delivery semantics?
easy
A. enable.idempotence = true
B. acks = 0
C. retries = 0
D. enable.idempotence = false
Solution
Step 1: Identify config for exactly-once
Kafka requires enable.idempotence = true to avoid duplicates and support exactly-once delivery.
Step 2: Check other options
The other options disable idempotence, acknowledgments, or retries, all preventing exactly-once.
3. Given this Kafka consumer code snippet with enable.auto.commit = false and manual commit after processing, what is the likely delivery guarantee?
medium
A. At-least-once
B. At-most-once
C. Exactly-once
D. No guarantee
Solution
Step 1: Analyze manual commit effect
Manual commit after processing means messages are processed before offset commit, so duplicates can happen if failure occurs before commit.
Step 2: Match with delivery guarantees
This matches at-least-once: no loss but possible duplicates. Exactly-once requires more setup; at-most-once commits before processing.
Final Answer:
At-least-once -> Option A
Quick Check:
Manual commit after processing = at-least-once [OK]
Hint: Manual commit after processing means at-least-once [OK]
Common Mistakes:
Assuming manual commit means exactly-once
Confusing commit timing with at-most-once
Ignoring possibility of duplicates
4. You notice duplicate messages in your Kafka consumer. Which fix improves correctness by avoiding duplicates?
medium
A. Set acks = 0 on the producer
B. Set enable.idempotence = true on the producer
C. Disable retries on the producer
D. Use enable.auto.commit = true on the consumer
Solution
Step 1: Identify cause of duplicates
Duplicates often happen when producer retries send messages multiple times without idempotence.
Step 2: Choose fix to avoid duplicates
Enabling idempotence ensures retries don't create duplicates. Other options disable guarantees or auto commit which can worsen duplicates.
Final Answer:
Set enable.idempotence = true on the producer -> Option B
Quick Check:
Idempotence true prevents duplicates [OK]
Hint: Enable idempotence to avoid duplicates [OK]
Common Mistakes:
Setting acks=0 disables delivery guarantees
Disabling retries causes message loss
Relying on auto commit to fix duplicates
5. You build a financial app needing no lost or duplicated transactions. Which Kafka setup best ensures correctness?
hard
A. Producer with acks=1 and consumer with auto commit disabled
B. Producer with acks=0 and consumer with auto commit enabled
C. Producer with enable.idempotence=false and consumer with manual commit before processing
D. Producer with enable.idempotence=true, acks=all, and consumer with manual commit after processing
Solution
Step 1: Identify needed guarantees for financial app
Financial apps require exactly-once semantics: no lost or duplicated messages.
Step 2: Match Kafka configs to exactly-once
Enable idempotence and acks=all on producer ensures no duplicates and full replication. Manual commit after processing on consumer avoids message loss.
Step 3: Evaluate other options
Other options either disable guarantees or commit offsets too early, risking loss or duplicates.
Final Answer:
Producer with enable.idempotence=true, acks=all, and consumer with manual commit after processing -> Option D