Quick revision
The short answer
I would scale on Kafka consumer group lag, using KEDA's Kafka scaler or an external metric for the HPA, because lag measures waiting work directly while CPU rises only after pods are already overloaded. The topic's partition count caps how many consumers can do useful work, so partitions must be planned for peak. Known daily spikes get scheduled scale-up ahead of time, minimum replicas cover the first minutes of a surprise spike, and cooperative rebalancing plus stabilization windows keep the rebalances caused by scaling from pausing consumption.
What to remember
In this answer, we saw that CPU measures effort while consumer lag measures waiting work, so we scale Kafka consumers on lag with KEDA or an external metric. The partition count caps the useful number of consumers, so it is sized for peak. Scheduled scaling prepares warm pods before the known spike, a minimum replica count absorbs surprises, and cooperative or broker-side rebalancing plus stabilization windows keep scaling from pausing consumption.
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.
I would scale on Kafka consumer group lag, using KEDA's Kafka scaler or an external metric for the HPA, because lag measures waiting work directly while CPU rises only after pods are already overloaded. The topic's partition count caps how many consumers can do useful work, so partitions must be planned for peak. Known daily spikes get scheduled scale-up ahead of time, minimum replicas cover the first minutes of a surprise spike, and cooperative rebalancing plus stabilization windows keep the rebalances caused by scaling from pausing consumption.
Why this question is asked
Kafka consumers are a common place where CPU-based autoscaling disappoints. Orders pile up for ten minutes before the HPA notices, and by the time new pods are warm the spike is half over. Interviewers ask this design question because a good answer combines Kafka mechanics (partitions, consumer groups, rebalances) with Kubernetes autoscaling and JVM startup. In this answer, we will see why CPU reacts late, design scaling on lag, respect the partition limit, add scheduled scaling and a sensible minimum, and make the rebalances that every scale event causes as cheap as possible.
1A Quick Recap of Consumer Groups and Lag
A Kafka topic is split into partitions. Consumers that share a group.id form a consumer group, and Kafka assigns each partition to exactly one consumer in the group, so the partitions are the unit of parallel work. Each consumer commits an offset, the position up to which it has processed.
Lag is the difference between the latest offset written to a partition and the offset the group has committed. If producers have written up to offset 1,050,000 and the group has committed 1,000,000, the lag is 50,000 messages. Lag is literally the queue of work waiting to be done.
2Why CPU-Based Scaling Reacts Too Late
A consumer pod's CPU is bounded by how fast it can process. When a spike arrives, each pod keeps processing at its usual rate, maybe a little faster, so CPU climbs slowly while lag explodes. The HPA sees 80 percent CPU only once pods are already saturated, then adds pods that need 30 to 90 seconds to start the JVM and Spring and to join the group, and longer to be warm. Worse, if consumers wait on a database, CPU may never rise at all while lag grows. CPU measures effort, not backlog, and the backlog is what customers feel.
3Scaling on Lag With KEDA
KEDA (Kubernetes Event-driven Autoscaling) is an operator that reads event-source metrics and drives an HPA for us. Its Kafka scaler reads consumer group lag and computes replicas so that each replica has about lagThreshold messages of backlog. The same could be built with a Prometheus exporter for lag and an external metric on a plain HPA, but KEDA packages it neatly.
Notice the cron trigger next to the Kafka trigger. KEDA takes the highest result, so during the lunch window we never drop below 16 replicas, and lag can still push us higher.
4The Partition Count Caps Useful Replicas
Because each partition goes to one consumer in the group, a group with more consumers than partitions leaves the extra consumers idle. With 24 partitions, the 25th pod gets nothing, costs money and still takes part in every rebalance. KEDA's Kafka scaler by default does not scale beyond the number of partitions; check the allowIdleConsumers setting in your version.
So the partition count is a capacity decision that must be made for peak. If one pod processes about 800 orders per second and the peak is 15,000 per second, we need at least 19 consumers, so 24 partitions gives headroom. Adding partitions later is possible but changes which partition each key maps to, which breaks per-key ordering during the change, so it is best decided up front. Concurrency inside a pod, for example concurrency=3 on a Spring Kafka listener container, also counts: three listener threads use three partitions.
| Input | Value | Result |
|---|---|---|
| Peak rate | 15,000 orders/s | |
| Rate per consumer thread | about 800/s | 19 consumers needed |
| Partitions chosen | 24 | headroom of 25% |
| Max useful replicas | 24 pods x 1 thread | or 8 pods x 3 threads |
5Scheduled Scaling and a Sensible Minimum
Daily spikes are predictable, which is a gift. Scaling up 20 to 30 minutes before the known peak means pods are started, have joined the group and are warm when the orders arrive. The cron trigger above does this, and the window can be generated from last month's traffic. For unexpected spikes, minReplicaCount is our shock absorber: four warm pods can absorb the first minute of a surge while new pods start. Setting the minimum to zero saves money overnight but means the first message of the day waits for a JVM to start, which is usually not acceptable for orders.
6Making Rebalances Cheap
Every time a consumer joins or leaves, the group rebalances: partitions are reassigned. With the old eager protocol, every consumer gives up all its partitions and stops until the new assignment is done, so frequent scaling causes repeated pauses. Two things help. The CooperativeStickyAssignor makes rebalances incremental: consumers keep the partitions that do not move and only the moved ones pause. In Kafka 4, the newer consumer group protocol from KIP-848, enabled with group.protocol=consumer, moves assignment to the broker and is incremental by design; check that your client and broker versions support it.
The second is to scale less often. A scale-down stabilization window of 5 to 10 minutes stops KEDA from removing pods during a short dip in lag, and scaling up in larger steps means fewer rebalances in total.
We also set a sensible graceful shutdown so a pod being removed commits its offsets and leaves the group cleanly, rather than waiting for a session timeout.
7Follow-up questions
Why not just keep enough pods for peak all day?
It works, but the service then pays for peak capacity 24 hours a day for a spike that lasts a few hours. Lag-based scaling with a scheduled window and a modest minimum usually keeps lag within target at a fraction of the cost. For a small service the simple answer may be fine; the design matters when the replica count is large.
What lag threshold should we choose?
Work back from the latency target. If orders must be processed within 30 seconds and a pod handles 800 per second, each pod can carry about 24,000 messages of backlog in theory; we pick something well below, say 5,000, to leave room for startup time.
Does lag-based scaling work if processing is slow because of the database?
Adding consumers when the database is the bottleneck increases load on the database and may make things worse. The scaling design must include the downstream: cap maxReplicaCount at what the database can take, or scale the database side too.
8Conclusion
In this answer, we saw that CPU measures effort while consumer lag measures waiting work, so we scale Kafka consumers on lag with KEDA or an external metric. The partition count caps the useful number of consumers, so it is sized for peak. Scheduled scaling prepares warm pods before the known spike, a minimum replica count absorbs surprises, and cooperative or broker-side rebalancing plus stabilization windows keep scaling from pausing consumption.
Open with
I would stop scaling on CPU and scale on consumer group lag, because lag is the actual backlog of orders, while CPU only rises once pods are already saturated, and new JVM pods then need a minute or more to be useful.
Then, if they want more
Concretely I would use KEDA with the Kafka scaler, with a lag threshold per replica derived from our processing latency target, for example 5,000 messages per pod.
The maximum replica count must not exceed the partition count, since extra consumers in a group sit idle, so I would size partitions for peak: if a pod does 800 orders a second and peak is 15,000, I want 24 partitions.
Because the spike is daily and predictable, I would add a cron trigger that scales up about half an hour before the peak, so pods are warm when the orders arrive, and I would keep a minimum of a few replicas to absorb the first minutes of an unexpected spike.
Finally, every scale event triggers a rebalance, so I would use the cooperative sticky assignor, or the new broker-side protocol in Kafka 4, plus a longer scale-down stabilization window, so scaling does not keep pausing consumption. And I would make sure the database can take the peak consumer count.
About this answer
- Last updated
- 12 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
- Kubernetes 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 "A Spring Boot service consumes orders from Kafka with large daily spikes. CPU-based HPA...": https://scrivio-interview-library.netlify.app/spring-boot/containers-and-kubernetes/5010653df2/