📖 Scenario: You work in a team managing a Kafka messaging system. To keep the system healthy, you need to monitor how far behind each consumer group is from the latest messages. This delay is called consumer lag. Monitoring lag helps prevent message loss and delays in processing.
🎯 Goal: Build a simple script that tracks consumer lag for a Kafka topic by comparing the latest message offsets with the consumer group's committed offsets.
📋 What You'll Learn
Create a dictionary with Kafka topic partitions and their latest offsets
Create a dictionary with consumer group committed offsets for each partition
Calculate the lag for each partition by subtracting committed offset from latest offset
Print the lag for each partition clearly
💡 Why This Matters
🌍 Real World
Monitoring consumer lag is critical in real Kafka deployments to ensure timely processing of messages and to avoid data loss or delays.
💼 Career
Understanding consumer lag and how to monitor it is a key skill for DevOps engineers and site reliability engineers working with Kafka.
Progress0 / 4 steps
1
Create latest offsets data
Create a dictionary called latest_offsets with these exact entries: 0: 150, 1: 200, 2: 180. This represents the latest message offset for each partition of the Kafka topic.
Kafka
Hint
Use curly braces to create a dictionary with partition numbers as keys and offsets as values.
2
Create consumer group committed offsets
Create a dictionary called committed_offsets with these exact entries: 0: 145, 1: 198, 2: 175. This represents the last processed offset by the consumer group for each partition.
Kafka
Hint
Use a dictionary similar to latest_offsets but with committed offset values.
3
Calculate consumer lag per partition
Create a dictionary called consumer_lag that calculates lag for each partition by subtracting the committed offset from the latest offset using a for loop with variables partition and latest iterating over latest_offsets.items().
Kafka
Hint
Use a for loop to go through each partition and calculate lag by subtracting committed offset from latest offset.
4
Print the consumer lag
Use a for loop with variables partition and lag iterating over consumer_lag.items() to print the lag in the format: Partition {partition} lag: {lag}.
Kafka
Hint
Use a for loop to print each partition's lag with the exact format.
Practice
(1/5)
1. What does consumer lag in Kafka indicate?
easy
A. The delay between the latest message produced and the message consumed
B. The number of consumers in a consumer group
C. The total number of messages produced to a topic
D. The size of the Kafka topic in bytes
Solution
Step 1: Understand consumer lag meaning
Consumer lag measures how far behind a consumer is from the latest message produced in the topic partition.
Step 2: Differentiate from other metrics
It is not about consumer count, total messages, or topic size, but about delay in consumption.
Final Answer:
The delay between the latest message produced and the message consumed -> Option A
Quick Check:
Consumer lag = delay behind latest message [OK]
Hint: Lag means delay between produced and consumed messages [OK]
Common Mistakes:
Confusing lag with consumer count
Thinking lag is topic size
Mixing lag with total messages produced
2. Which command correctly shows consumer lag for a group named my-group?
easy
A. kafka-consumer-groups.sh --describe --group my-group
B. kafka-consumer-groups.sh --list --group my-group
C. kafka-topics.sh --describe --group my-group
D. kafka-producer-perf-test.sh --group my-group
Solution
Step 1: Identify correct command for consumer lag
The command to describe consumer group lag is kafka-consumer-groups.sh --describe --group <group-name>.
Step 2: Eliminate incorrect commands
--list lists groups but does not show lag; kafka-topics.sh is for topics, not groups; kafka-producer-perf-test.sh is unrelated.
Final Answer:
kafka-consumer-groups.sh --describe --group my-group -> Option A
Quick Check:
Describe group shows lag [OK]
Hint: Use --describe with --group to see lag [OK]
Common Mistakes:
Using --list instead of --describe
Confusing topic commands with consumer group commands
Using producer commands for consumer lag
3. Given this output from kafka-consumer-groups.sh --describe --group my-group:
Hint: Add lag values from all partitions for total lag [OK]
Common Mistakes:
Using LOG-END-OFFSET as lag
Confusing CURRENT-OFFSET with lag
Adding offsets instead of lag values
4. You run kafka-consumer-groups.sh --describe --group my-group but see no lag column. What is the likely cause?
medium
A. The Kafka broker is down
B. The topic does not exist
C. The command syntax is incorrect
D. The consumer group has no committed offsets yet
Solution
Step 1: Understand missing lag column reason
If the consumer group has no committed offsets, lag cannot be calculated and may not show.
Step 2: Exclude other causes
Broker down or wrong syntax usually cause errors, not missing lag column; topic existence affects consumption but not lag display here.
Final Answer:
The consumer group has no committed offsets yet -> Option D
Quick Check:
No committed offsets = no lag shown [OK]
Hint: No committed offsets means lag won't appear [OK]
Common Mistakes:
Assuming broker down causes missing lag column
Thinking syntax error causes missing lag column
Confusing topic existence with lag display
5. You notice high consumer lag for a group processing a topic with 3 partitions. The lag per partition is 100, 0, and 50 respectively. What is the best approach to reduce lag quickly?
hard
A. Delete and recreate the consumer group
B. Increase the topic partition count to 10
C. Add more consumers to the group to increase parallel processing
D. Restart the Kafka broker to clear lag
Solution
Step 1: Analyze lag distribution
Partitions have uneven lag: 100, 0, 50. Some partitions are behind.
Step 2: Choose action to reduce lag
Adding consumers can help process partitions faster by parallel consumption.
Step 3: Evaluate other options
Increasing partitions alone won't reduce lag immediately; restarting broker or deleting group won't fix lag directly.
Final Answer:
Add more consumers to the group to increase parallel processing -> Option C
Quick Check:
More consumers = faster lag reduction [OK]
Hint: Add consumers to process partitions faster [OK]
Common Mistakes:
Thinking restarting broker clears lag
Believing increasing partitions instantly reduces lag
Deleting group removes offsets but not lag solution
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.