Spot Instances for cost savings in AWS - Time & Space Complexity
When using Spot Instances, it's important to understand how the number of instance requests affects the overall process time.
We want to know how the time to get and manage Spot Instances grows as we ask for more instances.
Analyze the time complexity of the following operation sequence.
// Request multiple Spot Instances
aws ec2 request-spot-instances --spot-price "0.05" --instance-count 5 --type "one-time" --launch-specification file://spec.json
// Wait for instances to be fulfilled
aws ec2 describe-spot-instance-requests --filters Name=state,Values=active
// Use instances
// Terminate instances when done
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0 i-abcdef01234567890
This sequence requests several Spot Instances, waits for them to be active, then terminates them after use.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Requesting Spot Instances via the request-spot-instances API call.
- How many times: Once per batch request, but the number of instances requested affects how long it takes to fulfill.
- Secondary operation: Polling describe-spot-instance-requests to check fulfillment status, repeated until all instances are active.
As you request more Spot Instances, the time to fulfill and manage them grows roughly in direct proportion to the number requested.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 1 instance request batch and multiple polling cycles |
| 100 | About 1 instance request batch and multiple polling cycles |
| 1000 | About 1 instance request batch and multiple polling cycles |
Pattern observation: The time and API calls grow roughly linearly as you increase the number of Spot Instances requested.
Time Complexity: O(n)
This means the time to request and manage Spot Instances grows in a straight line as you ask for more instances.
[X] Wrong: "Requesting more Spot Instances at once will take the same time as requesting just one."
[OK] Correct: Each additional instance adds to the total time because AWS must find capacity and fulfill each request, so time grows with the number requested.
Understanding how Spot Instance requests scale helps you design cost-effective and efficient cloud solutions, a valuable skill in real-world cloud work.
"What if we changed from requesting Spot Instances one batch at a time to requesting them in parallel batches? How would the time complexity change?"