The Horizontal Pod Autoscaler (HPA) is the built-in Kubernetes controller that automatically changes the number of Pod replicas in a Deployment, StatefulSet or other scalable workload so that observed load stays close to a target metric value. Instead of giving existing Pods more CPU or memory, it scales horizontally: it adds identical replicas when demand rises and removes them when demand falls.
In cloud-native infrastructure, the HPA is the first line of elastic capacity. It reacts to measured utilization within seconds to minutes, keeps response times stable during traffic peaks and lowers cost during quiet periods. It depends on resource requests, a working metrics pipeline and an application that can run as several interchangeable copies.
How the HPA control loop calculates replicas
The HPA works as a periodic control loop inside the kube-controller-manager, which by default evaluates every HorizontalPodAutoscaler object every 15 seconds. In each cycle it reads current metric values, compares them with the targets defined in the object and writes a new replica count to the scale subresource of the target workload. The Deployment or StatefulSet controller then creates or deletes Pods.
The core formula is desiredReplicas = ceil(currentReplicas × currentValue / targetValue). If four replicas run at 90% average CPU utilization against a 60% target, the result is ceil(4 × 90 / 60) = 6 replicas. A tolerance of 10% by default prevents changes when the ratio is close to 1.0, which avoids constant small adjustments. When several metrics are defined, the HPA computes a proposal for each and applies the highest replica count.
Metric values come from aggregated APIs. Resource metrics such as CPU and memory are served by the metrics.k8s.io API, usually provided by Metrics Server. Custom and external metrics come from custom.metrics.k8s.io and external.metrics.k8s.io, typically implemented by an adapter for Prometheus or a cloud monitoring service. The table below summarizes the moving parts.
| Component | Role in the loop | Typical implementation |
|---|---|---|
| HPA controller | Computes desired replicas each cycle | kube-controller-manager |
| HorizontalPodAutoscaler object | Declares target, metrics and bounds | autoscaling/v2 manifest |
| Resource metrics API | Supplies CPU and memory usage | Metrics Server |
| Custom metrics API | Supplies per-Pod or per-object metrics | Prometheus Adapter |
| External metrics API | Supplies metrics from outside the cluster | Cloud monitoring adapter, KEDA |
| Scale subresource | Receives the new replica count | Deployment, StatefulSet, ReplicaSet |
Metric types and target definitions
The autoscaling/v2 API supports four metric source types, and each one answers a different scaling question. Utilization targets are expressed as a percentage of the Pod resource request, not of node capacity, so a workload without CPU requests cannot be scaled on CPU utilization at all. The following table compares the options.
| Metric type | What it measures | Typical target | Best suited for |
|---|---|---|---|
| Resource | CPU or memory of each Pod | Utilization 50-80% | Stateless web and API services |
| ContainerResource | Usage of one named container | Utilization 60-75% | Pods with heavy sidecars |
| Pods | Custom per-Pod metric | Average value, such as 100 requests/s | Request-driven services |
| Object | Metric of another Kubernetes object | Value, such as Ingress requests/s | Traffic measured at the edge |
| External | Metric outside the cluster | Queue length per replica | Queue consumers and workers |
Each HPA also defines minReplicas and maxReplicas. The minimum protects availability and absorbs sudden spikes before new Pods become ready; the maximum caps cost and protects downstream systems such as databases. Choosing a target such as 60-70% CPU utilization leaves headroom for the time that new replicas need to start, pass readiness probes and receive traffic.
Scaling behavior, stabilization and policies
The behavior field controls how fast the HPA scales in each direction. A stabilization window makes the controller consider the recommendations of a recent period and choose the most conservative one. By default, scale-down uses a 300-second window, so a short dip in traffic does not remove replicas that will be needed again a minute later, while scale-up has no window and reacts immediately.
Scaling policies limit the size of each step, either as a number of Pods or a percentage per period. The default scale-up policy allows the larger of doubling the replica count or adding four Pods every 15 seconds. Tuning these settings is the main defense against flapping, the oscillation between scale-up and scale-down that wastes resources and disturbs caches and connection pools.
| Setting | Default | Effect of increasing it |
|---|---|---|
| Scale-down stabilization window | 300 s | Fewer premature removals, slower cost savings |
| Scale-up stabilization window | 0 s | Less reaction to brief spikes, slower response |
| Scale-up percent policy | 100% per 15 s | Faster growth during sudden surges |
| Scale-down percent policy | 100% per 15 s | Faster shrinking after load drops |
| Tolerance | 0.1 | Fewer small adjustments around the target |
How the HPA compares with other autoscalers
The HPA is one layer in a multi-level autoscaling stack, and it rarely works alone. It changes how many Pods exist, while other components decide how large each Pod is and whether the cluster has enough nodes to schedule them. Confusing these layers is a frequent cause of failed scaling during incidents.
| Autoscaler | What it changes | Trigger | Main limitation |
|---|---|---|---|
| Horizontal Pod Autoscaler | Number of Pod replicas | Pod or external metrics | Needs horizontally scalable workloads |
| Vertical Pod Autoscaler | CPU and memory requests | Historical usage | Conflicts with HPA on the same resource |
| Cluster Autoscaler | Number of nodes | Pending unschedulable Pods | Node startup takes minutes |
| KEDA | Replicas, including to zero | Event sources such as queues | Additional operator to run |
A typical production pattern combines the HPA for replicas with the Cluster Autoscaler or a node provisioner for capacity. Running the Vertical Pod Autoscaler on the same CPU or memory metric as the HPA creates a feedback conflict, because changing requests also changes the utilization percentage the HPA sees.
Common mistakes and limitations
Most HPA problems stem from missing prerequisites or unsuitable metrics, not from the controller itself. CPU utilization is a weak signal for I/O-bound or latency-sensitive services, and memory rarely falls after load drops because many runtimes keep allocated heap. The list below covers the issues practitioners meet most often.
- Missing resource requests: the HPA reports an unknown metric and never scales.
- Fixed replicas in the Deployment manifest: GitOps tools or
kubectl applyreset the count the HPA set; omit the replicas field when an HPA manages the workload. - Slow startup: Pods that need 90 seconds to warm up make scale-up lag behind demand; use readiness probes and lower targets.
- Memory-based scale-down: memory stays high after traffic falls, so replicas never decrease.
- Downstream limits: scaling to 50 replicas can exhaust database connections; set maxReplicas from real capacity.
The HPA also cannot scale to zero with standard settings, because minReplicas must be at least one unless an alpha feature gate is enabled. Event-driven workloads that idle for hours are therefore often handled by KEDA, which manages an HPA internally and adds the zero-to-one activation step.
The simple explanation
Imagine a supermarket that opens more checkout lanes when queues grow and closes them when the store is quiet. The Horizontal Pod Autoscaler does the same for an application. It watches how busy the running copies are and adds or removes copies automatically, so customers wait less during rush hour and the company pays less at night.
It does not make any single copy stronger. It only changes how many copies are working, within a minimum and maximum the team sets in advance.
How it is used in practice
An online ticket seller runs its checkout API as a Deployment with CPU requests of 500 millicores per Pod. The team sets an HPA with minReplicas of 4, maxReplicas of 40 and a target of 65% average CPU utilization, plus a second metric of 120 requests per second per Pod from a Prometheus adapter. The scale-down stabilization window is raised to 600 seconds because ticket releases come in waves.
When a popular concert goes on sale, traffic jumps tenfold within two minutes. The HPA doubles replicas in successive cycles, reaching 32 Pods, while the Cluster Autoscaler adds six nodes for the pending Pods. Median checkout latency stays under 300 milliseconds throughout the sale. An hour later, as traffic falls, the HPA gradually returns to four replicas and the extra nodes are released, so the peak capacity is paid for only while it is needed.