Volume 4 · Chapter 2 — Compute and Containers
Auto Scaling and Elasticity
2.1 Introduction
Elasticity - capacity that grows and shrinks with demand - is one of the cloud’s defining benefits, and Auto Scaling is the service that delivers it for EC2. It’s what lets a bank absorb a payday surge without permanently over-provisioning, and it’s also a resilience mechanism, automatically replacing failed instances. Understanding how Auto Scaling groups work with load balancers and health checks is essential because it’s the pattern behind nearly every scalable, self-healing compute tier, and it comes up constantly in design and interview discussions about handling load and failure.
2.2 Auto Scaling Groups
An Auto Scaling group (ASG) manages a fleet of identical instances, maintaining a desired count and adding or removing instances automatically. You define a minimum, maximum, and desired capacity, a template describing how to launch each instance (which AMI, type, subnets, security groups, role), and the ASG keeps that many healthy instances running across the Availability Zones you specify. Two things make it powerful: it distributes instances across AZs for resilience, and it continuously ensures the desired number are healthy - so if an instance fails or is terminated, the ASG launches a replacement automatically. This self-healing behaviour alone, independent of scaling, is a major reliability benefit.
flowchart TB
CW[CloudWatch metric\ne.g. CPU or request count] --> Policy{Scaling policy\nabove/below target?}
Policy -->|demand up| Out[Scale out - add instances]
Policy -->|demand down| In[Scale in - remove instances]
subgraph ASG[Auto Scaling Group across AZs]
I1[Instance AZ-A]
I2[Instance AZ-B]
I3[Instance AZ-A]
end
Out --> ASG
In --> ASG
ASG --> LB[Load Balancer sends traffic to healthy instances] 2.3 Scaling Policies
Auto Scaling responds to demand through scaling policies. Target-tracking scaling keeps a chosen metric at a target value - for example, “keep average CPU at 50%” - and the ASG adds or removes instances to hold that target, which is the simplest and most common approach. Step scaling adjusts capacity in defined steps based on how far a metric breaches a threshold. Scheduled scaling changes capacity at known times - useful when load is predictable, like scaling up before market open or payday. These can combine: scheduled scaling for predictable patterns plus target-tracking for unexpected variation. The metric driving scaling should reflect real load - CPU, request count per instance, or queue depth - so capacity tracks what actually stresses the system.
2.4 Scaling with Load Balancers and Health Checks
Auto Scaling works hand-in-hand with load balancers. As the ASG adds instances, it registers them with the load balancer, which starts sending them traffic once they pass health checks; as it removes instances, they’re deregistered and drained. The ASG uses health checks (including the load balancer’s) to decide which instances are healthy: an instance failing health checks is terminated and replaced, keeping the fleet healthy. This combination - ASG for the right number of healthy instances across AZs, load balancer for distributing traffic to them - is the core scalable, self-healing pattern. New instances must be able to start serving quickly and statelessly, which is why state lives in databases and caches rather than on the instances.
2.5 Designing for Elasticity
To scale well, the application must be built for it. Instances should be stateless - holding no unique session or data locally - so any instance can handle any request and new instances add capacity immediately; session state and data go to shared stores (a database, a cache, object storage). Startup should be fast so new capacity comes online quickly when demand rises. And the design should scale horizontally (add more instances) rather than relying on ever-bigger single instances. With these in place, Auto Scaling can add capacity for spikes and remove it afterward, so the bank pays only for what each moment needs while always meeting demand - the essence of cloud elasticity.
2.6 Banking Production Scenario
A bank’s customer-facing service runs in an Auto Scaling group across two Availability Zones behind an Application Load Balancer. Target-tracking scaling holds average CPU near a target, so as payday traffic climbs the ASG adds stateless instances that begin serving once healthy, and removes them as load falls - the bank pays for the surge only while it lasts. Scheduled scaling pre-warms extra capacity just before known peak times so there’s no lag. If any instance fails its health check, the ASG terminates and replaces it automatically, and if an entire AZ is lost, the ASG launches replacements in the healthy AZ while the load balancer routes around the failure. Elasticity and self-healing are both delivered by the same ASG pattern.
2.7 Troubleshooting: Scaling Problems
Scaling issues have recognizable causes. If the service is overwhelmed but not scaling, the scaling policy or its metric may be wrong (scaling on a metric that doesn’t reflect real load), or the max capacity is set too low to add enough instances - the fixes are choosing a load-reflecting metric and raising the ceiling. If instances are added but don’t help, they may be slow to start or not truly stateless, so new capacity isn’t serving quickly - fix startup time and remove instance-local state. If scaling thrashes (rapidly adding and removing), cooldowns and target values need tuning. If a scaled-in instance drops active requests, connection draining isn’t configured to let in-flight work finish. And if new instances fail health checks and get replaced in a loop, the instance configuration or health-check definition is wrong. The diagnostic pattern is to check the scaling metric and limits, instance startup and statelessness, and health-check and draining settings.
How does Auto Scaling work, and how does it provide both elasticity and resilience?
Auto Scaling manages a fleet of identical instances through an Auto Scaling group, which maintains a desired number of healthy instances and adds or removes them automatically. You define a minimum, maximum, and desired capacity, and a template describing how to launch each instance - the AMI, instance type, subnets, security groups, and IAM role - and the group keeps that many healthy instances running across the Availability Zones you specify. It delivers elasticity through scaling policies that respond to demand. The most common is target-tracking, which keeps a chosen metric at a target value - say “keep average CPU at 50%” - by adding instances when the metric rises above target and removing them when it falls below; there’s also step scaling that adjusts in steps based on how far a threshold is breached, and scheduled scaling that changes capacity at known times for predictable patterns like payday or market open. As it scales out, the group registers new instances with the load balancer, which sends them traffic once they pass health checks; as it scales in, instances are drained and deregistered. This means capacity follows demand - the bank meets a surge by adding instances and then removes them afterward, paying only for what each moment needs, which is the essence of cloud elasticity. Auto Scaling provides resilience through the same machinery: the group continuously uses health checks to ensure the desired number of healthy instances exist, so if an instance fails or becomes unhealthy, the group automatically terminates it and launches a replacement - self-healing without human intervention. And because instances are spread across multiple Availability Zones, the loss of an entire AZ just means the group launches replacements in the healthy zones while the load balancer routes traffic there, so an AZ failure degrades capacity rather than causing an outage. For all of this to work well, the application must be built for it - instances should be stateless so any new instance immediately adds capacity and any request can go to any instance, state should live in shared databases and caches, and startup should be fast so new capacity comes online quickly. So Auto Scaling gives elasticity by tracking a load metric to add and remove capacity with demand, and resilience by health-checking the fleet to automatically replace failures and by spreading instances across AZs to survive zone loss - the same group delivering both, which is why it’s the backbone of scalable, self-healing compute on AWS.
Chapter Revision Notes
- An Auto Scaling group maintains a desired count of identical, healthy instances across AZs, launching replacements automatically.
- Scaling policies: target-tracking (hold a metric at a target - most common), step (by threshold breach), scheduled (predictable times).
- It works with the load balancer: new instances get traffic once healthy; removed ones are drained; unhealthy ones are replaced.
- Elasticity (capacity follows demand, pay for what you use) and resilience (self-healing, survive AZ loss) come from the same ASG.
- Design for it: stateless instances, state in shared stores, fast startup, horizontal scaling.