Autoscaling
Overview
Traffic changes through the day, the week and the year. Running enough servers for the busiest minute all the time wastes money; running too few causes outages at the worst moment. Autoscaling adjusts the number of servers automatically to follow demand. It’s built into every cloud platform and Kubernetes, and it’s a standard part of designs for variable traffic.
How it works
An autoscaling group (or a Kubernetes HorizontalPodAutoscaler) has:
- a minimum number of instances that always run,
- a maximum as a safety limit on cost and on load sent to downstream systems,
- a metric and a target or thresholds,
- cooldown or stabilisation periods between changes.
A controller checks the metric every minute or so and adds or removes instances to bring it back to target.
Choosing the metric
The metric should rise and fall with the work the servers do:
| Metric | Good for | Watch out for |
|---|---|---|
| CPU utilisation | CPU-heavy services | I/O-bound services may be slow at low CPU |
| Requests per server | Typical web APIs | Requests of very different costs |
| Queue length or age | Background workers | Needs the right messages-per-worker ratio |
| Latency (p95/p99) | User-facing targets | Can rise for reasons more servers won’t fix |
| Custom (connections, sessions) | WebSocket gateways | Needs your own instrumentation |
Target tracking is the simplest policy: “keep average CPU at 55%”. The platform works out how many servers that needs.
Timing is everything
New capacity isn’t instant. A virtual machine may need 1–5 minutes to boot, start the app, load caches and pass health checks; containers on existing nodes are faster, but adding nodes takes minutes too. Therefore:
- Scale out early and in big steps. Trigger before servers are saturated (for example at 60% CPU, not 90%), and add a percentage of the fleet rather than one server at a time.
- Scale in slowly. Wait longer (for example 10–15 minutes below target) and remove a few at a time. Otherwise the fleet flaps: shrinks, gets overloaded, grows, shrinks again.
- Warm up. Count new servers only after they’re ready, and give them a gentle start (slow start on the load balancer) while caches fill.
- Drain before removing. Let in-flight requests finish (connection draining).
Scheduled and predictive scaling
Reactive autoscaling can’t handle traffic that arrives faster than servers boot. For predictable patterns:
- Scheduled scaling: raise the minimum before the 9 a.m. rush, a marketing email or a product launch.
- Predictive scaling: cloud services can learn daily and weekly patterns and scale ahead of them.
For truly sudden spikes, such as a concert going on sale or a viral post, combine pre-scaling with protection: queues, a waiting room, rate limits and load shedding (week 6).
What autoscaling doesn’t fix
- Stateful tiers. Adding database servers isn’t a quick, automatic operation; it involves replicas, shards and data movement.
- Downstream limits. Doubling app servers doubles the load on the database and on third-party APIs. Sometimes the right maximum is set by what’s behind you.
- The wrong bottleneck. If the database is saturated, adding app servers makes it worse.
- Slow starts. Apps that take minutes to start limit how quickly you can respond; make them start fast.
- Bugs and runaway loops. A bug that burns CPU will trigger scaling and a large bill. Always set a maximum and alert on unusual scaling.
Serverless: autoscaling taken further
Functions-as-a-service platforms (AWS Lambda, Google Cloud Functions, Azure Functions) and serverless containers scale per request, down to zero. They suit spiky or low-volume workloads but bring cold starts (the first request to a new instance is slower), per-account concurrency limits and per-invocation pricing that can be expensive at steady high load.
In an interview
You can usually cover autoscaling in a sentence: “App servers are stateless and in an autoscaling group across three zones, scaling on requests per instance, with a minimum of 6 and pre-scaling before planned events.” Then spend your time on what doesn’t autoscale: the database, caches and hot spots.
Recap
Autoscaling keeps a metric near its target by adding or removing servers between a minimum and maximum. Choose a metric that tracks real work, scale out early and quickly, scale in slowly, and account for warm-up time. Use scheduled or predictive scaling for known peaks and protective measures for sudden spikes. Autoscaling helps stateless tiers; databases and external dependencies need their own capacity plans.
Design a scaling policy
A ticket-selling API runs on servers that each handle about 400 requests/second at 70% CPU. Normal traffic is 2,000 req/s, evenings reach 6,000 req/s, and when a big concert goes on sale traffic jumps to 40,000 req/s within one minute. New servers take 3 minutes to become ready. Design the scaling setup: minimum and maximum servers, the metric and thresholds, and how you'd handle the concert on-sale spike.
Minimum servers:
Maximum servers:
Metric and thresholds:
Scale-in rule:
Concert on-sale spike:Minimum servers: 2,000 / 400 = 5, plus headroom and one per zone for failures:
about 8 servers (spread over at least 2-3 availability zones).
Maximum servers: 40,000 / 400 = 100, plus margin: ~120, and check the database
and downstream services can cope with that load (they are probably the real limit).
Metric and thresholds: target tracking on requests per server (~300 req/s) or
CPU around 55-60%; scale out quickly (evaluate every minute, add 20-50% capacity at a time).
Scale-in rule: remove servers slowly, e.g. when below target for 15 minutes,
a few at a time, with connection draining.
Concert on-sale spike: reactive autoscaling is too slow (traffic arrives in
1 minute, servers need 3). So:
- Scheduled scaling: pre-scale to ~110 servers 15-30 minutes before the on-sale time.
- A virtual waiting room / queue in front of checkout so excess users wait instead
of overloading the system.
- Rate limiting and load shedding as a safety net.
- Load-test the full path, including the database, before the event.Summary
- Autoscaling adds servers when a metric is above target and removes them when it is below, between a minimum and maximum.
- Good metrics track load directly: CPU, requests per server or queue length; target tracking keeps a metric near a set value.
- New servers take minutes to be useful, so scale out early and fast, and scale in slowly with cooldowns to avoid flapping.
- Use scheduled or predictive scaling for known peaks; reactive scaling can't catch sudden spikes.
- Autoscaling only helps stateless tiers that are the bottleneck; databases and downstream limits need their own plans.