Quick revision
The short answer
Each scale event triggers a rebalance, and with the classic eager protocol every partition stops during it, so lag jumps, which triggers more scaling and more rebalances: the autoscaler is reacting to lag that its own actions create. To stabilise it, scale on a smoother signal such as estimated time to drain or lag sustained over several minutes, add larger scale-up steps, a scale-down stabilisation window and a cooldown, and make rebalances cheap with the KIP-848 consumer protocol or cooperative-sticky assignment. Keep the maximum at the 24 partitions, which KEDA's Kafka scaler enforces by default, and pre-scale on a schedule for predictable peaks.
What to remember
In this answer, we traced the oscillation to a feedback loop: each scale event triggers a rebalance, the eager protocol stops all partitions, lag jumps, and the autoscaler reacts to lag it created. We stabilised it by scaling on time to drain or sustained lag, adding larger scale-up steps, a long scale-down window and a cooldown, making rebalances cheap with KIP-848 or cooperative-sticky assignment, capping replicas at the 24 partitions, and pre-scaling for the predictable evening peak.
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.
Each scale event triggers a rebalance, and with the classic eager protocol every partition stops during it, so lag jumps, which triggers more scaling and more rebalances: the autoscaler is reacting to lag that its own actions create. To stabilise it, scale on a smoother signal such as estimated time to drain or lag sustained over several minutes, add larger scale-up steps, a scale-down stabilisation window and a cooldown, and make rebalances cheap with the KIP-848 consumer protocol or cooperative-sticky assignment. Keep the maximum at the 24 partitions, which KEDA's Kafka scaler enforces by default, and pre-scale on a schedule for predictable peaks.
Why this question is asked
Autoscaling consumers on lag sounds like the obvious design: more lag, more pods. But scaling a consumer group is not free, because every change in membership makes the group redistribute its partitions, and that pause creates lag of its own. The result can be a system that oscillates instead of settling. Interviewers ask this to see whether we can spot a feedback loop from the symptoms and fix it with control-system thinking rather than by turning one knob. In this answer, we will recap rebalances and KEDA, trace the loop, change the scaling signal, add stabilisation, make rebalances cheaper, and respect the partition limit.
1A Quick Recap: Rebalances and KEDA
In a consumer group, the partitions of a topic are divided among the consumers. When a consumer joins or leaves, the group rebalances: it computes a new assignment and moves partitions. With the classic eager protocol, every consumer gives up all its partitions at the start of a rebalance and waits for the new assignment, so the whole group stops consuming for the duration, often several seconds, longer if consumers must finish their current batch.
KEDA is a Kubernetes autoscaler driven by event sources. Its Kafka scaler reads the group's lag and sets the desired replica count to roughly the total lag divided by lagThreshold, within the configured minimum and maximum, and hands it to the Horizontal Pod Autoscaler, which applies it according to its scaling behaviour settings.
2Tracing the Feedback Loop
At the evening peak, lag rises past the threshold and KEDA adds pods. Each new pod joins the group and triggers a rebalance. During the rebalance all 24 partitions stop, while producers keep writing at peak rate, so lag jumps further. KEDA sees the higher lag and adds more pods, each causing another rebalance. Soon there are 24 pods, the group settles, and consumption catches up quickly, so lag falls. KEDA now sees low lag and scales down. Each removed pod triggers another rebalance and another stop, lag climbs again, and the cycle repeats every few minutes.
That explains the symptoms: replicas swinging between 6 and 24, lag sawtoothing between 50,000 and 900,000, and a rebalance in the logs on every scale event. The autoscaler is reacting to the lag that its own decisions produce.
- Lag passes the threshold
- KEDA adds pods
- Each new pod triggers a rebalance
- Eager rebalance stops all 24 partitionsproducers keep writing
- Lag jumps furthermore pods, more rebalances
- Group settles at 24 pods, lag drains
- Low lag: KEDA removes podseach removal rebalances again
7 steps in order. Play them, or step through them one at a time.
Every scaling decision creates the lag that triggers the next one.
3Scale on a Better Signal
Raw lag is a noisy signal here because rebalances distort it. A better question is how long would the current lag take to drain at the current consumption rate. If lag is 300,000 records and the group consumes 50,000 per second, time to drain is 6 seconds, which is fine, even though 300,000 might exceed a raw threshold. If lag is 300,000 and consumption is 5,000 per second, time to drain is a minute and rising, which is a real reason to scale.
KEDA's Kafka scaler works on lag, so a time-to-drain signal usually comes from metrics we export ourselves, for example to Prometheus, and the Prometheus scaler. Alternatively, keep the lag scaler but act only on lag sustained over several minutes, so a rebalance spike of 30 seconds does not cause a decision.
4Add Stabilisation
Control loops need damping. Through KEDA's advanced settings, which pass HPA behaviour to Kubernetes, we can shape how fast replicas change. Scale up in larger steps, for example doubling the count, so one peak needs one or two decisions instead of many single-pod additions, each with its own rebalance. Add a scale-down stabilisation window of several minutes, so the HPA uses the highest recommendation over that window and does not remove pods the moment lag dips. KEDA's cooldownPeriod controls how long it waits before scaling to its minimum after activity stops.
With these settings, the evening peak causes a scale-up to perhaps 12 and then 24 within two or three decisions, and the pods stay until the peak has clearly passed, removed two at a time. Field names are from recent KEDA and Kubernetes versions; check yours.
5Make Rebalances Cheap
Even with fewer scale events, each rebalance should cost as little as possible. With cooperative-sticky assignment in the classic protocol, consumers give up only the partitions that actually move, and the others keep consuming. Better still, the KIP-848 consumer protocol, enabled with group.protocol=consumer and generally available in Kafka 4.0, moves partition assignment to the broker-side group coordinator and reconciles each member incrementally, without a group-wide stop. Adding a pod then moves a few partitions to it while the other pods carry on.
Static membership, with group.instance.id set per pod, avoids rebalances when the same pod restarts within session.timeout.ms. It helps rolling deployments, but not scaling, where membership genuinely changes.
| Protocol | Partitions stopped | Notes |
|---|---|---|
| Classic, eager | All 24 | Whole group pauses |
| Classic, cooperative-sticky | Only those that move | Two short rounds |
KIP-848 (group.protocol=consumer) | Only those that move | Broker-side, incremental |
| Static membership | None on quick restart | Helps restarts, not scaling |
6Respect the Partition Limit and Plan for Peaks
A consumer group can use at most one consumer per partition, so with 24 partitions, pods beyond 24 would sit idle. KEDA's Kafka scaler caps replicas at the partition count by default (unless allowIdleConsumers is set), and the maxReplicaCount should match. If 24 pods are not enough at peak, the answer is more partitions, planned in advance because adding partitions changes key-to-partition mapping, not more pods.
Finally, the evening peak is predictable. A scheduled scale-up, with KEDA's cron scaler alongside the Kafka one, can bring the group to 16 pods before the peak starts, so it absorbs the rise with no rebalances at the worst moment, and lag-based scaling only handles the surprises.
7Follow-up questions
Why not simply run 24 pods all the time?
That is a valid choice if cost allows; it removes scaling rebalances entirely. Autoscaling saves money in quiet hours, so the usual compromise is a sensible minimum, a scheduled pre-scale for the known peak, and damped lag scaling for the rest.
How would you confirm the rebalance theory?
Overlay the replica count, rebalance events from consumer logs or group metrics, and lag on one time chart. If each lag jump starts at a rebalance and each rebalance follows a scale event, the loop is confirmed.
Does KIP-848 remove the need for stabilisation?
It makes each scale event much cheaper, so lag jumps far less, but rapid scaling still adds churn and cost. Keep the stabilisation windows; they are cheap insurance.
8Conclusion
In this answer, we traced the oscillation to a feedback loop: each scale event triggers a rebalance, the eager protocol stops all partitions, lag jumps, and the autoscaler reacts to lag it created. We stabilised it by scaling on time to drain or sustained lag, adding larger scale-up steps, a long scale-down window and a cooldown, making rebalances cheap with KIP-848 or cooperative-sticky assignment, capping replicas at the 24 partitions, and pre-scaling for the predictable evening peak.
Open with
This is a feedback loop between the autoscaler and rebalances. When lag passes the threshold, KEDA adds pods; each new pod joins the group and triggers a rebalance, and with the classic eager protocol all 24 partitions stop while producers keep writing at peak.
Then, if they want more
So lag jumps, KEDA adds more pods, more rebalances follow. When the group finally settles it drains quickly, KEDA scales down, and each removal rebalances again. The scaler is reacting to lag it created itself.
To stabilise it I would first change the signal: scale on estimated time to drain, lag divided by consumption rate, or on lag sustained over several minutes, not a raw threshold. Then add damping: scale up in bigger steps, a scale-down stabilisation window of around ten minutes and a cooldown, so one peak causes one or two decisions.
Next, make rebalances cheap: move to the KIP-848 consumer protocol with group.protocol=consumer, or cooperative-sticky assignment, so only the moving partitions pause. Static membership helps restarts, not scale events. Finally, keep the maximum at 24, the partition count, which KEDA's Kafka scaler enforces by default, and because the evening peak is predictable, pre-scale on a schedule before it starts.
About this answer
- Last updated
- 13 November 2026
- Versions it names
- Kafka 4.0
- 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 "During the evening peak the replica count swings between 6 and 24 every few minutes, lag...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/69072b8842/