Skip to the page

Kubernetes: scaling and running it well · mid

The HPA and why CPU is the wrong signal

The Horizontal Pod Autoscaler scales on a metric against a target. An I/O-bound service can be drowning while CPU stays low, so scale on concurrency, queue length or latency, and fix the code that holds resources while it waits.

The notes

What you must know

  • HPA compares the average metric with the target (CPU as a percentage of the CPU request) about every 15 seconds and scales in proportion, with stabilization windows to avoid flapping.
  • A service that waits on a payment gateway and a database has low CPU. Threads are blocked, so the limit is concurrency (request threads, pool size). Requests queue and latency explodes while the HPA sees no reason to scale.
  • Anti-pattern: holding a database connection (an open transaction) while calling a slow external service. The pool runs dry although the database is idle. Move the external call outside the transaction.
  • Right now: raise minReplicas, or scale by hand for the incident.
  • Better signals: requests in flight per pod, queue depth, latency, or a custom or external metric through the metrics API (for example KEDA for queues). Scale on what is saturated.
  • CPU percentage is relative to the request, so a wrong request makes the HPA wrong. Check requests before you tune the HPA.
  • Scale-up lag: new pods need image pull, start and readiness. Keep headroom for sudden traffic with minReplicas and a startup that is fast.

The red pen

Common traps: mistake → fix

  • Mistake: HPA on CPU works for every service

    Fix: It misses I/O-bound services where threads wait.

  • Mistake: More pods fix a slow dependency

    Fix: More pods add more load to the slow dependency and more connections.

  • Mistake: HPA reacts instantly

    Fix: It checks periodically, then pods need time to start.

Easy to hard

Questions interviewers ask, easy to hard

  1. How does the HPA decide how many pods to run?
  2. Why did CPU stay low while checkout was failing?
  3. Why can holding a database connection during an external call cause an outage?
  4. Which metrics would you scale on for a queue worker and for an API?
  5. Checkout is drowning but the autoscaler did nothing. What do you do now and what do you change?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview