How Kubernetes HPA Was Sinking Us: Scaling on the Wrong Metric

The first Friday of March, 12:30 p.m. The sweetest peak of the day had begun on the food side, and order creation requests had climbed to 2,400 per second. Then the dashboards started turning red: p99 latency went from 300 milliseconds to 4 seconds, and the timeout rate hit 6 percent. The on-call engineer's first message put me at ease: "HPA is working, pod count went from 12 to 38." His second message is the reason this post exists: "The more pods we add, the worse it gets." Autoscaling was fanning the flames. That day we learned that HPA's question isn't "how many pods do we need?"; HPA only chases the metric you give it, and if you gave it the wrong metric, chasing it will carry you right over the cliff.

CPU at 45 Percent, the Service Dying: How?

Our order service's HPA had the classic setup: a target CPU utilization of 65 percent. During the crisis, average CPU showed 45 percent — so as far as HPA was concerned everything was fine, and scaling was merely tracking the traffic increase. But the service was choking. Our first wrong hypothesis was the database: "The PostgreSQL connection pool is full, the pods are waiting." The pool metrics really were maxed out, but that was a consequence, not the cause. The second hypothesis was Kafka; lag was increasing, but that too was a consequence. The real clue came when we dug deep into a single pod: container_cpu_cfs_throttled_periods_total. Even with average CPU at 45 percent, our pods were being throttled in 70 percent of CFS periods.

Here's the mechanism: in Linux CFS, CPU limits are enforced in 100-millisecond windows. If your limit is 1 core, you're entitled to 100 ms of CPU in each 100 ms window. Our Java service made short but intense CPU bursts per request (JSON processing, price calculation), and with GC and JIT piling on top, the per-window allowance ran out within a few milliseconds and threads were frozen for the rest of the window. Average CPU looks low, because the average also spreads across the milliseconds spent frozen. In other words, the metric HPA was looking at was precisely the metric that kept the problem invisible. On top of that, every new pod startup created extra CPU bursts from JIT warm-up and class loading; new pods got throttled even worse than the old ones during their first two minutes, leaving requests hanging even though they had passed readiness. As the pod count grew, the total warm-up load grew, and the load balancer kept sending requests to the slow pods. Scaling was, quite literally, making the fire bigger.

CPU allowance runs out quickly in CFS windows and threads freeze, but average CPU stays low; HPA cannot see the problem.
The metric HPA was watching was the very metric that kept the problem from showing.

You Can't Scale Right on the Wrong Metric

The emergency response came down to two moves: we temporarily removed the CPU limits (the requests stayed) and raised the minimum replica count ahead of the peak. Throttling dropped instantly, and p99 came down to 600 milliseconds within 90 seconds. But "we removed the limits, done" wouldn't have been worth writing this post; the real work was rebuilding our scaling philosophy from scratch.

First decision: the metric HPA chases should be the one closest to what the customer feels. CPU is a proxy metric, and for our workload it was a bad proxy. We deployed Prometheus Adapter and tied HPA to a custom metric: requests per second (RPS) per pod, with request queue wait time alongside it as a second signal. Through load tests we measured the RPS each pod could carry in a healthy state — in our configuration, at 85 requests/sec per pod p99 was still under 250 ms, and at 110 it broke — and we set the HPA target at 70 requests/sec: 35 percent below the breaking point, warm-up margin included. For services that consume from Kafka, the scaling metric became consumer lag; RPS means nothing there — what matters is the backlog of work. Today only three of our 14 critical services still scale on CPU — all three are latency-insensitive batch services doing genuinely CPU-bound work.

Instead of CPU, requests per pod and queue wait time are measured; services that consume from Kafka scale on consumer lag.
In load tests, p99 was under 250 ms at 85 requests/sec per pod and broke at 110.

The second decision was the limits policy, and it became the most hotly debated topic on the team. On latency-sensitive services we don't set CPU limits; we size requests realistically (from load tests, adding headroom above p95 usage) and manage the risk of node saturation not with limits but with accurate requests + a separate node pool + PodDisruptionBudget. The objection "a pod without limits will crush its neighbors" is legitimate; our answer is that crushing is only possible if the requests are lying. If the requests are honest, the scheduler won't overcommit in the first place. On the Java side we also reduced the startup pain: with AppCDS and tiered JIT settings, warm-up time dropped from 110 seconds to 35 seconds, and we changed the readiness probe from "returns HTTP 200" to "warm-up round completed" — the pod processes 200 synthetic requests on its own before it takes its first real request.

We didn't forget that the custom metric pipeline is itself a point of failure. If the Prometheus Adapter feeding HPA goes down, the scaler goes blind; in that case Kubernetes keeps the current replica count, which is a disaster if it catches you at a low count right before a peak. We took two precautions: the adapter and Prometheus run in a separate node pool in the critical infrastructure class, with their own alerts; and the minimum replica count of every critical service never drops below the level that "can handle the lunchtime peak while the metrics pipeline is dead." We trust automation, but trust needs a backup, too.

Scaling Speed Is a Metric Too

The third lesson lies somewhere nobody thinks about on day one: HPA's timing. With the default behavior, it took us an average of 3 minutes from the moment the metric threshold was crossed until HPA decided to scale and the new pod actually started taking traffic — metric collection interval, stabilization window, image pull, warm-up. Our lunchtime peak, meanwhile, ramps up steeply within 5 minutes. So at best, we were catching up in the middle of the peak. The solution came in three parts: using the behavior field, we made scale-up aggressive (allowing a 100 percent increase within a 15-second window), slowed down scale-down (a 5-minute stabilization window — it put an end to the flapping), and for predictable peaks we added the most honest tool for the job: scheduled scaling. Ten minutes before the lunch and dinner peaks, the minimum replica count rises automatically. Instead of expecting a reactive system to make predictions, we tell it the future we already know. It's not glamorous; it works.

Default HPA was catching up in the middle of the peak; aggressive scale-up, slow scale-down and scheduled scaling closed the gap.
We tell the scaler the future we already know, ahead of time.

Summary of the results: under a traffic profile similar to that Friday's (the following month's campaign Friday, 2,900 requests per second), p99 latency held steady at 310 milliseconds, the timeout rate stayed below 0.2 percent, and the pod count was 44 throughout the peak. The throttling rate on critical services is now a line glued to zero, and there's an alert sitting above that line: any deployment whose throttle rate exceeds 5 percent is automatically flagged as suspect. We saw a side effect on the monthly cloud bill, too: scaling on the right metric reduced the unnecessary "fear replicas," and infrastructure cost per order fell 17 percent.

The Mirror of Automation

The general lesson I took from the HPA incident is this: automation is a mirror of the world model you give it. The sentence "add pods when CPU exceeds 65 percent" is really a claim that "our bottleneck is CPU, and average CPU represents health" — and for us, that claim was wrong. If your system gets worse as it scales, question the relationship between the metric your scaler watches and what your customer feels; the answer is usually in the throttling counters, the queue times or the lag. If you have doubts about your own HPA setup — especially if the sentence "our pod count keeps going up but latency isn't coming down" sounds familiar — write to me here; I'll share our load test template and our HPA behavior settings. A two-hour look clears up a lot.

My favorite moment in the war room recording from that Friday is our senior SRE's calm remark: "The system is obeying us; the problem is that we don't know what we told it to do." The whole theory of autoscaling is a footnote to that one sentence.