The one rule of assignment
Within a consumer group, each partition is assigned to exactly one consumer. A consumer may own several partitions; a partition is never shared.
topic orders, 4 partitions, group "billing" with 2 consumers
p0 ──┐
p1 ──┴──► consumer A
p2 ──┐
p3 ──┴──► consumer B
Add a third consumer and the load redistributes. Add a fifth and something less pleasant happens.
Partition count caps parallelism
4 partitions, 5 consumers in one group
p0 ──► A
p1 ──► B
p2 ──► C
p3 ──► D
E ← assigned nothing, consumes nothing, costs money
Scaling a consumer deployment beyond the partition count does nothing. This is the single most common wasted-capacity mistake in Kafka deployments: an autoscaler happily runs 20 pods against a 6-partition topic, and 14 of them idle while lag keeps growing. Throughput is bounded by partitions, so relieving lag means either faster per-record processing or more partitions — and more partitions has the key-mapping consequence from Module 2.
Groups are the fan-out mechanism
Different group.id values are fully independent readers of the same data.
┌──► group "billing" (own offsets)
topic orders ─────┼──► group "warehouse" (own offsets)
└──► group "analytics" (own offsets)
So: one group = competing consumers sharing the work (queue-like). Many groups = each gets every record (pub/sub-like). Kafka gives you both with the same primitive.
A consequence worth stating: two instances of the same service that accidentally use different group.id values will each process everything. Duplicate side effects, and it looks like a bug in your code.
Rebalancing
A rebalance reassigns partitions across the group. It is triggered by:
- A consumer joining (deploy, scale-up)
- A consumer leaving cleanly (
close()) - A consumer being declared dead (missed heartbeats or exceeded
max.poll.interval.ms) - Partition count changing on a subscribed topic
Under the default eager protocol, all consumers stop consuming, give up their partitions, and receive a new assignment. That is a short processing stall for the whole group. Cooperative incremental rebalancing reduces this by only moving the partitions that need to move — worth enabling for large groups.
Liveness: two independent timers
These get conflated constantly, and the distinction is exactly what causes rebalance storms.
| Setting | Question it answers | Enforced by |
|---|---|---|
session.timeout.ms | Is the consumer process alive? | Background heartbeat thread |
max.poll.interval.ms | Is the consumer making progress? | Time between poll() calls |
Heartbeats are sent on a background thread, so a consumer stuck in a 10-minute handle() call still looks alive. What evicts it is max.poll.interval.ms: the group coordinator sees no new poll() and concludes the consumer is wedged.
Rebalance storms
1. Batch takes longer than max.poll.interval.ms
2. Coordinator evicts the consumer → rebalance
3. Its partitions move to other consumers, which are now MORE loaded
4. Those consumers exceed max.poll.interval.ms too
5. More evictions → more rebalances → group makes no progress
6. Lag climbs while CPU sits idle waiting on rebalances
The signature is a group that spends most of its time rebalancing and almost none consuming, with lag rising steadily. Diagnose it by correlating rebalance frequency with per-batch processing duration.
Fixes, in order:
- Lower
max.poll.records. Fewer records per batch means shorter batches. Cheapest, safest first move. - Make processing faster, or move slow work (external HTTP, large writes) off the poll thread with care — you still must not commit offsets for work that has not completed.
- Raise
max.poll.interval.msif the work is genuinely long and bounded. This is a real fix, not just a bandaid, but it also delays detection of truly stuck consumers. - Enable cooperative rebalancing to reduce the cost of each rebalance.
- Set
group.instance.idfor static membership so rolling restarts do not trigger full rebalances.
Rebalance listeners
If you commit offsets manually, you must handle losing partitions mid-flight. ConsumerRebalanceListener.onPartitionsRevoked is where you commit what you have finished — otherwise the next owner replays it.
Poll-loop shape
The consumer is not thread-safe. One consumer per thread. The usual patterns:
| Pattern | Trade-off |
|---|---|
| One consumer per thread | Simple, ordering preserved, parallelism capped by partitions |
| Poll on one thread, hand off to a worker pool | Higher throughput, but you must track completion to commit safely and you lose per-partition ordering unless you shard workers by partition |
The second pattern is where "we lost ordering" bugs are born. If you use it, shard by partition, not round-robin.
Exercise: A group of 3 consumers reads a 6-partition topic. Each record triggers an external API call averaging 400 ms, and max.poll.records is at the default 500. The group rebalances every few minutes and lag grows. Calculate roughly how long one batch takes, explain which timer is being violated, and give the change you would make first plus one you would explicitly avoid.