Module 6 · Reliability and infrastructureLesson 72 of 886 minIntermediate

Horizontal autoscaling

An autoscaler adds servers when load is high and removes them when it's low, so you pay for what you need without falling over at peak.

Try it

Watch servers being added as traffic rises and removed as it falls. Push the peak traffic higher and notice the lag while new servers start.

Traffic changes through the day, the week and around events. Running enough servers for the peak all the time wastes money; running too few causes outages. Autoscaling adjusts automatically.

How it works

  1. Pick a metric that tracks load: average CPU, requests per second per server, or queue length.
  2. Set a target, like 60% CPU.
  3. The autoscaler periodically compares actual with target and adds or removes servers.

Kubernetes' Horizontal Pod Autoscaler, AWS Auto Scaling groups and most cloud platforms work this way.

Getting it right

  • Startup time is the enemy. A server that takes 2 minutes to boot can't help with a 30-second spike. Make startup fast (small images, no slow warmup), and keep headroom with a target below 100%.
  • Scale up fast, scale down slowly: removing servers too eagerly causes flapping, adding and removing over and over.
  • Min and max: a minimum keeps you safe at night (and across zones); a maximum stops a bug or attack from scaling you into a huge bill.
  • Pick the right metric: CPU is easy, but for a queue worker, queue length or message age is better.

Beyond reactive scaling

  • Scheduled: add capacity before known peaks (9 am Monday, a product launch).
  • Predictive: forecast from past patterns.
  • Vertical autoscaling adjusts a pod's CPU and memory instead of counts.
  • Cluster autoscaling adds whole machines when pods don't fit.

Watch the next bottleneck

Scaling the app tier from 10 to 100 servers multiplies database connections and queries tenfold. Autoscaling works best for stateless tiers, backed by databases and caches sized for the maximum, not the minimum.

Quick check

Traffic doubles in 10 seconds, but new servers take 2 minutes to start. What helps most?