Video in progress
Your HPA is configured on CPU, but when traffic grows the service slows down and almost no pods get added. What could be the cause, and what metric would you scale on?
If the service spends its time waiting on I/O (downstream calls, the DB) rather than burning CPU, CPU stays low and the HPA sees nothing, while requests pile up in queues. I scale such a service on a load metric like concurrent requests per pod, RPS or Kafka lag, and I factor in how long a pod takes to become ready.
📖 Answer
A typical I/O-bound Spring service: every request takes 300 ms, and 280 ms of that is waiting on other services and the DB. When traffic doubles, the Tomcat threads run out and latency goes up to 3 seconds, while CPU sits at 35%. With an HPA target of 70%, it does nothing.
The opposite case exists too: if the CPU requests value is tiny (say 200m), JIT compilation at startup pushes utilization to 400% and the HPA adds pods you don't need on every deploy.
Better signals:
- for an HTTP service: concurrent (in-flight) requests per pod or RPS, exposed as a custom metric through Prometheus Adapter;
- for a Kafka consumer: consumer lag (KEDA's Kafka scaler does this), but pods beyond the partition count are useless;
- for queue workers: queue length.
metrics:
- type: Pods
pods:
metric:
name: http_requests_inflight # a metric you emit yourself
target:
type: AverageValue
averageValue: "40"
Account for time too: metric collection (15–30 s), the HPA decision, pod scheduling, image pull, Spring startup (20–40 s), JIT warm-up. A new pod takes 1–2 minutes to carry real load. For sharp spikes that is too late, so you set minReplicas with some headroom, make behavior.scaleUp faster and scale-down slower (stabilizationWindowSeconds).
And remember that autoscaling also multiplies the limit further down the chain. 10 pods × 20 DB connections = 200, while the DB's max_connections may be 150.
Sign up to mark studied questions and save your progress.