Autoscaling
Fleets that match demand
Autoscaling grows or shrinks instance fleets to match demand: target tracking keeps average CPU sensible; scheduled scaling covers known peaks; and the scaling process itself fails in characteristic ways you must design around.
What you will learn
- Explain scale out/in triggers and cool-down/cooldown semantics
- Combine target tracking, scheduled and predictive scaling
- Design ASG/VMSS/MIG that survive scale-in races and capacity gaps
A group (ASG, VMSS, MIG) watches metrics like average CPU or request count, adds instances when utilization climbs and removes them when idle — using launch templates and health checks so the fleet always matches demand.
What is this?
AWS Auto Scaling Groups: launch template + min/max/desired, scaling policies (target tracking, step, scheduled, predictive), health checks (EC2 and ELB), lifecycle hooks and termination policies. Azure VMSS: autoscale rules on metrics/CPU, schedules, scaling actions, health probes. GCP Managed Instance Groups: autoscaler with target utilization, CPU/requests metrics, cool-down and buffer sizing.
Why do you need it?
- Pay for demand, not peak: fleets shrink at night automatically.
- Smoothly absorb traffic spikes that would tip one big box.
- Self-healing: replace unhealthy instances without human contact.
- Predictable costs: keep min warm, scale elastically above it.
Mental model
A supermarket opening and closing checkout lanes
Autoscaling
Manager watches queue length (metric), opens lanes when lines grow (scale out), closes them when quiet (scale in), and always keeps enough lanes for the schedule — the same trigger/policy relation as autoscaling.
How it works
- Group definition: min, max, desired, launch template (image + size + user-data) that every new instance uses.
- Target tracking scales to a threshold: average CPU 60% adds/removes instances to hold that average.
- Scale-out is fast (instances start eagerly); scale-in is conservative and drains over cooldown so spot price and traffic settle.
- Health checks flag unhealthy instances; the group terminates and replaces them.
- Scheduled scaling pre-warms for known peaks (9am login wave) and lifecycle hooks pause termination for cleanup.
Autoscaling control loop.
Examples
Target-tracking autoscaling in each cloud
# AWS: target tracking on average CPU at 60%aws autoscaling put-scaling-policy --auto-scaling-group-name web --policy-name cpu60 --policy-type TargetTrackingScaling --target-tracking-configuration '{"PredefinedMetricSpecification": {"PredefinedMetricType":"ASGAverageCPUUtilization"},"TargetValue":60.0}'# Azure VMSS: autoscale rule file (CPU > 70% add, < 30% remove)az monitor autoscale create -g app-rg --resource vmss-1 --min-count 2 --max-count 10 --count 2# GCP MIG autoscalergcloud compute instance-groups managed set-autoscaling web-mig --max-num-replicas 10 --target-cpu-utilization 0.6All three use target-metric autoscaling policies over instance groups.
In production
- A checkout service scales on ALB target response time with a 15-instance cap.
- ETL workers scale by SQS queue depth via a custom metric policy.
- A VMSS scales on custom in-guest CPU counters with scheduled surge before payroll runs.
Best practices
- Set a min that sustains base traffic and a max that bounds cost.
- Add warm pools/pre-warm for bursty latency-sensitive tiers.
- Make instances killable: no state outside external stores (S3/DB/queue).
- Test scale-in: rolling termination should not drop in-flight requests.
Common mistakes
- Autoscaling over a single oversized instance — peak-loaded and expensive.
- Ignoring cooldown: policies fight each other and 'thrash' the fleet.
- State lives on the instance (local logs, sessions) → scale-in drops data.
- Max set too low: scale-out can never catch the spike (looks like 'not working').
Common issues
Group never scales above 2 instancesMax too low or policy throttled
Raise max, verify the policy evaluates the right metric for the right group.
New instances fail health checks, group replaces constantlyHealth check port misconfigured or boot script failing
Fix the launch template/health check; check cloud-init for boot errors.
Debugging tips
- Check the scaling activity history: the group logs reasons for each add/remove.
- Scale-out slow? Check cloud-init and image boot time; optimize with warm pools.
- Cooldown + step policy interactions cause oscillation — log policy evaluations.
- If the group never scales in, suspend/resume processes and inspect the termination policy.
Interview checkpoint
Hands-on lab
- Translate traffic requirements into min/max/desired and targets
- Simulate the control loop with a Python model
- Design a throttling and cooldown that avoids oscillation
Design and Simulate an Autoscaling Policy
30 min · intermediate
Quick quiz
What prevents an autoscaler from oscillating between add and remove?