Quick revision
The short answer
With eager rebalancing, each of the 30 pods triggers two group-wide rebalances, one when it leaves and one when it rejoins, so a rolling deploy causes about 60 rebalances, each pausing all 30 consumers, and lag piles up. The fix is static membership, a stable group.instance.id such as the StatefulSet pod name with session.timeout.ms above the 40 s restart (60 to 90 s), so a restarted pod reclaims its partitions with no rebalance, plus cooperative rebalancing or the new consumer protocol for the moves that still happen, faster restarts, graceful shutdown and a rollout that waits until each pod is consuming again.
What to remember
In this answer, we traced the 60 rebalances to eager rebalancing with dynamic members: every pod leaves and rejoins, and each rebalance stops all 30 consumers, so lag climbs to 2 million records. Static membership with a stable StatefulSet pod name and a session timeout above the 40 s restart removes those rebalances, cooperative rebalancing or the new consumer protocol makes the remaining ones small, and graceful shutdown with readiness-gated rollouts keeps restarts short. The trade-off is that a pod that fails to return leaves its partitions idle until the session timeout, so the timeout stays only as long as needed.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With eager rebalancing, each of the 30 pods triggers two group-wide rebalances, one when it leaves and one when it rejoins, so a rolling deploy causes about 60 rebalances, each pausing all 30 consumers, and lag piles up. The fix is static membership, a stable group.instance.id such as the StatefulSet pod name with session.timeout.ms above the 40 s restart (60 to 90 s), so a restarted pod reclaims its partitions with no rebalance, plus cooperative rebalancing or the new consumer protocol for the moves that still happen, faster restarts, graceful shutdown and a rollout that waits until each pod is consuming again.
Why this question is asked
Deploy-time lag is a cost teams often accept as normal, until it starts breaching latency targets every time someone ships. Here the numbers tell the story: 30 pods, more than 60 rebalances, and 2 million records of lag on every deploy. Interviewers ask this to see whether we can connect those numbers to the rebalance protocol and pick fixes that remove the cause rather than hide it. In this answer, we will explain where the 60 rebalances come from, why each one is so expensive, then apply static membership, cooperative rebalancing and better restarts, and finish with the trade-off static membership brings.
1Recap: What Causes a Rebalance
A consumer group rebalances whenever its membership changes. A pod that shuts down cleanly sends a LeaveGroup request; the coordinator rebalances to give its partitions to the others. When the new pod starts, it sends JoinGroup with a brand-new member id, and the group rebalances again to give it a share.
With the default eager assignor, RangeAssignor, every rebalance is a stop-the-world event: every member revokes all of its partitions, commits, rejoins and waits for the new assignment before consuming anything.
2Where 60 Rebalances Come From
Multiply it out. 30 pods each leave and join once, so 30 times 2 is 60 rebalances, matching the logs. During a ten-minute deploy, roughly every ten seconds the whole group stops. Each stop lasts until the slowest member reaches its next poll() and joins, often several seconds, and longer if some member is in the middle of a big batch.
While all 30 consumers are paused, producers keep writing. If the topic receives, say, 50,000 records per second, 40 seconds of accumulated pauses is already 2 million records of lag. So the lag is not caused by slow pods; it is caused by the group repeatedly stopping.
| Setup | Rebalances | Who pauses each time |
|---|---|---|
| Eager, dynamic members (today) | about 60 | all 30 consumers |
| Cooperative, dynamic members | about 60, small | only partitions that move |
| Static membership, restart under timeout | about 0 | only the restarting pod's partitions |
| Static + cooperative or KIP-848 | about 0; small if a pod fails to return | only the restarting pod's partitions |
3Fix 1: Static Membership
Static membership tells the coordinator that a restarted pod is the same member. We set group.instance.id to a name that is stable across restarts and unique in the group. In Kubernetes, a StatefulSet gives exactly that: pod orders-consumer-7 comes back as orders-consumer-7. A static member does not send LeaveGroup on shutdown, and when it rejoins with the same id before session.timeout.ms expires, the coordinator returns its previous partitions without rebalancing anyone.
The session timeout must therefore be longer than a restart. With 40 s restarts, 60 to 90 s gives enough margin for a slow image pull or a slow startup.
Notice that a plain Deployment does not work for this, because its pod names are random and change on every rollout; each new pod would be a new static member while the old one lingers until its session times out. The broker also caps the session timeout with group.max.session.timeout.ms (30 minutes by default), so 75 s is allowed.
4Fix 2: Make the Remaining Moves Cheap
Some rebalances still happen: when we scale, when a pod fails to come back in time, or when a node dies. Cooperative rebalancing, with CooperativeStickyAssignor, keeps the partitions that do not move consuming during those, so a rebalance costs a few partitions a few seconds instead of the whole group. Switching an eager group needs two rolling restarts, the first listing both assignors and the second only the cooperative one.
On Kafka 4.x, the new consumer group protocol, group.protocol=consumer, goes further: the broker computes assignments and members converge one by one with no group-wide barrier. It also supports static membership. Check that the client libraries in use support it before switching.
5Fix 3: Shorten and Smooth the Restarts
The faster each pod restarts, the less lag its own partitions accumulate. Startup can often be cut by avoiding heavy work before the consumer starts. Shutdown should be graceful: on SIGTERM, stop the listener container, finish the current batch, commit offsets and close the consumer, within a terminationGracePeriodSeconds long enough for that.
The rollout itself should wait until a pod is consuming again before restarting the next. A readiness probe that reports ready only once the consumer has its assignment and has polled successfully does that; without it, Kubernetes may restart the next pod while the previous one is still starting, stacking up the pauses.
- SIGTERM: stop container, finish batch, commitstatic member: no
LeaveGroupsent - Pod restarts in about 40 sits partitions wait; others keep consuming
- Back within session.timeout.ms (75 s)?YesRejoin with same instance id, same partitionsNoSession expires, small cooperative rebalance
- Readiness: assigned and pollingonly now does the rollout move to the next pod
4 steps in order. Play them, or step through them one at a time.
6The Trade-Off of Static Membership
Static membership trades rebalances for idle partitions. While a pod restarts, nobody consumes its partitions, and if it never comes back, for example because the new version crashes on startup, its partitions stay idle until session.timeout.ms expires. With a 75 s timeout that costs 75 s of lag on those partitions, which is acceptable. With a 10-minute timeout, chosen 'to be safe', a failed pod could leave partitions untouched for ten minutes. So we keep the timeout just above the normal restart time and alert on consumers that do not return.
7Follow-up questions
Would raising session.timeout.ms alone have helped?
No. Without a group.instance.id, a cleanly stopped consumer sends LeaveGroup immediately, and the new pod joins with a new member id, so both rebalances still happen. The session timeout only delays detection of crashed members; static membership is what lets the coordinator recognise the returning pod.
How do we verify the fix?
Run a deploy and compare the group's rebalance count, which should drop from about 60 to near zero, the maximum lag during the deploy, and the per-partition lag of each restarting pod, which should show only a short bump. The coordinator logs and consumer metrics such as rebalance-total make this easy to measure.
8Conclusion
In this answer, we traced the 60 rebalances to eager rebalancing with dynamic members: every pod leaves and rejoins, and each rebalance stops all 30 consumers, so lag climbs to 2 million records. Static membership with a stable StatefulSet pod name and a session timeout above the 40 s restart removes those rebalances, cooperative rebalancing or the new consumer protocol makes the remaining ones small, and graceful shutdown with readiness-gated rollouts keeps restarts short. The trade-off is that a pod that fails to return leaves its partitions idle until the session timeout, so the timeout stays only as long as needed.
Open with
Thirty pods each leave and rejoin, and with eager rebalancing and normal dynamic members each of those is a full rebalance where every consumer revokes all its partitions and waits for a new assignment.
Then, if they want more
Thirty times two is sixty, which matches the logs, and the whole group stops every ten seconds or so. Producers keep writing during those pauses, so lag climbs to millions. The main fix is static membership.
I would run the consumers as a StatefulSet and set group.instance.id to the pod name, which stays the same across restarts, and set session.timeout.ms above the 40-second restart, say 75 seconds. A static member does not send LeaveGroup on shutdown, and when it comes back with the same id within the timeout it gets its partitions back with no rebalance.
Then I would switch to CooperativeStickyAssignor, or the new consumer protocol on Kafka 4, so the rebalances that still happen only pause the partitions that move.
I would also shorten restarts: graceful shutdown that finishes the batch and commits, and a readiness probe that only passes once the consumer is assigned and polling, so the rollout waits before restarting the next pod.
The trade-off is that a pod that does not come back leaves its partitions idle until the session timeout, so I keep that timeout just above the normal restart time.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Each restart takes about 40 seconds, lag climbs from near zero to 2 million records...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/407cb204b0/