Load Balancing & Auto Scaling
Elastic Load Balancing distributes traffic across EC2 instances, containers and IP targets, while Auto Scaling Groups keep the right number of instances running. Learn ALB versus NLB, health checks, target groups, scaling policies and how the two services work together to survive traffic spikes and instance failures.
What is this?
Elastic Load Balancing distributes incoming traffic across EC2 instances, containers and IP targets. The Application Load Balancer (ALB) works at layer 7 with host and path routing; the Network Load Balancer (NLB) works at layer 4. Auto Scaling Groups maintain a desired number of instances, launch or terminate to match demand, replace unhealthy ones, and integrate with the load balancer so only healthy targets receive traffic.
Why do you need it?
- A single server is a single point of failure — load balancers spread risk across many targets.
- Auto scaling is how AWS handles traffic spikes without manual intervention.
- Health checks decide when traffic is diverted away from failing instances.
- Scaling policies (target tracking, scheduled, predictive) drive both cost and reliability.
- ALB path and host routing lets you run many microservices behind one entry point.
Mental model
A restaurant host at the door
A load balancer and its waiters
The host (load balancer) greets every party, checks the seating chart (health checks), and sends them to an available table (a target). If a waiter is slow or leaves, the host stops seating people there and redirects to others. During the dinner rush, the manager (auto scaling) adds tables and waiters; after hours, the extra tables are cleared. Every party gets served without overwhelming any single waiter.
How it works
- You register targets (instances in an Auto Scaling group, or IPs) with a target group and define a health check — path, port and interval.
- The load balancer owns a DNS name and listeners; ALB rules route by host header or path prefix to different target groups.
- The load balancer health-checks targets and stops sending traffic to any that fail; the ASG does its own checks and replaces bad instances.
- Auto scaling policies read CloudWatch metrics (CPU, request count, custom metrics) and adjust desired capacity between a minimum and maximum.
- The load balancer performs connection draining — letting in-flight requests finish — before it removes a target from rotation.
Traffic flow through an ALB into an Auto Scaling group.
Key commands
aws autoscaling describe-auto-scaling-groupsShow ASG state, desired, min and max capacity and attached load balancers.
aws elbv2 describe-target-health --target-group-arn <arn>Check the health state of every target in a group.
aws autoscaling update-auto-scaling-group --desired-capacity 4Manually scale the group to four instances.
curl -s http://<alb-dns>/healthHit the health endpoint through the load balancer.
free -mWatch instance memory when load tests drive CPU up.
Examples
Scale out and confirm the load is balanced
$ aws autoscaling update-auto-scaling-group \ --auto-scaling-group-name web-asg \ --desired-capacity 4 $ aws elbv2 describe-target-health \ --target-group-arn arn:aws:elasticloadbalancing:...:targetgroup/web/1234TargetGroups: [ { "Target": {"Id": "i-0abc123..."}, "TargetHealth": {"State": "healthy"} }, ...]Raising desired capacity tells the ASG to launch instances until it reaches four. describe-target-health then confirms each instance reports healthy to the target group — the load balancer spreads traffic across exactly these targets. Scaling works in reverse too: when the metric drops, the ASG terminates instances and the load balancer drains them.
In production
- Run stateless web tiers in an ASG behind an ALB with target-tracking scaling on request count.
- Use ALB path rules to route /api and /admin to separate target groups within one distribution.
- Attach scheduled scaling policies for known peak hours instead of reacting late.
- Enable sticky sessions (ALB cookie) only for stateful workloads that cannot be made stateless.
- Use connection draining and instance de-registration delays for graceful shutdowns during deploys.
Best practices
- Make instances stateless so any instance can serve any request — scaling and replacement depend on it.
- Keep the minimum capacity above zero for production and spread instances across availability zones.
- Define a real health check path (like /healthz) that verifies dependencies, not just a TCP port.
- Set reasonable cooldown and warm-up so scaling does not thrash under variable load.
- Test scaling policies with load tests before they are needed during an incident.
Common mistakes
- Scaling on CPU only, while the real bottleneck is request latency or memory.
- Setting min and max to the same number, which disables scaling entirely.
- A health check that always succeeds, so the load balancer keeps sending traffic to broken instances.
- Running stateful sessions on instances and losing user sessions on every scale-in.
- Ignoring connection draining settings and dropping in-flight requests during rollouts.
Common issues
Target marked unhealthy in a loopThe health check fails intermittently — timeout, error path, or a dependency the check does not cover.
Look at the health check status reason, test the path manually with curl on the instance, and lengthen the timeout or fix the endpoint.
ASG will not launch instancesThe launch template has a bad AMI, the subnet is out of IPs, or an instance quota is exhausted.
Check the ASG activity history for the exact error, correct the launch template, and verify subnets have free addresses.
5xx errors from the load balancerNo healthy targets, or the targets are saturated and returning 500s.
Check target health and instance CPU and memory; scale out or fix the application before traffic routing is restored.
Debugging tips
- Check the health check status and reason — describe-target-health tells you why a target is unhealthy.
- Look at the ALB access logs for 5xx and latency patterns before blaming the instances.
- Verify the instance security group allows health-check traffic from the load balancer.
- Compare the ASG desired count versus running count — a stuck launch (bad AMI, subnet, quota) shows there.
- Compare instance metrics across AZs to spot a zone-level problem early.
Interview checkpoint
Hands-on lab
- Generate a realistic access log with request records
- Count requests and inspect the busiest entries
- Aggregate and rank entries the way dashboards do
- Spot anomalies with head, tail and grep
Simulate Load and Analyze Requests Like a Balancer
25 min · intermediate
Quick quiz
Which component decides which instances receive traffic based on a /healthz path?
An Auto Scaling group can be scaled manually by changing its desired capacity.
Traffic to your site spikes every weekday at 9am but scaling reacts slowly, causing errors. What is the best approach?
Cheat sheet
ALBLayer 7 — host/path routing, TLS terminationNLBLayer 4 — raw TCP/UDP, low latencyTarget groupSet of targets plus a health checkListenerPort + protocol + routing rulesConnection drainingLet in-flight requests finishDesired capacityInstances the ASG targets nowMin / maxBounds the group may scale withinLaunch templateAMI, instance type, user dataTarget trackingKeep a metric near a valueScheduled scalingChange capacity at fixed times