CD9: Monitoring, alerts and scaling

Lesson 5 of 5 · reading 15–25 minutes; practice and project time is on the module page

Scaling with load

🎯You'll be able to

Scale a service horizontally on a signal that matches its work, with bounds and a stabilisation window, and prove with a load test that scaling fails no requests.

🏠The everyday picture

A toll plaza opens more booths as queues grow, and closes them only after traffic has stayed light for a while, not the moment one car is left. There's a fixed number of booths it can't go beyond, and it never closes them all.

📘What it really is

📝A worked example

Input: in the finished kit, terraform apply -var monitoring=true, start node tools/autoscale.js --min=2 --max=6 --target=150 --down-delay=60, then run node tools/load.js http://localhost:8080/api/routes 90 10, and watch docker ps --filter name=routerani-dev-app.

Expected output: the autoscaler logs lines such as {"rps":612.3,"copies":2,"desired":5,"next":5} and scaling from 2 to 5 copies within a few intervals; after the load stops, it waits for the window and logs scaling from 6 to 2 copies (or from wherever it peaked). In a run on 1 November 2026 the copies went 2 → 4 → 6 → 2 with no failed requests. If you stop Prometheus, the log shows no change this round and the copy count stays put. These are Project 3 tests 6, 7 and 8.

🌍Where it's used

📝Case scenario

A retailer's autoscaler scaled down the moment traffic dipped and up again a minute later, all afternoon. Each scale-up took two minutes, so users hit slow pages repeatedly. Adding a 10-minute stabilisation window for scale-down, and scaling on requests per second rather than CPU (which lagged), kept the copy count steady through the dips.

🖱️Try it

Read the starter's tools/autoscale.js and list its problems: rounding with Math.round, no minimum or maximum, no stabilisation window, and treating a Prometheus error as 0 requests. For each, describe what would happen during a real traffic spike or monitoring outage. Success: four problems, each with a consequence.

⚠️Common mistakes

🛠️For your project

Project 3 tests 6 to 8 check that the autoscaler reaches its maximum under load without failing requests, comes back to its minimum (never below) after a wait, and changes nothing while Prometheus is unreachable.

✅Quick check

At 700 requests per second with a target of 150 per copy and a maximum of 4, how many copies?

Show the answer

ceil(700 ÷ 150) = 5, capped at the maximum: 4.

Why scale down slowly but up quickly?

Show the answer

Up quickly protects users from load; down slowly avoids removing copies that are needed again a moment later.

← PreviousAlerts people trust
ProLearners Assist