Three courses' worth of scale
This combined project joins System design and AWS. Your service must handle a festival-day rush without paying for festival capacity all year. First stop: an autoscaling policy like AWS target tracking — keep average CPU near a target by adding or removing instances.
- Day 1: target-tracking autoscaling
- Day 2: global routing and health checks
- Day 3: a cached, versioned data layer
- Day 4: capacity planning for a worker fleet
Target tracking
function createAutoscaler({ min, max, target = 50, cooldown = 300, now = () => Date.now() / 1000 }) {
let lastChange = -Infinity;
return {
evaluate(current, cpuSamples) {
if (cpuSamples.length === 0) return current; // no data: change nothing
const avg = cpuSamples.reduce((a, b) => a + b, 0) / cpuSamples.length;
let desired = Math.min(max, Math.max(min, Math.ceil((current * avg) / target)));
if (desired < current && now() - lastChange < cooldown) desired = current; // scale in slowly
if (desired !== current) lastChange = now();
return desired;
},
};
}