Sub topic 1
Why scale horizontally and spread across Availability Zones
Prerequisites
This chapter assumes you know the basics of EC2 (instances, AMIs, security groups) and VPC (subnets, route tables). You should also understand HTTP and TCP at a high level and be able to run AWS CLI commands.
Two ways to add capacity
Vertical scaling means moving to a bigger instance, for example from m5.large to m5.2xlarge. It is simple because the application does not change. It has three limits:
- An instance resize requires a stop and start, so the application is down briefly.
- There is a largest instance size.
- One instance is still one failure point.
Horizontal scaling means running more instances of the same size and sharing the work among them. You can add or remove instances without downtime, and losing one instance removes only a fraction of your capacity. The cost is that the application must tolerate running as several copies.
| Aspect | Vertical | Horizontal |
|---|---|---|
| Change | Larger instance | More instances |
| Downtime to change | Usually yes | No |
| Upper limit | Largest instance type | Account quotas and design |
| Single point of failure | Yes | No, if spread out |
| Application requirement | None | Stateless or shared state |
Design for stateless instances
Horizontal scaling works when any instance can handle any request. Keep session data, uploads and user state out of instance memory and local disks. Put them in a shared service such as DynamoDB, ElastiCache or S3. Then the Auto Scaling group can terminate any instance without losing user data.
Availability Zones and high availability
An Availability Zone (AZ) is one or more data centers in a Region with independent power, cooling and networking. AZs in a Region are connected by low-latency links. A failure in one AZ should not affect the others.
Scaling out inside a single AZ gives you more capacity but not high availability, because an AZ-level outage would still take down every instance. Spread the instances across at least two AZs. Each AZ then needs a subnet. The next chapter covers how subnets map to AZs.
You can list the AZs your account can use in a Region:
aws ec2 describe-availability-zones \
--region us-east-1 \
--query "AvailabilityZones[].[ZoneName,State]" \
--output tablePlan for losing a zone
Spreading across AZs only helps if the surviving zones can carry the load. With two AZs, each should be able to handle the whole workload alone, or you accept degraded service during an outage. With three AZs, each zone needs to handle only about half the traffic, so spare capacity costs less. Three AZs therefore often give better resilience per dollar than two.
Two components do the work. A load balancer sends traffic only to healthy targets. An Auto Scaling group keeps the right number of instances running and balances them across AZs. The rest of this tutorial builds both.
Exercise
An application runs on four instances, all in one AZ behind a load balancer. Why is this not highly available, and what is the smallest change that fixes it?
Show answer
An outage in that AZ removes all four instances at once, so the load balancer has no healthy targets. Run the instances in subnets in at least two AZs, for example two in each. Each AZ should then be able to carry the load if the other fails.