Sub topic 10
Diversifying Spot capacity and choosing an allocation strategy
This chapter assumes you know what a Spot Instance is, how the two-minute interruption notice works, and how Auto Scaling groups and launch templates fit together. Here you will learn how to spread Spot capacity across many pools and how to pick the strategy that decides where AWS launches your instances.
Capacity pools
A Spot capacity pool is a set of unused EC2 instances with the same instance type, Availability Zone, and Region. Each pool has its own price and its own chance of being reclaimed. If you ask for only c5.large in one Availability Zone, you depend on a single pool. When AWS needs that capacity back, every instance you run is at risk at the same time, and a replacement may not be available.
Diversifying means giving AWS many pools to choose from. You can:
- List several instance types, including different sizes and generations.
- Include different families that have similar CPU and memory, such as
c5,c5a, andm5. - Use every Availability Zone in the Region by listing several subnets.
More pools mean fewer simultaneous interruptions and a better chance of finding capacity when you scale out.
Allocation strategies
An allocation strategy tells EC2 which pool to pick for each Spot instance it launches.
| Strategy | What it optimizes | Typical use |
|---|---|---|
| `price-capacity-optimized` | Pools with the most available capacity, then the lowest price among them | Recommended default for most workloads |
| `capacity-optimized` | Pools least likely to be interrupted | Jobs where interruptions are costly, such as long batch runs |
| `lowest-price` | Cheapest pools only | Rarely a good choice, because the cheapest pools are often the most crowded |
The lowest-price strategy is the one to watch for. It can put most of your fleet into a few cheap pools, so an interruption in one pool can take out a large share of your capacity.
A diversified Auto Scaling group
The following mixed instances policy lists three instance types and uses price-capacity-optimized. It also keeps two On-Demand instances as a stable baseline, which a Savings Plan or Reserved Instance can cover. Everything above that baseline runs on Spot.
{
"LaunchTemplate": {
"LaunchTemplateSpecification": {
"LaunchTemplateName": "web-template",
"Version": "$Latest"
},
"Overrides": [
{ "InstanceType": "c5.large" },
{ "InstanceType": "c5a.large" },
{ "InstanceType": "m5.large" }
]
},
"InstancesDistribution": {
"OnDemandBaseCapacity": 2,
"OnDemandPercentageAboveBaseCapacity": 0,
"SpotAllocationStrategy": "price-capacity-optimized"
}
}Read the fields in this order:
Overridesis the list of instance types. Each type, combined with each Availability Zone in the group, forms a Spot pool.OnDemandBaseCapacityof 2 means the first two instances are always On-Demand.OnDemandPercentageAboveBaseCapacityof 0 means every instance beyond those two is Spot.SpotAllocationStrategyselects how EC2 picks among the pools.
With three types and three Availability Zones, this group draws on nine pools.
Common mistakes
- Listing one instance type. The group has a single pool per Availability Zone, so one reclaim event can remove most of it.
- Mixing incompatible types. Types in one group should have similar vCPU and memory. Otherwise the application sizes itself for the smallest type and wastes the larger ones, or runs out of memory on the smaller ones.
- Using one subnet. A single Availability Zone cuts the number of pools by the number of zones you left out.
- Skipping interruption handling. Diversification lowers the odds of losing capacity. It does not remove the two-minute notice, so your application still needs to drain or checkpoint.
Exercise
An Auto Scaling group runs only c5.large in one subnet with the lowest-price strategy. Name two changes that reduce the risk of losing most of its Spot capacity at once.
Show answer
Add more instance types with similar vCPU and memory, such as c5a.large and m5.large, and add subnets in more Availability Zones so there are more pools. Switching to price-capacity-optimized also helps, because it favors pools with more available capacity instead of only the cheapest ones.