# Horizontal pod autoscaling

## What it is

Horizontal Pod autoscaling adjusts how many replicas a Kubernetes workload runs.
Its controller checks a measured signal and changes the target's desired replica
count within configured bounds. A workload that receives more traffic may get
additional Pods. As demand falls, the controller can return capacity to the
cluster. Scaling changes the number of running instances, while the resources
requested by each instance are set elsewhere.

An autoscaler needs a metric, a target and a suitable workload. A common setup
uses average CPU utilisation relative to the requested CPU for the Pods. Other
setups use a custom application metric or an external measurement, such as
demand waiting in a queue. The controller checks the signal periodically. When
the measured value is above its target, it can calculate a larger desired
population. Tolerance, missing measurements, readiness and scaling behaviour
settings influence what change is actually made.

Choose the signal with the work in mind. CPU use can be a useful measure for
compute-bound processing. A worker spending most of its time waiting for an
upstream service can have low CPU use while its queue grows. For that service, a
measure of waiting work or age may better reflect what users experience. A
signal should tell the controller something useful about what an additional
replica can accomplish.

## Why it matters

Traffic can arrive faster than a team can adjust replicas manually. An overnight
import can add a backlog to a worker service. A public event can produce several
times the normal request rate for a short period. Scaling can help while those
requests are arriving, and an appropriately sized baseline can still handle the
work present before replacements become ready.

The time required to add capacity matters. A new Pod needs scheduling, startup
and readiness. The cluster may need more node capacity, which has its own delay.
A metric might report a rapidly growing queue while the expected new workers
remain pending. An autoscaler can react to the signal and still leave customers
waiting if the deployment takes too long to add usable capacity. Establish which
delay dominates a burst and measure the entire path.

Scaling down is another control problem. Removing workers as soon as one sample
looks quiet can cause repeated churn and force the application to rebuild caches
or reconnect to a service. A stabilisation policy can preserve capacity while
short dips pass. A longer delay can cost more if it leaves many replicas idle.
The suitable behaviour comes from observed work patterns and the cost of losing
capacity while another burst arrives.

## What it looks like in practice

Imagine a worker service that converts incoming documents. Measure how long the
oldest queued request has waited and how many jobs workers can complete per
minute. Select a target that fits the product's waiting-time goal. Set enough
minimum capacity to absorb the normal arrival pattern during startup. Use a load
test with a gradual increase and a sudden spike. Record arrival rate, queue
depth, desired replicas, running replicas and usable workers.

A simple observed example helps establish whether the policy is doing what its
owner expects. With three replicas, an average CPU utilisation of 75 percent and
a 50 percent target, the basic scaling ratio suggests five replicas after
rounding up. Configured bounds and behaviour policies can limit that change. Log
the input and the next desired population, then follow how many workers actually
become ready. This example applies when the metric samples describe the expected
pods and the controller has enough information to use them.

A second test can saturate an upstream dependency that all workers share. Extra
Pods help only when they can do more useful work. If every worker waits on the
same fixed connection limit, scaling can amplify contention. Try a bounded
worker concurrency limit and observe whether added workers increase completion
rate. Use peak results and cost to decide where more replicas help and where the
shared service needs attention.

Engineer's
[multi-cloud container operations case study](/portfolio/ran-20-docker-apps-on-google-cloud-and-aws/)
documents deploying and monitoring applications across clouds and using
Kubernetes to diagnose orchestration issues. That monitoring discipline is
useful when developing a scaling policy. The illustrative worker test follows
both the controller's desired count and the throughput it actually delivers. It
gives operators a way to tell whether a deployment, the cluster's capacity or
the application itself is limiting recovery.

## Where it goes wrong

An autoscaler may have too little information when some Pods have no usable
metric samples. Check its conditions and the health of the metrics source. When
using resource utilisation, give the relevant containers appropriate requests. A
CPU percentage is interpreted in relation to a request and may be unavailable
when that relationship is missing. Review whether each contributing Pod has a
useful signal during startup as well as during steady traffic.

It is easy to choose a target without considering the consequences of repeated
rescheduling. Inspect what happens to in-flight work when replicas are removed,
especially if a job can be retried. Give the worker time to drain or return its
task to a queue. Add an explicit floor for the times when a service has to
accept bursts promptly and a ceiling that limits demand on shared resources.

Finally, test operation when the controller cannot get the desired number of
ready replicas. Verify that the backlog and pending workload remain observable
and that overload follows an intentional policy. Revisit the metric and bounds
when the product's workloads change. A successful scaling setup explains what
causes the desired count to change and shows the resulting effect on a real
request or job completion time.

## Further reading

- [Kubernetes Horizontal Pod Autoscaling](https://kubernetes.io/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/).
  Explains metrics, the control loop and scaling behaviour.
- [Kubernetes autoscaling walkthrough](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/).
  Shows a scale-up and scale-down exercise.
- [The HorizontalPodAutoscaler v2 API](https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/horizontal-pod-autoscaler-v2/).
  Describes metrics, conditions and behaviour settings.

<https://engineer.company/glossary/horizontal-pod-autoscaling/>
