Why you need it
Visitors do not come at a steady rate. A sale or a mention on TV can bring three times as many in minutes, and the code still says one copy. Set the number high enough for the rush and you pay for empty copies the rest of the day.
Think of it like this
A supermarket manager who watches the queues. When lines get long, she opens more tills. When the shop empties out, she sends staff home. Nobody had to plan the rush the day before.
Where you’ll find it
- Kubernetes: The Horizontal Pod Autoscaler changes the number of copies to keep CPU or another number near a target.
- AWS: Target tracking in EC2 Auto Scaling adds and removes servers to hold, say, 50% CPU.
- Google Cloud and KEDA: The Compute Engine autoscaler does the same for groups of servers, and KEDA scales on things like queue length.
What to know first
Reconciliation. A controller keeps the running copies equal to the number in the code. See Desired state and reconciliation.
Metrics. Numbers measured all the time, like how busy each copy's CPU is. See Metrics and alerts.
Target. The busyness you want each copy to sit at, for example 50% CPU, leaving room for surprises.
How it works
- Watch how busy the copies are. The autoscaler reads a metric every few seconds, often the average CPU across copies. This is the autoscaler in the animation.
- Work out the new number. If copies are three times busier than the target, you need three times as many.New copies = current copies × (busyness now ÷ target), rounded up.
- Change the number, not the copies. The autoscaler only raises the number in the code, from 1 to 3. The controller then starts the copies, as in Desired state and reconciliation.
- New copies are not instant. Each copy needs time to start, often 10 seconds to a few minutes. Visitors wait meanwhile, as in the animation. So aim the target low enough to leave room, and keep a minimum number of copies.
- Go down slowly. When visitors leave, the number goes back down, but only after the quiet has lasted a while (often about 5 minutes). Otherwise a short dip removes copies just before the next wave.
An example
One copy, target 30% CPU. A sale starts and that copy hits 90%.
1 × (90 ÷ 30) = 3. The autoscaler sets 3. About 30 seconds later, two new copies are up and each sits near 30%.
At night CPU falls to 10% on each of 3. 3 × (10 ÷ 30) = 1. After 5 quiet minutes, the number goes back to 1.
Further reading
- Horizontal Pod Autoscaling, Kubernetes docs. The formula above, plus how it avoids flapping up and down. Start with this one.
- Managing load, Google SRE Workbook. Autoscaling next to load balancing and shedding, with what goes wrong.
- Target tracking scaling policies, AWS docs. The same idea for whole servers, with warm-up time built in.