Sikau

Project: Ready for millions · Day 1 · about 35 min

Autoscaling

Target tracking on CPU: scale out fast, scale in slowly.

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;
    },
  };
}

The rest of this lesson

  • The target-tracking formulaexplain
  • Fast out, slow inexplain
  • Busy serverspredict
  • Quiet serverspredict
  • Write the policywrite code
  • Badge quizquiz
  • What you builtrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 4 lessons of Project: Ready for millions