Sub topic 1
Why Scale Horizontally and Spread Across Availability Zones
This chapter explains the two ideas the rest of the tutorial builds on. The first is scaling out with more instances instead of making one instance bigger. The second is placing those instances in more than one Availability Zone (AZ). Later chapters add the load balancer and Auto Scaling group that put both ideas into practice.
Prerequisites
This chapter assumes you already know the following:
- Basic EC2 concepts: instances, AMIs, and security groups.
- Basic VPC concepts: subnets, route tables, and what an Availability Zone is.
- HTTP and TCP at a high level.
- How to run AWS CLI commands.
You do not need to have created a load balancer or an Auto Scaling group yet.
Vertical and horizontal scaling
Scaling means changing how much capacity a workload has so that it matches demand. There are two ways to do it.
- Vertical scaling (scaling up) moves the work to a larger instance, for example from
m5.largetom5.2xlarge. It adds CPU, memory, and network capacity to a single machine. - Horizontal scaling (scaling out) adds more instances of the same size and shares the work between them. Scaling in removes instances again.
| Property | Vertical scaling | Horizontal scaling |
|---|---|---|
| Change required | Stop, resize, and start the instance | Launch or terminate instances |
| Downtime | Usually a restart | None if a load balancer is in front |
| Upper limit | Largest instance type available | Set by quotas, budget, and the application |
| Failure impact | The one instance is a single point of failure | Losing one instance removes only a share of capacity |
| Application requirement | Usually none | Instances must be interchangeable |
Vertical scaling is simple and sometimes the right choice, such as for a database that cannot be split. Its limits are the largest instance type and the restart needed to change size. It also leaves one machine that, if it fails, takes the whole service down.
Horizontal scaling removes those limits, but it makes a demand on the application. Any instance must be able to serve any request, and instances must be able to appear and disappear at any time.
Making instances interchangeable
Interchangeable instances are usually called stateless. In practice this means:
- Session data is kept outside the instance, for example in ElastiCache or DynamoDB, or in a signed token that the client carries.
- Uploaded files go to Amazon S3 or Amazon EFS, not to the instance's local disk.
- Configuration comes from the launch template, user data, Systems Manager Parameter Store, or Secrets Manager, not from hand edits on a running server.
- Logs are shipped off the instance, for example to CloudWatch Logs, before the instance can be terminated.
If your application cannot meet these conditions yet, horizontal scaling will cause errors such as users being logged out or files going missing when an instance is replaced.
Availability Zones and high availability
An Availability Zone is one or more discrete data centers in an AWS Region, with independent power, cooling, and networking. AZs in a Region are connected by low-latency links. A subnet exists in exactly one AZ, so the subnets you choose decide which AZs your instances can run in.
High availability means the service keeps working when a component fails. A fleet of ten instances in one AZ is scalable but not highly available. If that AZ has a problem, all ten instances are affected at once. Spreading the same fleet across two or three AZs means an AZ failure removes only part of the capacity.
Planning for the loss of an AZ
Spreading across AZs only helps if the remaining AZs can carry the load. Work through a simple example. Your service needs 6 instances' worth of capacity at peak.
| Layout | Instances per AZ | Capacity left after losing one AZ | Enough for peak? |
|---|---|---|---|
| 2 AZs, 6 instances | 3 and 3 | 3 | No |
| 2 AZs, 12 instances | 6 and 6 | 6 | Yes |
| 3 AZs, 9 instances | 3, 3, and 3 | 6 | Yes |
Three AZs need fewer total instances than two to survive the loss of one zone, because the lost share is smaller. This is one reason many designs use three AZs when the Region offers them. The trade-off is that cross-AZ traffic can add data transfer charges, which a later chapter on cost covers.
How the pieces fit together
Horizontal scaling across AZs needs three things working together.
- Subnets in at least two AZs, which give instances somewhere to run.
- A load balancer that sends each request to a healthy instance in any of those AZs.
- An Auto Scaling group that adds, removes, and replaces instances to hold the desired capacity.
The load balancer gives clients one stable DNS name. The instances behind it can change at any time without clients noticing. The Auto Scaling group supplies the "elastic" part: it launches instances when demand rises and terminates them when it falls.
Seeing your AZs from the CLI
Before building anything, check which AZs your Region offers and which of your subnets sit in them. This Python script lists both. It assumes your credentials and default Region are already configured.
import boto3
ec2 = boto3.client("ec2")
zones = ec2.describe_availability_zones(
Filters=[{"Name": "state", "Values": ["available"]}]
)
print("Available zones:")
for zone in zones["AvailabilityZones"]:
print(" ", zone["ZoneName"], zone["ZoneId"])
subnets = ec2.describe_subnets()
print("Subnets:")
for subnet in subnets["Subnets"]:
print(" ", subnet["SubnetId"], subnet["AvailabilityZone"], subnet["CidrBlock"])Note that the output shows both a zone name such as us-east-1a and a zone ID such as use1-az1. The zone name is mapped to a physical zone separately for each AWS account, so us-east-1a in your account may not be the same physical zone as us-east-1a in another account. Use the zone ID when you need to compare zones across accounts.
Common mistakes
- Putting every subnet in one AZ. The Auto Scaling group and load balancer then look redundant but share one failure domain. Check the AZ of each subnet you pass in.
- Sizing for the normal case only. A fleet that needs every instance at peak has no headroom when a zone fails.
- Keeping state on the instance. Scale-in and replacement delete local data.
- Assuming scaling is instant. New instances need time to launch and pass health checks. Later chapters on warmup and scaling policies cover how to account for this.
Exercise
A service needs capacity equal to 8 instances at peak and must survive the loss of one AZ without dropping below that capacity. How many instances do you need in total if you use 2 AZs, and how many if you use 4 AZs, assuming instances are spread evenly?
Show answer
With 2 AZs, losing one leaves half the fleet, so you need 16 instances (8 in each). With 4 AZs, losing one leaves three quarters of the fleet, so you need 8 / 0.75, which rounds up to 11 instances. Spread evenly that is 3, 3, 3, and 2, so check the worst case: losing a zone with 3 leaves 8 instances, which is enough. More AZs lower the extra capacity needed, but each added AZ adds cross-AZ traffic and operational complexity.
Exercise
Name two reasons an application might not be ready for horizontal scaling, and one change that fixes each.
Show answer
Sessions stored in instance memory: move session data to ElastiCache or DynamoDB, or use signed tokens. Uploads written to the local disk: store them in Amazon S3 or EFS. Other valid answers include hand-edited configuration (move it to the launch template or Parameter Store) and logs kept only on the instance (ship them to CloudWatch Logs).