In this lesson, you will learn how Kafka dynamically redistributes partition ownership among consumers to ensure continuous processing when the group topology changes.
The Problem: Dynamic Membership
In a distributed streaming architecture, we want our consumers to scale horizontally. When you add a new consumer to a group or an existing one crashes, the workload must be re-divided. If a consumer goes offline, the partitions it was handling would otherwise stop being processed. If a new one joins, we want it to take on some of the load to improve throughput.
The Mechanism: The Group Coordinator
Kafka manages this via a Group Coordinator—a specific broker node responsible for the state of a consumer group. When members join or leave, they trigger a "rebalance."
- Join Group: New consumers send a
JoinGrouprequest. - Sync Group: The Group Coordinator designates one consumer as the "Group Leader." This leader receives the list of all active consumers and their subscriptions.
- Partition Assignment: The leader applies a partition assignment strategy (like
RangeAssignororRoundRobinAssignor) to map consumers to partitions. It sends this plan back to the coordinator, which distributes it to the rest of the group.
The Impact on Processing
During a standard rebalance, a "stop-the-world" effect occurs. All consumers stop processing data while the partition map is updated. This prevents multiple consumers from reading from the same partition simultaneously, which would break your ordering guarantees.
Here is a simplified view of how a consumer identifies itself to the group:
Properties props = new Properties();
props.put("group.id", "order-processing-group");
props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.RoundRobinAssignor");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("orders-topic"));
When a rebalance is triggered, your consumer will receive a callback (if you implement ConsumerRebalanceListener). This allows you to commit offsets manually before the partitions are revoked, ensuring no data is processed twice when the group resumes.
Key Takeaway: Consumer group rebalancing is the automatic coordination mechanism that maintains high availability by detecting membership changes and recalculating partition assignments, though it introduces temporary processing pauses that must be managed.