What is a Consumer Group in Kafka?
AΒ Consumer Group in Kafka is a collection of one or more consumer instances (processes/threads) that work together to consume messages from one or more topics.
-
Each consumer in the group reads from a different partition of a topic.
-
Together, the group ensures that each message in a partition is processed by only one consumer in that group.
π This provides scalability (parallel consumption) and fault tolerance.
πΉ Key Properties
-
Group ID
-
Each consumer group has a unique
group.id
. -
Example:
billing-consumers
,analytics-consumers
.
-
-
Partition Assignment
-
Kafka automatically divides partitions among consumers in the group.
-
If number of consumers > partitions β some consumers remain idle.
-
If consumers < partitions β some consumers process multiple partitions.
-
-
Offset Management
-
Each group tracks its offsets (position in partition).
-
Stored in Kafkaβs internal topic
__consumer_offsets
. -
Allows groups to restart from the last committed offset.
-
πΉ Example Scenario
Topic orders
with 4 partitions:
P0 P1 P2 P3
Case 1: One Consumer (Group A)
-
Consumer-1
reads all partitions (P0βP3).
Case 2: Two Consumers (Group A)
-
Consumer-1
β P0 + P1 -
Consumer-2
β P2 + P3
Case 3: Four Consumers (Group A)
-
Each consumer reads exactly one partition.
Case 4: More Consumers than Partitions
-
One or more consumers stay idle (not assigned any partition).
πΉ Multiple Groups Example
-
Group A (Billing Service) consumes topic
orders
. -
Group B (Analytics Service) also consumes topic
orders
.
π Each group gets a full copy of the data β enabling fan-out to different applications.
πΉ Benefits of Consumer Groups
β
Scalability β distribute load across multiple consumers.
β
Fault tolerance β if a consumer crashes, partitions are reassigned to remaining consumers.
β
Parallelism β multiple consumers process partitions in parallel.
β
Isolation β multiple groups can consume the same topic independently.
πΉ In Short
A Consumer Group = a team of consumers with a shared group ID, consuming from a topic together:
-
Each partition β assigned to only one consumer in the group.
-
Offsets tracked per group.
-
Enables scalable, fault-tolerant, parallel consumption.