📖 Scenario: You are working with Apache Kafka to process messages in real-time. Kafka uses consumer groups to distribute message processing across multiple consumers. When consumers join or leave a group, Kafka triggers a rebalance to redistribute partitions among consumers.Understanding how rebalancing works helps you build reliable and efficient Kafka consumers.
🎯 Goal: Build a simple Kafka consumer group setup that demonstrates rebalance behavior. You will create a consumer group with two consumers, configure rebalance listeners, and observe partition assignment changes when a consumer joins or leaves.
📋 What You'll Learn
Create two Kafka consumer instances with the same group ID
Configure a rebalance listener to handle partition assignment and revocation
Simulate consumer join and leave events to trigger rebalancing
Print partition assignment changes during rebalance events
💡 Why This Matters
🌍 Real World
Kafka consumer groups are widely used in real-time data processing systems to scale message consumption and ensure fault tolerance.
💼 Career
Understanding Kafka rebalancing is essential for roles like backend developer, data engineer, and site reliability engineer working with distributed streaming platforms.
Progress0 / 4 steps
1
Create Kafka consumer properties
Create a dictionary called consumer_props with these exact entries: 'bootstrap.servers': 'localhost:9092', 'group.id': 'test-group', 'key.deserializer': 'org.apache.kafka.common.serialization.StringDeserializer', and 'value.deserializer': 'org.apache.kafka.common.serialization.StringDeserializer'.
Kafka
Hint
Use a Python dictionary with the exact keys and values as shown.
2
Define a rebalance listener class
Create a class called RebalanceListener with two methods: on_partitions_revoked and on_partitions_assigned. Each method should accept a parameter partitions and print 'Partitions revoked: ' or 'Partitions assigned: ' followed by the partitions list.
Kafka
Hint
Define a class with two methods that print the partitions list with the specified messages.
3
Create two consumers with rebalance listener
Create two consumer instances called consumer1 and consumer2 using KafkaConsumer with consumer_props. Assign an instance of RebalanceListener to both consumers' subscribe method as the listener parameter for topic 'test-topic'.
Kafka
Hint
Use KafkaConsumer with consumer_props and subscribe both consumers to 'test-topic' with the rebalance listener.
4
Print partition assignments on rebalance events
Write code to simulate consumer1 polling messages once, then close consumer2 to trigger rebalance. Print 'Consumer1 polling...' before polling and 'Consumer2 closed to trigger rebalance' after closing consumer2.
Kafka
Hint
Call consumer1.poll() and then consumer2.close() with print statements before and after.
Practice
(1/5)
1. What triggers a rebalance in a Kafka consumer group?
easy
A. When a consumer commits an offset
B. When a message is consumed
C. When a topic is deleted
D. When a consumer joins or leaves the group
Solution
Step 1: Understand rebalance triggers
Rebalancing happens when the group membership changes or partitions change.
Step 2: Identify correct trigger
Joining or leaving a consumer causes the group to rebalance to share partitions evenly.
Final Answer:
When a consumer joins or leaves the group -> Option D
Quick Check:
Consumer join/leave triggers rebalance [OK]
Hint: Rebalance happens on group membership changes [OK]
Common Mistakes:
Thinking rebalance happens on every message
Confusing topic deletion with rebalance
Assuming offset commit triggers rebalance
2. Which of the following is the correct way to listen for rebalance events in Kafka consumer code?
easy
A. consumer.subscribe(topics, new ConsumerRebalanceListener() {...})
B. consumer.on('rebalance', callback)
C. consumer.addRebalanceListener(callback)
D. consumer.listenRebalance(callback)
Solution
Step 1: Recall Kafka consumer API
Kafka's Java consumer uses subscribe with a ConsumerRebalanceListener to handle rebalance events.
Step 2: Match correct syntax
Only consumer.subscribe(topics, new ConsumerRebalanceListener() {...}) shows the correct method signature for rebalance listener.
Final Answer:
consumer.subscribe(topics, new ConsumerRebalanceListener() {...}) -> Option A
Quick Check:
Use subscribe with ConsumerRebalanceListener [OK]
Hint: Use subscribe with ConsumerRebalanceListener for rebalance [OK]
Common Mistakes:
Using non-existent methods like on or addRebalanceListener
Confusing event listeners with other APIs
Missing the listener parameter in subscribe
3. Given a consumer group with 3 consumers and 6 partitions, what happens after one consumer leaves the group?
medium
A. The consumer group stops consuming messages
B. The remaining 2 consumers rebalance and each consumes 3 partitions
C. The partitions are left unassigned until the group is restarted
D. The remaining consumers continue consuming their original partitions
Solution
Step 1: Understand partition assignment
Partitions are divided evenly among active consumers in the group.
Step 2: Calculate new assignment after consumer leaves
With 6 partitions and 2 consumers left, each gets 3 partitions after rebalance.
Final Answer:
The remaining 2 consumers rebalance and each consumes 3 partitions -> Option B
Quick Check:
Partitions evenly split after consumer leaves [OK]
Hint: Partitions redistribute evenly after consumer leaves [OK]
Common Mistakes:
Assuming partitions stay with old consumers
Thinking partitions become unassigned
Believing the group stops consuming
4. You wrote a rebalance listener but your consumer crashes with a NullPointerException during rebalance. What is the likely cause?
medium
A. Not subscribing to the topic before starting the consumer
B. Using the wrong Kafka version
C. Not handling partition assignment and revocation properly in the listener
D. Committing offsets manually instead of auto-commit
Solution
Step 1: Analyze rebalance listener role
The listener must handle partition revocation and assignment to avoid null references.
Step 2: Identify cause of NullPointerException
Failing to check or update state during rebalance causes null pointer errors.
Final Answer:
Not handling partition assignment and revocation properly in the listener -> Option C