What are consumer groups in Kafka and Why do we need it.
Prompt:
So why do we create consumer group in Kafka? Basically, we have a consumer group and each consumer assigned to one partition. So what happens is, we have topics on which the consumer is subscribed to. And if the topic is in one consumer group, so for example, there is a topic 1 in consumer group 1. So if there are three topics in that consumer group 1, so either of them, one of them will receive the message the producer produced to that consumer group. But if we have different consumer groups, so each of them will receive that message on their topic, either the topic, for example, if we have group 1, group 2, group 3, and in each group, we have topic 1, topic 1, topic 1. So when producer send the message, it will go to each group and each topic. But when we have the consumer group, single consumer group, and all topic 1 replicates itself three times, it will be sent to one of the topics. Okay, am I correct? Make an article on that with this same example and give me copy-paste content.
Answer
Kafka Consumer Groups Explained
What is a Consumer Group in Kafka?
A consumer group in Kafka is a mechanism that allows multiple consumers to work together and consume messages from a topic efficiently.
The main goals of consumer groups are:
- Load balancing
- Scalability
- Fault tolerance
- Parallel processing
A consumer group is identified by a unique group.id.
Example:
group.id=payment-service-group
All consumers with the same group.id belong to the same consumer group.
Important Clarification
There is a misunderstanding in the example.
A consumer group does not contain topics.
Instead:
- Topics exist independently in Kafka
- Consumers subscribe to topics
- Consumers belong to consumer groups
So the correct relationship is:
Consumer Group
└── Consumers
└── Subscribe to Topics
Core Rule of Kafka Consumer Groups
Inside a single consumer group:
- One partition is consumed by only one consumer
- Messages are distributed among consumers
- This provides load balancing
Across different consumer groups:
- Each group receives all messages independently
- This provides pub-sub behavior
Example 1 — Single Consumer Group
Suppose we have:
Topic: order-events
Partitions: 3
And we have 3 consumers in the same consumer group:
Consumer Group: order-processors
Consumer A
Consumer B
Consumer C
Kafka will assign partitions like this:
Partition 0 → Consumer A
Partition 1 → Consumer B
Partition 2 → Consumer C
Now when the producer sends messages:
Producer → order-events
The messages are distributed across partitions.
Each consumer only receives messages from its assigned partition.
So:
- Consumer A gets messages from Partition 0
- Consumer B gets messages from Partition 1
- Consumer C gets messages from Partition 2
This means:
- Messages are shared among consumers
- No duplicate processing inside the same group
- Consumers work in parallel
This is called competing consumers pattern.
Very Important Concept
Messages are NOT sent to “one of the topics”.
The topic remains the same.
What actually happens is:
Producer → Topic → Partitions → Consumers
Consumers in the same group divide the partitions among themselves.
Example 2 — Multiple Consumer Groups
Now suppose we have:
Topic: order-events
And three different consumer groups:
Group 1 → analytics-service
Group 2 → notification-service
Group 3 → audit-service
Each group has its own consumers.
Now when producer sends a message:
Producer → order-events
ALL consumer groups receive the message independently.
Meaning:
analytics-service receives message
notification-service receives message
audit-service receives message
Because Kafka stores offsets separately for each consumer group.
So every group tracks its own progress independently.
Real-World Example
Imagine an e-commerce system.
Producer publishes:
{
"orderId": 101,
"status": "CREATED"
}
to topic:
order-events
Now different services may consume it:
Consumer Group 1 — Payment Service
group.id = payment-group
Purpose:
- Process payment
Consumer Group 2 — Notification Service
group.id = notification-group
Purpose:
- Send email/SMS
Consumer Group 3 — Analytics Service
group.id = analytics-group
Purpose:
- Generate reports
Each group receives the same message independently.
So one event can trigger multiple business workflows.
Why Consumer Groups Are Needed
1. Scalability
If message traffic increases:
Consumer 1
Consumer 2
Consumer 3
can process messages in parallel.
More consumers = higher throughput.
2. Load Balancing
Kafka distributes partitions among consumers automatically.
Example:
4 partitions
2 consumers
Consumer A → Partition 0,1
Consumer B → Partition 2,3
3. Fault Tolerance
If one consumer crashes:
Consumer B crashes
Kafka rebalances partitions:
Consumer A takes over remaining partitions
Processing continues automatically.
4. Independent Processing
Different services can consume the same event independently using different consumer groups.
Example:
order-created event
→ payment service
→ email service
→ analytics service
→ fraud detection service
Important Rule About Partitions
Inside one consumer group:
Number of active consumers <= number of partitions
Example:
Topic partitions = 3
Consumers = 5
Only 3 consumers will actively consume.
The remaining 2 stay idle.
Because one partition can only be assigned to one consumer in the same group.
Simple Visual Diagram
Single Consumer Group
Topic: order-events
-----------------------
| P0 | P1 | P2 |
-----------------------
Consumer Group A
Consumer1 → P0
Consumer2 → P1
Consumer3 → P2
Messages are divided among consumers.
Multiple Consumer Groups
Topic: order-events
/ | \
/ | \
Group A Group B Group C
(Payments) (Analytics) (Emails)
Each group receives all messages independently.
Final Understanding
Same Consumer Group
Consumers share the work.
Each message is processed once per group.
Used for:
- Scaling
- Parallel processing
- Load balancing
Different Consumer Groups
Each group receives all messages independently.
Used for:
- Multiple business services
- Independent workflows
- Event-driven architecture
One-Line Summary
Same consumer group = load balancing
Different consumer groups = pub/sub
Comments
Post a Comment