Video in progress
What is a consumer group in Kafka, and how does the number of partitions in a topic limit the number of consumers?
Consumers in one group split the topic's partitions among themselves: each partition goes to one consumer within the group. So a topic with 6 partitions can't be read in parallel by more than 6 consumers in a group; the extras sit idle.
📖 Answer
Consumers with the same group.id form one group. Kafka distributes the partitions among them:
topic: payments (6 partitions)
group billing-service: pod1 -> p0,p1 pod2 -> p2,p3 pod3 -> p4,p5
group notification: pod1 -> p0..p5
Two things follow from this.
First, different groups don't interfere with each other. Both billing and notification get every message, which is pub/sub. Inside a group, a message goes to only one consumer, which is a queue.
Second, the parallelism limit is the number of partitions. You scaled billing-service to 10 pods, but there are only 6 partitions, so 4 pods read nothing. This is common with autoscaling: "we added pods, and the lag didn't go down".
In Spring Kafka there can be several consumers inside one pod too:
@KafkaListener(topics = "payments", groupId = "billing-service", concurrency = "3")
public void on(PaymentEvent e) { ... }
2 pods × 3 concurrency = 6 consumers, exactly right for 6 partitions.
You can increase the partition count but not decrease it. When you increase it, messages with the same key start landing in a different partition and the order is briefly broken. That's why, when creating a topic, it's common practice to set somewhat more partitions than the parallelism you expect.
Sign up to mark studied questions and save your progress.