Consumer groups: how Kafka scales reads
Consumer groups are how Kafka spreads work across machines. An analogy helps:
Imagine a restaurant with 4 tables (partitions) serving pizza. You have a consumer group - your team of waiters.
Scenario 1: One waiter (consumer)
-
Serves all 4 tables alone
-
Overworked, slow service
Scenario 2: Four waiters
-
Each serves exactly one table
-
Perfect distribution
-
Maximum parallelism
Scenario 3: Eight waiters
-
Only 4 can work (one per table)
-
Other 4 sit idle
-
Waste of resources
The rule
Effective consumers = min(num_partitions, num_consumers_in_group)
Multiple Consumer Groups
You can have multiple consumer groups reading the same topic independently:
Topic: user-events (4 partitions)
Consumer Group "analytics"
├── Consumer 1 → Partition 0, 1
└── Consumer 2 → Partition 2, 3
Consumer Group "fraud-detection"
├── Consumer 1 → Partition 0
├── Consumer 2 → Partition 1
├── Consumer 3 → Partition 2
└── Consumer 4 → Partition 3
Each group maintains its own offset, so they don’t interfere with each other.
Offsets: your position in each partition
Offset = Your reading position in a partition
Think of it like bookmarking in a book:
Partition 0:
[msg-0] [msg-1] [msg-2] [msg-3] [msg-4] [msg-5]
↑
last read (offset=3)
Kafka stores these bookmarks in a special internal topic called __consumer_offsets.
When Do You Move the Bookmark?
The critical decision: When do you move the bookmark?
Auto-commit (dangerous default)
consumer = KafkaConsumer(
enable_auto_commit=True,
auto_commit_interval_ms=5000 # Every 5 seconds
)
for message in consumer:
process(message) # What if this fails?
Problem: Kafka commits offset even if processing fails. You lose messages.
Manual commit (safer)
consumer = KafkaConsumer(
enable_auto_commit=False
)
for message in consumer:
try:
process(message)
consumer.commit() # Commit after success
except Exception:
# Handle error, don't commit
log_and_retry(message)
Async commit (fastest)
def on_commit(offsets, response):
if response:
log_error(response)
for message in consumer:
process(message)
consumer.commit_async(callback=on_commit)
Delivery semantics
At-most-once (commit before processing)
consumer.commit() # Commit first
process(message) # Process second
# If processing fails → message lost
At-least-once (commit after processing)
process(message) # Process first
consumer.commit() # Commit second
# If commit fails → message reprocessed
Exactly-once (transactional)
consumer.begin_transaction()
result = process(message)
database.write(result)
consumer.commit_offsets(offsets)
consumer.commit_transaction()
# Atomic: either both happen or neither
For most systems, at-least-once delivery plus idempotent processing is the right default: exactly-once costs throughput and only covers Kafka-to-Kafka pipelines.
Rebalancing: where most consumer incidents start
Most consumer-side incidents involve a rebalance. Here is what happens:
Trigger events:
- Consumer joins group
- Consumer crashes/leaves
- Consumer takes too long to process
- Partition count changes
The rebalance dance:
- Coordinator detects trigger
- ALL consumers stop consuming (this is the problem)
- Partition assignment recalculated
- Consumers resume with new assignments
With the classic eager protocol, nothing processes until every member has rejoined, so one consumer stuck in a slow batch holds up the whole group (up to max.poll.interval.ms, 5 minutes by default). The rebalancing lesson reproduces this on a real cluster.
Cooperative Sticky Rebalancing
The modern solution to minimize rebalance impact:
Old way (Eager rebalancing):
Consumer 1: [P0, P1, P2] → [] → [P0, P1]
Consumer 2: [P3, P4, P5] → [] → [P2, P3, P4, P5]
↑
everyone stops
New way (Cooperative rebalancing):
Consumer 1: [P0, P1, P2] → [P0, P1] (keeps most partitions)
Consumer 2: [P3, P4, P5] → [P2, P3, P4, P5] (picks up P2)
↑
only P2 pauses briefly
Enable it:
partition.assignment.strategy=org.apache.kafka.clients.consumer.CooperativeStickyAssignor
On Kafka 4.x you can go one step further with the new protocol, group.protocol=consumer, where the broker computes assignments. Lesson 4 compares all three with real timings.
Heartbeat and Session Management
The coordinator doesn’t know if a consumer is alive unless it tells it:
session.timeout.ms = 45000 # "If I don't hear from you in 45s, I'll kick you out"
heartbeat.interval.ms = 3000 # "I'll send heartbeat every 3s"
max.poll.interval.ms = 300000 # "Max 5 mins between poll() calls"
A common failure
Your processing takes 6 minutes:
for message in consumer:
slow_processing(message) # Takes 6 minutes
# Consumer kicked out after 5 minutes!
Fix:
max.poll.interval.ms = 600000 # 10 minutes
# OR better - process in batches:
messages = consumer.poll(timeout_ms=1000, max_records=100)
for message in messages:
process_quickly(message) # Back to poll() soon
Static membership for stable deployments
If you’re running on Kubernetes and pods restart frequently, each restart triggers a rebalance. This is painful.
Solution: Static group membership
consumer = KafkaConsumer(
group_instance_id='pod-1-unique-id' # Stable across restarts
)
Now when pod restarts, Kafka recognizes it as the same consumer and skips rebalance.
Consumer Configuration Cheat Sheet
Group coordination
group.id = my-consumer-group
group.instance.id = unique-per-instance # For static membership
Rebalancing
partition.assignment.strategy = org.apache.kafka.clients.consumer.CooperativeStickyAssignor
session.timeout.ms = 45000
heartbeat.interval.ms = 3000
max.poll.interval.ms = 300000
Offset management
enable.auto.commit = false # Manual commit preferred
auto.offset.reset = earliest # Or 'latest'
Performance tuning
fetch.min.bytes = 1024 # Min data per fetch
fetch.max.wait.ms = 500
max.poll.records = 500 # Messages per poll()
max.partition.fetch.bytes = 1048576 # 1 MB per partition
To see where consumer groups fit next to brokers, replicas and the controller, see the diagram in Kafka architecture explained.
Hands-on exercise: watch a consumer group share partitions
Goal: see how a group splits partitions, what “lag” and committed offsets look like, and how to replay a topic.
- Use the
orderstopic from Lesson 1 (3 partitions). Open two terminals and start one consumer in each, in the same group:
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders --group demo --from-beginning
- In a third terminal, describe the group:
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group demo
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group demo --members
- Start a third and then a fourth consumer in the same group and describe it again.
- Stop all consumers, then rewind the group to the start of the topic (first with
--dry-run, then with--execute):
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group demo \
--reset-offsets --to-earliest --topic orders --dry-run
Questions: How many partitions does each consumer get with 2, 3 and 4 consumers? What does the 4th consumer do? Why must the group be stopped before you reset offsets?
What you should see
- With 2 consumers, one owns 2 partitions and the other owns 1 (
#PARTITIONSin the--membersview). With 3, each owns exactly one. - The 4th consumer gets no partitions and sits idle: a partition is read by at most one consumer in a group, so consumers beyond the partition count add no throughput (they are only hot standbys).
CURRENT-OFFSETis the group’s committed position,LOG-END-OFFSETthe end of the partition, andLAGthe difference. If you start a new group without--from-beginning,CURRENT-OFFSETshows-: the group starts at the end and has committed nothing yet.- Kafka refuses to reset offsets for a group with active members (
Error: Assignments can only be reset if the group 'demo' is inactive, but the current state is Stable.), because running consumers would immediately commit over the new positions.
Key Takeaways
- Consumer groups enable horizontal scaling - more consumers = more parallelism
- Manual offset management is safer than auto-commit
- Use cooperative rebalancing to minimize downtime
- Static membership prevents rebalance storms in containerized environments
- At-least-once + idempotent processing is the sweet spot for most use cases
Quiz
- A topic has 4 partitions and a consumer group has 6 consumers. How many consumers do work?
- Two consumer groups read the same topic. Do they affect each other’s progress?
- Why can auto-commit lose messages?
Show answers
- Four. Effective consumers = min(partitions, consumers); the other two sit idle.
- No. Each group keeps its own committed offsets, so both receive every message independently.
- Offsets are committed on a timer, regardless of whether processing succeeded; if processing fails after a commit, those messages are skipped.