Sub topic 9
Spreading a Workload Across Availability Zones
This chapter assumes you know what a Region and an Availability Zone (AZ) are, can run AWS CLI commands, and understand subnets and CIDR blocks at a basic level. It covers how to lay out a simple workload so that losing one AZ does not take the workload down.
Why spread across AZs
Each AZ in a Region is an isolated location with its own power, cooling, and networking. A failure in one AZ, such as a power loss, usually does not affect the others. If your whole workload runs in one AZ, that AZ is a single point of failure. If it runs in two or more, the surviving AZs can keep serving traffic.
Spreading across AZs only helps if two things are true:
- Each AZ holds enough capacity to carry the load when another AZ is gone.
- Traffic is routed away from the failed AZ automatically.
A simple multi-AZ layout
A common layout has one VPC (Virtual Private Cloud, your private network) in one Region. Inside it, each AZ gets its own subnet. A subnet lives in exactly one AZ, so the subnet is how you choose where a resource runs. A load balancer sits in front and sends requests to servers in every AZ.
The load balancer is itself a regional service. You attach one subnet from each AZ, and it runs nodes in each of them. It sends traffic only to healthy targets, so when the servers in one AZ fail their health checks, requests go to the other AZ.
Building it step by step
First create one subnet per AZ. Use AZ IDs so the placement is the same physical zone across accounts (see the previous chapter on names versus IDs).
aws ec2 create-subnet \
--vpc-id vpc-0123456789abcdef0 \
--cidr-block 10.0.1.0/24 \
--availability-zone-id use1-az1
aws ec2 create-subnet \
--vpc-id vpc-0123456789abcdef0 \
--cidr-block 10.0.2.0/24 \
--availability-zone-id use1-az2The VPC ID is a placeholder. Use your own. The two CIDR blocks must not overlap.
Next, put servers in an Auto Scaling group that spans both subnets. The group launches instances across the subnets you give it and keeps them balanced. If an AZ becomes unavailable, it launches replacements in the remaining AZ.
For stateful parts, use a service's own multi-AZ option instead of building replication yourself. For example, Amazon RDS offers a Multi-AZ deployment that keeps a standby copy in another AZ and fails over automatically. Amazon S3 stores objects across multiple AZs in a Region by default for most storage classes.
Common errors
- Only one subnet is attached. An Auto Scaling group or load balancer with subnets from a single AZ is not multi-AZ, however many instances it has.
- No spare capacity. Two AZs each running at 80% load cannot absorb a lost AZ. With two AZs, size each to carry the full load alone. With three, each needs about 50% of the load as headroom.
- Zonal resources left behind. An EBS volume exists in one AZ, and an instance can attach only to a volume in the same AZ. Snapshots are regional, so you can restore a volume in another AZ from a snapshot.
- Hidden single-AZ dependencies. A NAT gateway is zonal. If all private subnets route through one NAT gateway, losing its AZ cuts off outbound traffic from every AZ. Create one per AZ and route each private subnet to the NAT gateway in its own AZ.
- Assuming it covers a Region outage. Multi-AZ protects against AZ failure. It does not protect against a Region-wide problem. That needs a multi-Region design, which costs more and is more complex.
Choosing the number of AZs
Two AZs is the minimum for tolerating one failure. Three AZs lets each one run at a lower share of capacity during a failure, but adds cost and complexity. Pick based on how much downtime the workload can accept.
Exercise
A team runs 6 web servers behind a load balancer, split 3 and 3 across two AZs, and each server runs near full capacity. One AZ fails. What is the problem, and what would you change?
Show answer
The remaining 3 servers must carry the load of 6, so they will be overloaded. Run enough servers in each AZ to handle the full load alone (for example 6 in each AZ, or add a third AZ with 3 servers each so any two AZs carry the load), and use an Auto Scaling group across all the subnets so replacements launch in the surviving AZs.