Sub topic 20
Designing for resilience and controlling cost
This chapter assumes you can already create a launch template, an Auto Scaling group (ASG), an Application Load Balancer (ALB) and a target tracking policy, as in the earlier chapters. It covers how to combine them so the web tier survives failures and does not cost more than it must.
Resilience: plan for losing a zone
An Availability Zone (AZ) is the failure unit you design around. Spreading instances across zones only helps if the remaining zones can carry the load when one disappears.
Capacity math decides this. If you run in two AZs and one fails, the other must absorb 100% of the traffic, so each zone can run at only 50% of its capacity during normal operation. With three AZs, the two survivors absorb the load, so each zone can run at about 67%. More zones means less idle headroom for the same protection.
| AZs used | Share of traffic each survivor takes after one AZ fails | Max normal utilization per AZ |
|---|---|---|
| 2 | 100% | 50% |
| 3 | 50% | about 67% |
| 4 | 33% | 75% |
Apply this in three places:
- Subnets: give the ASG (
--vpc-zone-identifier) and the load balancer a subnet in each zone you rely on. - Minimum size: set
min-sizeto at least the number of zones, so every zone keeps an instance. A minimum of 1 in a two-zone group means a zone failure can leave you with zero healthy targets until a replacement launches. - Maximum size: leave room for the extra instances that surviving zones launch after a failure. When an AZ becomes unavailable, the ASG launches replacements in the other zones, which only works if
max-sizeallows it.
Health checks and graceful removal
Keep --health-check-type ELB on the ASG so instances the load balancer marks unhealthy are replaced, not just left registered. Set the grace period longer than your slowest boot time, or instances get terminated before they finish starting.
Keep the deregistration delay close to your real request duration. The default of 300 seconds protects long requests but slows deployments and scale-in. If your requests finish in a few seconds, a value like 30 is usually enough.
Avoid hidden single points of failure
- A target group with only one zone's targets turns a zone failure into an outage.
- Dependencies matter as much as the web tier. A single-AZ database or cache makes the multi-AZ web tier decorative. Use Multi-AZ options for those too.
- An NLB has cross-zone load balancing off by default. With it off, each load balancer node sends traffic only to targets in its own zone, so an uneven number of targets per zone gives uneven load.
Cost: pay for the capacity you use
Most of the bill comes from instances, then data transfer, then load balancer hours. Work through them in that order.
Right-size the baseline and the curve
- Choose the instance size from measured utilization. A fleet averaging 15% CPU is oversized.
- Use target tracking so scale-in happens automatically. A policy that only scales out leaves you paying for peak capacity all day.
- Add scheduled actions if load is predictable, such as a lower
min-sizeat night. - Cover the steady baseline (your
min-sizeinstances) with a Savings Plan or Reserved Instances, and leave the elastic part On-Demand or Spot.
Use Spot for the elastic part
A mixed instances policy keeps a fixed On-Demand base and fills the rest with Spot. List several instance types so a shortage of one type does not stop scaling.
{
"LaunchTemplate": {
"LaunchTemplateSpecification": {
"LaunchTemplateName": "web-lt",
"Version": "$Latest"
},
"Overrides": [
{ "InstanceType": "m6i.large" },
{ "InstanceType": "m6a.large" },
{ "InstanceType": "m5.large" }
]
},
"InstancesDistribution": {
"OnDemandBaseCapacity": 2,
"OnDemandPercentageAboveBaseCapacity": 25,
"SpotAllocationStrategy": "price-capacity-optimized"
}
}Save this as mixed.json and apply it:
aws autoscaling update-auto-scaling-group \
--auto-scaling-group-name web-asg \
--mixed-instances-policy file://mixed.jsonThis keeps two On-Demand instances always, then runs 25% of everything above that on On-Demand and 75% on Spot.
Reduce load balancer and transfer costs
- One ALB can serve many services through host-based and path-based rules and multiple target groups. Running one ALB per small service multiplies the hourly charge.
- Delete load balancers, target groups and ASGs left over from tests. An idle load balancer still bills hourly.
- Cross-zone traffic is free of inter-AZ data charges on an ALB, but when you enable cross-zone on an NLB, data crossing zones is charged. Weigh the even load against that cost.
- Keep chatty traffic inside a zone where possible, for example by not routing every request to a database in another zone.
Putting the checklist together
- Subnets in at least two, preferably three, AZs for both the ASG and the load balancer.
min-sizecovers the zone-loss case, andmax-sizeallows recovery.- ELB health checks, a realistic grace period and a tuned deregistration delay.
- Target tracking for scaling, scheduled actions for known patterns.
- Savings Plan or Reserved Instances for the baseline, Spot for the burst.
- Dependencies (database, cache) are also multi-AZ.
Exercise
Your service needs 12 instances to handle peak traffic, and it must keep serving peak traffic if one AZ fails. How many instances do you need in total if you use 2 AZs, and how many with 3 AZs? Which design costs less?
Show answer
With 2 AZs, the surviving zone must hold all 12, so each zone needs 12 and the total is 24. With 3 AZs, the two surviving zones must hold 12 together, so each needs 6 and the total is 18. The 3-AZ design costs less (18 instead of 24 instances) while giving the same protection.