Horizontal Pod Autoscaler (HPA)
The Horizontal Pod Autoscaler is a built-in Kubernetes controller that automatically changes the number of pod replicas in a workload to keep a metric, such as average CPU use, near a target.
Also known as: HPA, pod autoscaling
How it works
An HPA object targets a scalable workload, usually a Deployment or StatefulSet, and sets minimum and maximum replicas plus one or more metric targets. The controller checks metrics periodically, by default every 15 seconds, and calculates the replica count needed. The basic formula from the Kubernetes docs is: desired replicas equals current replicas multiplied by current metric value divided by target value, rounded up.
The stable autoscaling/v2 API supports several metric types:
- Resource metrics such as CPU and memory utilization, from the Metrics Server
- Custom metrics about pods, such as requests per second, through an adapter like the Prometheus Adapter
- External metrics from outside the cluster, such as queue length; KEDA builds on this
A behavior section tunes scale-up and scale-down speed and stabilization windows to avoid flapping.
Why it matters
HPA lets a service handle traffic peaks without being sized for the peak all day, which improves both reliability and cost. It has limits worth knowing. Utilization is calculated against the pod's resource requests, so requests must be set and sensible. It cannot scale to zero on its own. New pods need free node capacity, so it works together with a node autoscaler such as Karpenter or Cluster Autoscaler. Using HPA and the Vertical Pod Autoscaler on the same CPU or memory metric causes conflicts. See Gepton's Kubernetes service.
Horizontal Pod Autoscaler (HPA) in depth
Example: scaling an API on request rate
Example: a hypothetical search API is limited by request concurrency rather than CPU, so CPU-based scaling reacts too late. The team exposes a requests-per-second metric from the application to Prometheus and installs the Prometheus Adapter to serve it through the custom metrics API. The HPA targets an average value of requests per second per pod, chosen from load testing, with a CPU target kept as a second metric as a safety net. When several metrics are set, the HPA computes a replica count for each and uses the highest. A behavior section lets scale-up add pods quickly but makes scale-down gradual, with a stabilization window so brief dips in traffic do not remove capacity that will be needed again a minute later.
Tuning HPA behavior
- Requests first: utilization targets are percentages of pod resource requests, so inaccurate requests make every target meaningless. Rightsize before tuning.
- Stabilization windows: by default scale-down considers recommendations over a five-minute window; lengthen it for spiky traffic, shorten it to release capacity sooner.
- Scaling policies: limit how many pods or what proportion can be added or removed per period.
- Min and max replicas: the minimum should cover baseline traffic and zone redundancy; the maximum protects downstream systems such as databases from overload.
- Readiness: new pods should report ready only when they can serve, otherwise scaling adds pods that fail requests.
- Startup time: slow-starting applications need earlier scaling or a higher minimum, since the HPA cannot predict traffic.
HPA, VPA, KEDA and node autoscalers
| Component | Scales | Use with HPA? |
|---|---|---|
| Vertical Pod Autoscaler | Pod requests and limits | Not on the same CPU or memory metric; VPA in recommendation mode is a safe companion |
| KEDA | Replicas based on event sources, including to zero | KEDA creates and manages the HPA for you |
| Karpenter or Cluster Autoscaler | Nodes | Yes; HPA adds pods, the node autoscaler adds capacity for them |
How to evaluate autoscaling
Load-test with production-like traffic shapes, including sudden spikes. Measure how long it takes from a traffic increase to new pods serving, latency and error rates during the ramp, how often replicas flap up and down, and average utilization against requests. Watch the HPA's own conditions and events, which show when it is limited by max replicas or cannot read metrics.
Common mistakes
- No resource requests set, so CPU-based HPA cannot calculate utilization.
- Scaling on CPU for I/O-bound or queue-driven work.
- Maximum replicas higher than the database or downstream API can handle.
- Forgetting PodDisruptionBudgets, so scale-down and node consolidation remove too many pods at once.
The HPA is a core part of running workloads on Kubernetes efficiently and supports FinOps goals. Estimate cluster spend with our Kubernetes cost estimator. We tune autoscaling in our Kubernetes service.
Questions about Horizontal Pod Autoscaler (HPA)
Something else? Send us your question and an engineer replies with next steps.
Why is our HPA not scaling even though traffic is high?
Common causes are easy to check. The metrics pipeline may be broken, so the HPA cannot read values; its status conditions and events will say so. Resource requests may be missing or set so high that utilization never crosses the target. The workload may already be at its maximum replicas. New pods may be pending because there is no node capacity, which points to the node autoscaler. Or the chosen metric, often CPU, may not rise with the real bottleneck, such as database connections or queue length.
Can the HPA scale on business metrics like orders per minute?
Yes, through custom or external metrics. The application or a monitoring system exposes the metric, an adapter such as the Prometheus Adapter makes it available through the Kubernetes metrics APIs, and the HPA targets a value per pod or an overall value. KEDA offers a simpler route for many sources, including Prometheus queries and message queues. Choose a metric that rises before the service is overloaded, and validate the target value through load testing rather than guessing.