Sub topic 9
Spreading a workload across Availability Zones
This chapter assumes you know what a Region and an Availability Zone (AZ) are, that you can run aws commands and Python scripts, and that you know what a VPC, subnet and load balancer are. It covers how to lay out a workload so it keeps running when one AZ fails.
What multi-AZ protects against
An AZ is one or more data centers in a Region with independent power, cooling and physical security. AZs in the same Region are connected by fast, low-latency links. Multi-AZ design uses that separation. If one AZ loses power or networking, the copies of your workload in the other AZs keep serving traffic.
It does not protect you from a Region-wide event, a bad deployment, a deleted database or a leaked credential. Those problems need backups, deployment safeguards and, for Region failures, a multi-Region design. Multi-AZ is the baseline for availability inside one Region.
The working model
Three rules cover most designs.
- Subnets are zonal. A subnet lives in exactly one AZ, so you need at least one subnet per AZ you want to use. A VPC itself is regional and spans all AZs.
- Stateless tiers run in every AZ. Web and application servers sit in each AZ behind a load balancer. Any instance can serve any request.
- Stateful tiers replicate across AZs. Databases, caches and file systems need a standby or replica in another AZ, or a managed service that does this for you.
The diagram shows a typical two-AZ layout.
Step 1: one subnet per AZ
Create subnets by AZ ID where you can. AZ names such as us-east-1a map to different physical zones in different accounts, while IDs such as use1-az1 are the same everywhere. This matters when two accounts must share a zone, for example a producer and a consumer that want low-latency, same-AZ traffic.
aws ec2 create-subnet \
--vpc-id vpc-0123456789abcdef0 \
--cidr-block 10.0.1.0/24 \
--availability-zone-id use1-az1The VPC ID is a placeholder. Repeat the command with a different CIDR block and AZ ID for each additional zone. To avoid typing this by hand, the boto3 script below creates one subnet in each standard AZ that is available in a Region. It skips Local Zones and Wavelength Zones.
import boto3
VPC_ID = "vpc-0123456789abcdef0" # placeholder: use your VPC ID
ec2 = boto3.client("ec2", region_name="us-east-1")
response = ec2.describe_availability_zones(
Filters=[
{"Name": "zone-type", "Values": ["availability-zone"]},
{"Name": "state", "Values": ["available"]},
]
)
for index, az in enumerate(response["AvailabilityZones"], start=1):
subnet = ec2.create_subnet(
VpcId=VPC_ID,
CidrBlock=f"10.0.{index}.0/24",
AvailabilityZoneId=az["ZoneId"],
)
print(az["ZoneName"], az["ZoneId"], subnet["Subnet"]["SubnetId"])The script assumes your VPC CIDR block contains 10.0.1.0/24 and the following ranges. Adjust the CIDR logic if your VPC uses a different range.
Step 2: let the service spread the instances
Most multi-AZ work is configuration. You give the service subnets from several AZs and it handles placement.
An Auto Scaling group balances instances across the subnets you list. The command below uses placeholder subnet IDs, launch template name and target group ARN. Replace them with your own values.
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name web-asg \
--launch-template 'LaunchTemplateName=web-template,Version=$Latest' \
--min-size 2 \
--max-size 6 \
--vpc-zone-identifier "subnet-0aaa1111bbbb2222c,subnet-0ddd3333eeee4444f" \
--target-group-arns arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/web/0123456789abcdef \
--health-check-type ELB \
--health-check-grace-period 120Setting --health-check-type ELB means the group replaces instances that fail load balancer health checks, not only those that fail EC2 status checks. Afterward, confirm the spread instead of assuming it:
aws autoscaling describe-auto-scaling-groups \
--auto-scaling-group-names web-asg \
--query "AutoScalingGroups[].Instances[].[InstanceId,AvailabilityZone,HealthStatus]" \
--output tableIf every row shows the same AZ, the group has effectively one AZ, and your design is not multi-AZ.
Which services are zonal
Knowing the scope of each building block tells you where you must add redundancy yourself.
| Component | Scope | What you do |
|---|---|---|
| Subnet | One AZ | Create one per AZ |
| EC2 instance | One AZ | Run several, spread by Auto Scaling |
| EBS volume | One AZ | Replicate data at the application layer or use snapshots |
| NAT gateway | One AZ | Create one per AZ and route each AZ's subnets to its own |
| Application Load Balancer | Nodes in each AZ you enable | Enable at least two AZs |
| RDS with Multi-AZ | Primary and standby in different AZs | Turn on Multi-AZ |
| S3 Standard, EFS Standard | Multiple AZs, managed for you | Nothing extra; avoid One Zone classes for critical data |
Capacity: size for the loss of an AZ
Spreading instances is not enough. When an AZ fails, the surviving AZs must absorb its traffic. If two AZs each run at 70% CPU, losing one asks the other to carry 140%, and it will fail too.
| AZs in use | Share of load each survivor takes over | Maximum steady utilization per AZ |
|---|---|---|
| 2 | Up to 2x | 50% |
| 3 | Up to 1.5x | about 66% |
| 4 | Up to 1.33x | 75% |
More AZs lower the cost of redundancy. Three AZs need 50% extra capacity to survive one failure, while two AZs need 100% extra. Not every Region has three or more AZs usable by your account, and some services are limited in the AZs they support. Check your Region before you plan around three.
Do not rely on launching replacement capacity during the failure. During a large event, many customers request capacity at the same time, and API calls can slow down. A design with pre-provisioned capacity in the surviving AZs keeps working without any new launches. This idea is called static stability.
Load balancing details
A load balancer places a node in each AZ you enable and sends traffic to those nodes through DNS. Cross-zone load balancing decides whether a node sends traffic only to targets in its own AZ or to targets in all AZs.
- Application Load Balancer: cross-zone is always on.
- Network Load Balancer and Gateway Load Balancer: cross-zone is off by default. With it off, an AZ with fewer healthy targets receives the same share of traffic as the others and can overload.
Cross-zone traffic is also billed as data transfer. With an ALB, the load balancer's own cross-zone traffic is not charged, but traffic between your own instances in different AZs is charged per GB in each direction. The Region and cost chapter covers this in more detail.
Common errors
- Two subnets, one AZ. The console lets you pick two subnets that sit in the same zone. Check each subnet's AZ ID before you create a load balancer.
- One NAT gateway for the whole VPC. If its AZ fails, private subnets in every AZ lose outbound internet access. Create one per AZ and give each AZ its own route table.
- Multi-AZ web tier, single-AZ database. The application is up but has nowhere to read data.
- Hard-coded AZ names. Scripts that assume
us-east-1aexists break in accounts where it is unavailable or maps differently. Query the AZs instead, as in the script above. - Treating an EBS volume as portable. You can attach a volume only to an instance in the same AZ. To move data, snapshot it and create a new volume in the target AZ.
- Never testing failover. A design is a hypothesis until you break something. Stop all instances in one AZ in a test environment, or use AWS Fault Injection Service, and confirm the error rate stays low.
Putting it together
A minimal resilient layout has these parts:
- A VPC with at least two subnets in different AZs for each tier (public and private).
- An Application Load Balancer enabled in the public subnets of both AZs.
- An Auto Scaling group that spans the private subnets of both AZs, with a minimum of two instances.
- One NAT gateway per AZ.
- A database with Multi-AZ turned on, or a service that replicates across AZs by default.
- Utilization targets that allow one AZ to fail, as in the capacity table.
Exercise
Your Auto Scaling group runs 6 instances across 3 AZs, and each instance runs at 60% CPU. One AZ fails and the group cannot launch replacements right away. Roughly what CPU will the surviving instances reach, and what does that tell you about the design?
Show answer
The 4 survivors take over the work of 6 instances. Total load is 6 x 60% = 360%, and 360% / 4 = 90% CPU per instance. That leaves almost no headroom, so the design is not safe against an AZ failure. With 3 AZs, steady utilization should stay near 66% or below, and lower if you want a margin for traffic spikes.
Exercise
You create two subnets with a load balancer, and the describe-auto-scaling-groups output shows all instances in us-east-1a. Name two possible causes and one command or check that confirms each.
Show answer
Possible cause 1: both subnets are in the same AZ. Check each subnet's AZ with aws ec2 describe-subnets --subnet-ids <id> --query "Subnets[].[SubnetId,AvailabilityZoneId]". Possible cause 2: the Auto Scaling group lists only one subnet in --vpc-zone-identifier. Check with aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names web-asg --query "AutoScalingGroups[].VPCZoneIdentifier".