Autoscaling

Topic 6 of 8 in Week 2: Scaling, traffic and estimation10 min readIntermediateFree course

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

Autoscaling follows demand over a day. Traffic is low overnight, rises from about 06:00, peaks around 17:00 and falls in the evening. The number of servers, shown as a stepped capacity line, stays above traffic: it steps up ahead of the peak because the policy reacts early and new servers need minutes to boot, and steps down later and more slowly to avoid flapping. Example policy: scale out when CPU is above 60 percent for 3 minutes; scale in when it is below 30 percent for 15 minutes.

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.

Exercise

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.

Text
Minimum servers:
Maximum servers:
Metric and thresholds:
Scale-in rule:

Concert on-sale spike:

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.