Autoscaling and capacity planning
Autoscaling handles today's peaks, capacity planning handles next quarter's. How the scaling formula behaves, why it lags, and how to forecast the ceiling.
Metrics with PrometheusKubernetes core objectsCompute, storage and networking building blocks
Autoscaling and capacity planning
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Autoscaling reacts to load over minutes. Capacity planning makes sure that what the autoscaler can reach, and what the account, database and budget allow, is still enough months from now.
- Target tracking scales replicas in proportion to how far the metric is from its target. In Kubernetes that is desired = ceil(current × currentMetric / targetMetric), with a 10% tolerance band.
- Scale-up is fast and scale-down is deliberately slow. A stabilization window, five minutes by default in Kubernetes, stops the replica count from flapping.
- Autoscaling always lags, because metrics are averaged and new instances need time to start. Set the target with enough headroom to absorb the ramp.
- Plan capacity against the real limits, including losing a zone, the autoscaler's maximum, quotas and the database, not against average load.
The problem: traffic doesn’t hold still
Traffic to a shop doubles at lunchtime, triples on a sale day and drops to almost nothing at 4 a.m. If you provision for the peak, you pay for idle machines most of the day. If you provision for the average, the peak takes you down, as happened in the postmortem lesson.
There are two tools for this, on two time scales:
- Autoscaling adds and removes capacity automatically, within minutes, as load changes.
- Capacity planning makes sure the ceiling the autoscaler can reach, and everything behind it, is still high enough in three or six months.
Teams that have autoscaling often assume they no longer need the second. The autoscaler can only scale to its configured maximum, within your account quotas, in front of a database that does not autoscale.
How autoscalers decide
Every cloud scales groups of VMs: AWS Auto Scaling groups, Azure Virtual Machine Scale Sets and Google managed instance groups. In Kubernetes, the Horizontal Pod Autoscaler (HPA) scales pods, and a cluster autoscaler or Karpenter adds nodes when pods don’t fit. Most of them offer target tracking: you name a metric and a target, and the scaler keeps the metric near it.
The Kubernetes documentation gives the HPA’s rule exactly:
desiredReplicas = ceil[ currentReplicas × ( currentMetricValue / desiredMetricValue ) ]
Two details from the same page matter in practice:
- Tolerance. If the ratio is within 0.1 of 1.0, the HPA does nothing, which stops constant small adjustments.
- Downscale stabilization. Before scaling down, the HPA takes the highest recommendation from the last 300 seconds. Scale-up acts at once and scale-down waits.
Scaling on CPU works when CPU tracks load. For queue workers, scale on queue depth instead. KEDA does this in Kubernetes, and cloud scalers accept custom metrics. Vertical scaling, giving each instance more CPU or memory, is the other direction and usually needs a restart.
A worked example: watching the formula work
I simulated that rule in Python, minute by minute. Each pod handles 100 requests a second at full CPU, the target is 60% CPU, the replica limits are 2 to 20, and the tolerance and window are the documented defaults. Traffic ramps from 120 to 720 requests a second over ten minutes, holds, drops to 300, then 120. I printed a line whenever replicas changed, and every ten minutes:
$ python3 hpa.py
min 0 120 rps util 60% raw 2 -> replicas 2
min 10 180 rps util 90% raw 3 -> replicas 3
min 11 240 rps util 80% raw 4 -> replicas 4
min 12 300 rps util 75% raw 5 -> replicas 5
min 13 360 rps util 72% raw 6 -> replicas 6
min 14 420 rps util 70% raw 7 -> replicas 7
min 15 480 rps util 69% raw 9 -> replicas 9
min 17 600 rps util 67% raw 10 -> replicas 10
min 18 660 rps util 66% raw 11 -> replicas 11
min 20 720 rps util 65% raw 11 -> replicas 11
min 30 300 rps util 27% raw 5 -> replicas 11
min 34 300 rps util 27% raw 5 -> replicas 5
min 40 300 rps util 60% raw 5 -> replicas 5
min 49 120 rps util 24% raw 2 -> replicas 2
min 50 120 rps util 60% raw 2 -> replicas 2
Three behaviours show up.
- It lags. Through the whole ramp, utilization sits above the 60% target, at 90% at the first step. Each decision reacts to load that has already arrived. My simulation is kinder than reality: real pods take time to start and pass readiness, and metrics are averaged over a window, so the real lag is longer. If your instances take two minutes to boot, a 60% target is the headroom that carries you through those two minutes.
- Tolerance leaves it slightly hot. At 720 requests a second it settled on 11 pods at 65%, because 65/60 is inside the 10% band. Fine, but it shows that the target is not exact.
- Scale-down waits. When traffic fell at minute 30, the raw recommendation dropped to 5 at once, but replicas held at 11 until the five-minute window had passed.
The real HPA also limits how fast it scales (by default, at most double the pods or add four per 15 seconds, whichever is more) and treats pods with missing metrics conservatively. My simulation leaves both out. The HPA manifest itself is a HorizontalPodAutoscaler object pointing at a Deployment, which Kubernetes core objects introduces. I had no cluster to apply one to.
Capacity planning: where is the ceiling?
Autoscaling hides growth until it hits a wall. Capacity planning finds the wall first. The method fits on a page:
- Measure peak demand over time, using weekly peaks rather than averages.
- Fit a trend, and add known events such as a launch, a sale or a new region.
- Work out the real ceiling: the autoscaler’s maximum at the target utilization, with one zone lost, and every shared limit behind it (database connections, account quotas, licences).
- Act with enough lead time to raise the ceiling.
I fitted a straight line to twelve weeks of made-up weekly peaks and checked it against the simulation’s maximum of 20 pods at 60%:
$ python3 forecast.py
growth 13.1 rps/week (2.3% of current peak per week)
autoscaler max at 60% target 1200 rps reached in ~50 weeks
same, with one zone lost 800 rps reached in ~19 weeks
Fifty weeks looks like plenty. Then you remember that with three zones, losing one leaves two-thirds of the capacity, and the real deadline is under five months. A straight line is the simplest forecast. Growth is often faster, and seasonal peaks sit on top of the trend. Redo it monthly, and add back-of-the-envelope estimates for launches the history can’t predict.
About the video
“Kubernetes Autoscaling: HPA vs. VPA vs. Keda vs. CA vs. Karpenter vs. Fargate” is from Anton Putra, published November 2023, 14 minutes 37 seconds. Its chapters cover the HPA, the HPA on custom Prometheus metrics, the VPA, KEDA, the cluster autoscaler, Karpenter and serverless Kubernetes (Fargate and Autopilot). That is a full survey of the autoscaling half of this lesson, in Kubernetes terms. It does not cover capacity planning, and VM scaling groups only by comparison. English is judged from the title, description and channel. Digital Cloud Training’s video covers EC2 Auto Scaling groups. The SREcon talk is a four-step capacity planning method from an SRE at Hosted Graphite.
Where this fits
That finishes observability and operations. The last group is about protecting the system: its secrets, its supply chain and its data, starting with secrets management for infrastructure.
Check yourself
4 pods run at 90% average CPU with a 60% target. How many pods does the HPA ask for?
ceil(4 × 0.90 / 0.60) = ceil(6) = 6.
Average CPU is 65% against a 60% target. Why does the HPA do nothing?
The ratio is about 1.08, inside the default 10% tolerance, so no scaling happens.
Why does scaling down wait for a window while scaling up does not?
Removing capacity too early is risky if load comes back, and flapping wastes startup work. Under-capacity hurts users at once, so scale-up acts immediately.
Why plan capacity with one zone lost?
If a zone fails at peak, the remaining zones must carry all the traffic. With three zones that means only two-thirds of your capacity is really usable.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.