Sub topic 2
Consumer groups and partition assignment
This chapter assumes you know what a topic, a partition and an offset are, and that you can read a basic Spring Boot application.yml. Here you will learn how Kafka divides a topic's partitions among the consumers that read it.
What a consumer group is
A consumer group is a set of consumers that share one group.id and cooperate to read a topic. Kafka gives each group two guarantees:
- Within a group, each partition is assigned to exactly one consumer at a time.
- Each group tracks its own offsets, independently of every other group.
The first guarantee splits the work. The second lets several applications read the same topic without interfering. In the diagram, order-service shares the three partitions of orders between two consumers. order-audit is a separate group, so it reads every record again.
What this means for ordering and parallelism
Kafka orders records only within a partition. Because one consumer in a group owns a partition, it sees that partition's records in order. This is why the producer chapter keys events by order ID. All events for one order land on one partition, and one consumer processes them in sequence.
The number of partitions caps the parallelism of a group. Consumers beyond that number sit idle.
| Consumers in group | Partitions | Assignment |
|---|---|---|
| 1 | 3 | One consumer reads all three |
| 2 | 3 | One reads two partitions, the other reads one |
| 3 | 3 | One partition each |
| 4 | 3 | Three are active, one is idle |
Two groups in Spring
In Spring, the group is set with groupId on @KafkaListener, or with spring.kafka.consumer.group-id as the default. Two listeners in different groups both receive every record:
package com.example.orders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
@Component
public class OrderAuditListener {
private static final Logger log = LoggerFactory.getLogger(OrderAuditListener.class);
@KafkaListener(topics = "orders", groupId = "order-audit")
public void audit(OrderEvent event) {
log.info("Audit saw order {}", event.orderId());
}
}Each OrderEvent published to orders is delivered once to order-service and once to order-audit. If you start a second instance of the application, both instances join the same groups. Kafka then splits the partitions between the instances automatically.
Rebalancing
A rebalance is the redistribution of partitions among the members of a group. It is triggered when:
- a consumer joins or leaves the group,
- a consumer stops sending heartbeats, or fails to call
poll()withinmax.poll.interval.ms, - the topic gains partitions.
During a rebalance, affected consumers pause. With the eager protocol, every consumer gives up all its partitions and is reassigned. The cooperative protocol, enabled by CooperativeStickyAssignor, moves only the partitions that must change owner. Recent Kafka clients list RangeAssignor and CooperativeStickyAssignor by default. You can choose the assignor with partition.assignment.strategy.
A slow listener is a common cause of unwanted rebalances. If processing one batch takes longer than max.poll.interval.ms, Kafka treats the consumer as dead and reassigns its partitions. The records are then processed again by another consumer.
Offsets belong to the group
Kafka stores each group's committed offsets in the internal __consumer_offsets topic, keyed by group, topic and partition. A consumer that takes over a partition resumes from the group's last committed offset.
The setting auto-offset-reset: earliest applies only when the group has no committed offset for a partition. A new group name therefore starts from the beginning of the topic. An existing group ignores the setting and resumes where it left off.
Exercise
A topic has 4 partitions. Group A has 6 consumers and group B has 2 consumers. How many consumers in each group are active, and does group B receive every record?
Show answer
Group A has 4 active consumers and 2 idle ones, because each partition goes to only one consumer in a group. Group B has 2 active consumers that share the 4 partitions, two each. Group B still receives every record, because each group tracks its own offsets and reads the full topic independently of group A.