Scrivio Interview library
DebuggingIntermediate6 min read · updated 13 November 2026

A deployment rolls 30 consumer pods one by one.

Each restart takes about 40 seconds, lag climbs from near zero to 2 million records during every deploy, and the logs show more than 60 rebalances in ten minutes. Explain the cause and how you would make deploys cheap.

Try it yourself

Consumer group: who reads which partition

Change the number of partitions and consumers, and see who owns what, who is idle, and where ordering holds.

Read this example as text

Who reads which partition

  1. 3 partitions, 2 consumers. C0 reads P0, P1; C1 reads P2.
  2. 4 partitions, 2 consumers. C0 reads P0, P1; C1 reads P2, P3.
  3. 3 partitions, 3 consumers. C0 reads P0; C1 reads P1; C2 reads P2.
  4. 6 partitions, 4 consumers. C0 reads P0, P1; C1 reads P2, P3; C2 reads P4; C3 reads P5.
  5. 2 partitions, 4 consumers. C0 reads P0; C1 reads P1; C2 is idle; C3 is idle.

A partition has exactly one reader in the group. A consumer beyond the number of partitions is idle.

Six records with 3 partitions and 2 consumers

  1. Record A1. Key A goes to P0, read by C0.
  2. Record B1. Key B goes to P1, read by C0.
  3. Record A2. Key A goes to P0, read by C0.
  4. Record C1. Key C goes to P2, read by C1.
  5. Record B2. Key B goes to P1, read by C0.
  6. Record A3. Key A goes to P0, read by C0.

A1, A2 and A3 sit in one partition and are read in that order. Nothing orders a record in one partition against a record in another.

What this example leaves out
  • One topic, and Kafka's range assignor, the first in the default list. Other assignors spread partitions differently.
  • A, B and C stand for key hashes 0, 1 and 2. The real client hashes the key bytes; the rule shown, same key to the same partition, is the same.
  • A rebalance is shown as instant. In a real group, consumers stop briefly while partitions move.

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.

Rebalances during one rolling deploy of 30 pods
SetupRebalancesWho pauses each time
Eager, dynamic members (today)about 60all 30 consumers
Cooperative, dynamic membersabout 60, smallonly partitions that move
Static membership, restart under timeoutabout 0only the restarting pod's partitions
Static + cooperative or KIP-848about 0; small if a pod fails to returnonly 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.

YAML
# StatefulSet container env
env:
- name: POD_NAME
valueFrom: { fieldRef: { fieldPath: metadata.name } } # orders-consumer-0 .. orders-consumer-29
# application.properties
spring.kafka.consumer.properties.group.instance.id=${POD_NAME}
spring.kafka.consumer.properties.session.timeout.ms=75000 # above a 40 s restart
spring.kafka.consumer.properties.partition.assignment.strategy=org.apache.kafka.clients.consumer.CooperativeStickyAssignor

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.

Step throughOne pod's restart with the fixes in place
  1. SIGTERM: stop container, finish batch, commitstatic member: no LeaveGroup sent
  2. Pod restarts in about 40 sits partitions wait; others keep consuming
  3. Back within session.timeout.ms (75 s)?
    YesRejoin with same instance id, same partitionsNoSession expires, small cooperative rebalance
  4. 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.

What to say in the interviewAbout 90 seconds aloud

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/