Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
How auto-scaling works: metrics and thresholds, target tracking, horizontal vs vertical scaling, cooldowns, warm-up delays and scaling pitfalls.
Quick answer: Auto-scaling automatically adds or removes computing capacity to match demand. An autoscaler watches a metric, such as average CPU use or requests per instance, and compares it with a target. When the metric rises above the target, it starts more instances (scaling out); when it falls, it removes some (scaling in), within limits you set. A load balancer spreads traffic across whatever instances exist. The benefits are that the service stays responsive during spikes and you stop paying for idle capacity when things are quiet. The main catch is that scaling takes time, so truly sudden spikes also need spare capacity and other defences.
Also Read: How CI/CD Pipelines Work: From Commit to Production - Tooling
Traffic is never flat. It follows daily and weekly cycles, jumps during sales or news events, and occasionally surges without warning.
You have two bad options without auto-scaling:
Auto-scaling replaces a fixed guess with a control loop.
| Horizontal (out / in) | Vertical (up / down) | |
|---|---|---|
| What changes | The number of instances | The size of an instance: more CPU and memory |
| Limit | Practically none | The largest machine available |
| Disruption | None; instances are added behind a load balancer | Often needs a restart |
| Fault tolerance | Improves: more instances | Unchanged: still one machine |
| Requirement | The application must be able to run as many copies | None |
Auto-scaling normally means horizontal scaling. Vertical scaling is still useful for systems that are hard to distribute, such as a single primary database.
Every autoscaler runs the same cycle:
A load balancer sits in front and distributes requests across healthy instances, adding new ones as they pass health checks. See what a load balancer does.
The Kubernetes Horizontal Pod Autoscaler uses a simple proportional formula:
desired replicas = ceil(current replicas × current metric / target metric)
If 4 pods are running at 90% CPU and the target is 60%:
ceil(4 × 90 / 60) = 6
It scales to 6 pods, which should bring the average back to about 60%. The Kubernetes autoscaling documentation describes this and the other autoscalers.
You also set a minimum and maximum number of instances. The minimum guarantees availability and some headroom. The maximum caps your bill and protects downstream systems.
| Policy | How it works | Good for |
|---|---|---|
| Target tracking | "Keep average CPU at 50%"; the system adds and removes capacity to hold it | The sensible default |
| Step scaling | "If CPU is above 70%, add 2; above 90%, add 5" | Fine control over the response |
| Scheduled | "Run 20 instances from 08:00 on weekdays" | Predictable patterns and known events |
| Predictive | Forecast demand from history and scale ahead of it | Regular daily or weekly cycles |
AWS's EC2 Auto Scaling documentation covers these for virtual machines.
The metric must rise and fall in proportion to load, and adding instances must bring it down.
| Metric | Suits |
|---|---|
| CPU utilisation | CPU-bound services; the common default |
| Requests per instance | Web services and APIs |
| Queue length, or the age of the oldest message | Background workers. See how message queues work |
| Concurrent connections | WebSocket and streaming servers |
| A custom business metric | Anything that reflects real work |
CPU is not always right. A service that mostly waits on a database can be overloaded while its CPU is nearly idle. For queue workers, the size of the backlog is the meaningful signal. Event-driven autoscalers such as KEDA scale on these external signals and can scale to zero when there is no work.
Memory is usually a poor trigger: many runtimes do not release memory when load drops, so the metric never comes back down.
All of this depends on good metrics; see logging, metrics and tracing.
A spike that arrives in seconds can outrun the autoscaler. The delays add up:
Altogether this is often one to several minutes. Ways to shorten it or cope with it:
If the autoscaler reacts to every wobble, it adds instances, sees the metric drop, removes them, sees it rise, and so on. This oscillation is called flapping or thrashing.
The remedies:
Removing an instance must not cut off the requests it is handling.
Auto-scaling assumes instances are interchangeable.
In a container platform, scaling happens at more than one level. See what Kubernetes does.
| Autoscaler | Adjusts |
|---|---|
| Horizontal Pod Autoscaler | The number of pods |
| Vertical Pod Autoscaler | Each pod's CPU and memory allocation |
| Cluster autoscaler | The number of nodes (machines) in the cluster |
More pods need somewhere to run. If the nodes are full, the cluster autoscaler adds a node, which takes longer.
Serverless platforms take this to its conclusion: scaling is per request and fully managed. See what serverless really means.
Horizontal scaling changes the number of instances. Vertical scaling changes the size of each instance.
Typically within one to a few minutes, covering detection, launching instances and start-up. Containers are quicker than virtual machines.
One that tracks load directly. CPU is a reasonable default for web services; queue length works better for background workers.
Partly. It will catch up, but not instantly. Headroom, caching, queues and rate limiting cover the gap.
Auto-scaling is a feedback loop: measure, compare with a target, adjust capacity. It keeps a service responsive and avoids paying for idle machines, provided the application is stateless, the metric reflects real load, and you allow for the minutes it takes new capacity to arrive. Combine it with headroom and a few protective measures, and sudden spikes become manageable.