Sub topic 10
Diversifying Spot capacity and choosing an allocation strategy
This chapter assumes you know how Spot Instances work and how the two-minute interruption notice behaves. It covers how to reduce how often interruptions happen, and how to pick the strategy that decides which Spot capacity you receive.
Spot capacity pools
Spot capacity is not one big pool. AWS tracks spare capacity separately for each combination of instance type, Availability Zone, and Region. Each combination is a Spot capacity pool. For example, c5.large in us-east-1a is one pool, and c5.large in us-east-1b is a different one.
Each pool has its own price and its own chance of running short. When AWS needs capacity back for On-Demand customers, it reclaims instances from specific pools. A workload that uses one pool is exposed to that pool's behavior. A workload spread over many pools is unlikely to lose all of its capacity at once.
This leads to the main rule of Spot design: be flexible about instance type and Availability Zone. Flexibility gives your allocation strategy more pools to choose from.
Ways to diversify
You can diversify along three dimensions.
- Availability Zones. Give your Auto Scaling group or fleet subnets in every Availability Zone of the Region that your application can use.
- Instance types within a family.
c5.largeandc5a.largehave the same vCPU and memory, so they are usually interchangeable. - Instance families and generations.
m5.large,m5a.large, andm6i.largeall have 2 vCPUs and 8 GiB of memory.c5.largehas 2 vCPUs and 4 GiB. Including both c and m types works if the application is happy with 4 GiB.
Keep the types you list comparable in size. If your application needs 2 vCPUs and 8 GiB, a type with 4 GiB of memory will cause failures. Listing it just to widen the pool count is a mistake.
If listing types by hand is tedious, attribute-based instance type selection lets you describe requirements instead, such as a vCPU range and a memory range. EC2 then uses every current-generation type that matches, including new ones released later. This is often the easiest way to get a large number of pools.
Allocation strategies
An allocation strategy tells EC2 which pools to launch from when you ask for Spot capacity. The options for Auto Scaling groups and EC2 Fleet are in the table.
| Strategy | What it optimizes | Use it when |
|---|---|---|
| `price-capacity-optimized` | Picks pools that have both low price and deep spare capacity | Default choice for most workloads |
| `capacity-optimized` | Picks pools with the deepest spare capacity, ignoring price | Interruptions are costly, such as long batch jobs or CI runners |
| `capacity-optimized-prioritized` | Follows your type priority order, but still favors capacity | Some types are preferred, for example because of licensing or a better commitment match |
| `lowest-price` | Picks the cheapest pools | Rarely. It concentrates on the cheapest pools and raises interruption risk |
| `diversified` (EC2 Fleet and Spot Fleet only) | Spreads evenly across all pools | You want to spread risk and do not care about price differences |
The two strategies you will use most are explained below.
price-capacity-optimized looks at two signals. It identifies pools with the lowest chance of interruption, then chooses the lowest-priced among them. It does not simply chase the cheapest pool. AWS recommends it for most workloads, and it is the choice in the example below.
lowest-price is the old default. It picks the cheapest pool, and cheap pools are often cheap because many people are bidding for them and spare capacity is thin. With lowest-price, you can also set SpotInstancePools to spread across the N cheapest pools. This helps a little, but a pool being cheap does not make it safe. Treat lowest-price as a legacy option.
Spot price volatility is low now. Prices change slowly, and the interruption rate matters more than price. That is why the strategies above prioritize capacity depth over price.
A mixed instances policy
The next policy keeps two On-Demand instances as a baseline, so a Savings Plan or Reserved Instance can cover them. Everything above that baseline is Spot, spread over three instance types.
{
"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"
}
}Three fields matter here.
Overrideslists the instance types. Each type, multiplied by each Availability Zone of your subnets, forms a pool. Three types over three zones give nine pools.OnDemandBaseCapacity: 2means the first two instances are always On-Demand.OnDemandPercentageAboveBaseCapacity: 0means every instance above the base is Spot. A value of 20 would make 20% of the additional instances On-Demand.
Save the policy as mixed-policy.json and use it to create the group. The subnet IDs are placeholders, so replace them with subnets in three different Availability Zones.
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name web-asg \
--min-size 2 \
--max-size 10 \
--vpc-zone-identifier "subnet-0aaa1111,subnet-0bbb2222,subnet-0ccc3333" \
--mixed-instances-policy file://mixed-policy.json \
--capacity-rebalanceThe --capacity-rebalance flag turns on Capacity Rebalancing. Spot sometimes sends a rebalance recommendation before an interruption notice, signaling that an instance is at elevated risk. With this flag, the group launches a replacement as soon as that signal arrives, then terminates the at-risk instance. The replacement is usually ready before the two-minute notice would have started.
Checking whether a request will succeed
Before you commit to a configuration, you can ask EC2 how likely a Spot request is to succeed. The Spot placement score rates a set of instance types and Regions on a scale of 1 to 10, where higher means a better chance of getting the capacity you asked for.
aws ec2 get-spot-placement-scores \
--instance-types c5.large c5a.large m5.large \
--target-capacity 20 \
--region-names us-east-1 us-east-2 us-west-2The result lists each Region with a score. Use it to choose a Region for a new workload, or to test whether adding more instance types raises the score. The score reflects the moment you ask. It does not guarantee future availability.
Edge cases and failure modes
- Too few types. One type in three zones gives three pools. A capacity shortage in that type affects all three. Aim for at least ten pools where the application allows it.
- Mismatched sizes. Auto Scaling can weight each type through the
WeightedCapacitysetting inOverrides. If you mixlargeand2xlarge, set weights of 1 and 4 so that the group's capacity counts units of work, not instances. - Missing zones. If a subnet for one Availability Zone is left out, the group cannot use pools there. Check your subnet list when diversifying.
- Commitments only match On-Demand usage in effect. Savings Plans and Reserved Instances discount eligible usage, including Spot-adjacent baseline instances only when they run On-Demand. Spot usage is not covered by either, so do not size a commitment to include it.
- Prioritized strategy with a bad order.
capacity-optimized-prioritizedis best-effort. If your top-priority type is in short supply, EC2 favors capacity over your order. - Stateful workloads. Diversification lowers the chance of interruption but never removes it. Applications must still handle the two-minute notice by draining connections and saving state.
Exercise
An Auto Scaling group runs a mixed instances policy with two instance types and one subnet. Spot interruptions are frequent. Name two changes that reduce them, and the allocation strategy you would set.
Show answer
Add more instance types of comparable size, and add subnets in the other Availability Zones, so there are more Spot pools to choose from. Set the strategy to price-capacity-optimized, and turn on Capacity Rebalancing so replacements launch when a rebalance recommendation arrives.
Exercise
A policy lists three instance types and three subnets in different Availability Zones. How many Spot capacity pools does the group have, and what happens to pool count if you add a fourth type?
Show answer
Three types times three zones gives nine pools. A fourth type makes twelve pools, which gives the allocation strategy more options.