6. Vertical vs Horizontal Scaling
6.6 What is autoscaling, and how do you get it right?
In the kitchen: You don't pay ten chefs all day for a two-hour dinner rush. You bring in extra chefs for the rush and send them home after. Two things go wrong if you're careless: a new chef needs time to get ready before cooking, and if you hire and fire every ten minutes, the kitchen is chaos.
In the system: Autoscaling automatically adds servers when load rises and removes them when it falls, between a minimum and a maximum.
Ways to decide when:
- Target tracking: "keep average CPU around 60%" or "keep about 300 requests a second per server". The autoscaler adds or removes servers to stay near the target.
- Scheduled: "at 7:55 pm on sale day, have at least 67 servers". For events you know are coming.
- Predictive: some platforms forecast from past days and scale ahead of the usual daily curve.
The rules that make it work:
- New servers aren't instant. Starting a machine, loading the app and warming its caches can take minutes. A reactive autoscaler is always behind a sudden spike.
- Scale out fast, scale in slowly. Leave a gap between the "add" and "remove" thresholds (for example, add above 70%, remove below 40%) and a cooldown after each change. Otherwise the fleet flaps: add a server, load drops, remove it, load rises, add it again (the Level 4 bug).
- Pick a metric that tracks the real limit. CPU works for compute-heavy services. For a service that mostly waits on the database, request count or latency is better. For workers, use queue length (Chapter 22).
- Set a sensible maximum, so a bug or an attack doesn't create a thousand servers, and so you don't overwhelm the database behind them.
Figure: the chapter's simulation. With a 3-minute warm-up, reacting to the Book Fest spike loses about 3 million requests. Scaling ahead loses none.
For the Book Fest: the start time is known, so schedule the capacity before it, and keep target tracking on as a safety net for surprises.
Memory code: "Scale out fast, scale in slow, and schedule what you know."