Apache Kafka Streaming (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
What is a Consumer and a Consumer Group? How does group rebalancing work?
junior LevelCode ExampleApache Kafka
In Simple Words:
- Consumer: A client application that subscribes to topics and processes records.
- Consumer Group: A collection of consumers cooperating to read from a topic.
- Each partition in a topic is assigned to exactly one consumer within a group.
- If a topic has 4 partitions and a consumer group has 2 consumers, each consumer processes 2 partitions.
- If a consumer group has 4 consumers, each gets 1 partition (ideal 1:1 parallelism).
- If a consumer group has 6 consumers for 4 partitions, 2 consumers sit idle as hot standbys.
- Rebalance: When a consumer crashes or a new consumer joins the group, Kafka triggers a rebalance to reassign partitions among active group members.
sql
Topic: 'payments' (4 Partitions)
+-------------+ +-------------+ +-------------+ +-------------+
| Partition 0 | | Partition 1 | | Partition 2 | | Partition 3 |
+-------------+ +-------------+ +-------------+ +-------------+
\ / \ /
v v v v
+--------------------+ +--------------------+
| Consumer A (Pod 1) | | Consumer B (Pod 2) |
+--------------------+ +--------------------+
[ Consumer Group: 'fraud-detection-service' ]