📖 Scenario: You are building a Kafka consumer application that reads messages from a topic. To ensure your application processes messages reliably, you need to manage how and when the consumer commits the offsets of messages it has processed.This project will guide you through setting up a Kafka consumer, configuring offset commit strategies, and printing the committed offsets.
🎯 Goal: Build a Kafka consumer that reads messages from a topic, uses a specific offset commit strategy, and prints the committed offsets to verify the strategy works.
📋 What You'll Learn
Create a Kafka consumer configuration with a specific group id and offset reset policy
Add a configuration variable to control the offset commit strategy (automatic or manual)
Implement the consumer logic to commit offsets according to the chosen strategy
Print the committed offsets after processing messages
💡 Why This Matters
🌍 Real World
Kafka consumers must manage offset commits to avoid processing messages multiple times or missing messages. Choosing the right commit strategy helps build reliable data pipelines.
💼 Career
Understanding consumer offset commit strategies is essential for roles involving real-time data processing, streaming applications, and building fault-tolerant systems using Kafka.
Progress0 / 4 steps
1
Create Kafka consumer configuration
Create a dictionary called consumer_config with these exact entries: 'bootstrap.servers': 'localhost:9092', 'group.id': 'test-group', and 'auto.offset.reset': 'earliest'.
Kafka
Hint
Use a Python dictionary with the exact keys and values as shown.
2
Add offset commit strategy configuration
Add a variable called commit_strategy and set it to the string 'auto' to represent automatic offset commits.
Kafka
Hint
Just create a variable named commit_strategy and assign it the string 'auto'.
3
Implement consumer offset commit logic
Write a for loop that iterates over messages. Inside the loop, if commit_strategy is 'manual', call consumer.commit() after processing each message. Assume consumer and messages are predefined.
Kafka
Hint
Use a for loop over messages and an if statement to check commit_strategy.
4
Print committed offsets
Write a print statement to display the string 'Committed offsets:' followed by the result of consumer.committed().
Kafka
Hint
Use print('Committed offsets:', consumer.committed()) to show the committed offsets.
Practice
(1/5)
1. What is the main purpose of committing consumer offsets in Kafka?
easy
A. To delete old messages from the topic
B. To increase the speed of message production
C. To create new topics automatically
D. To track which messages have been processed to avoid duplicates
Solution
Step 1: Understand consumer offset role
Consumer offsets mark the position of the last processed message in a partition.
Step 2: Identify the purpose of committing offsets
Committing offsets helps Kafka know where to resume reading after a restart, preventing duplicate processing.
Final Answer:
To track which messages have been processed to avoid duplicates -> Option D
Quick Check:
Offset commit = track processed messages [OK]
Hint: Offsets mark progress to avoid reprocessing messages [OK]
Common Mistakes:
Confusing offset commit with message production speed
Thinking offsets delete messages
Believing offsets create topics
2. Which Kafka consumer configuration enables automatic offset commits?
easy
A. enable.auto.commit=false
B. auto.offset.reset=earliest
C. enable.auto.commit=true
D. auto.commit.interval.ms=0
Solution
Step 1: Identify the setting for automatic commits
The setting enable.auto.commit controls if Kafka commits offsets automatically.
Step 2: Check the correct value to enable auto commit
Setting it to true enables automatic offset commits.
Final Answer:
enable.auto.commit=true -> Option C
Quick Check:
enable.auto.commit=true means auto commit enabled [OK]
Hint: Look for enable.auto.commit=true to enable auto commits [OK]
Common Mistakes:
Confusing auto.offset.reset with auto commit
Setting enable.auto.commit to false expecting auto commit
Misunderstanding auto.commit.interval.ms controls commit frequency, not enabling
3. Given this code snippet, what will be the output if the consumer commits offsets manually after processing 3 messages?
consumer.subscribe(["topic"])
for i in range(5):
msg = consumer.poll(1000)
print(f"Processing message {i}")
if i == 2:
consumer.commitSync()
Hint: Call commit only after processing a message [OK]
Common Mistakes:
Calling commitAsync regardless of message presence
Moving subscribe inside the loop unnecessarily
Misordering process and commit calls
5. You want to ensure your Kafka consumer starts reading from the earliest offset if no committed offset exists, but commits offsets manually only after processing batches of 10 messages. Which configuration and strategy combination is correct?
hard
A. Set auto.offset.reset to earliest and disable enable.auto.commit, then call commitSync() after every 10 messages
B. Set auto.offset.reset to latest and enable enable.auto.commit, commit automatically every 10 messages
C. Set auto.offset.reset to earliest and enable enable.auto.commit, commit manually after every message
D. Set auto.offset.reset to none and disable enable.auto.commit, never commit offsets
Solution
Step 1: Choose offset reset policy
Setting auto.offset.reset to earliest ensures reading from the start if no offset exists.
Step 2: Select commit strategy
Disabling enable.auto.commit allows manual control; calling commitSync() after 10 messages commits offsets in batches.
Final Answer:
Set auto.offset.reset to earliest and disable enable.auto.commit, then call commitSync() after every 10 messages -> Option A
Quick Check:
earliest + manual commit after batch = correct strategy [OK]
Hint: earliest + manual commit after batch = safe and controlled [OK]
Common Mistakes:
Using latest offset reset when earliest is needed
Enabling auto commit but expecting manual control
Not committing offsets causing reprocessing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.