EC2 pricing models (on-demand, reserved, spot) in AWS - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the cost and usage of EC2 instances grow when using different pricing models.
How does the number of instances and their usage affect the overall cost and resource allocation?
Analyze the time complexity of launching EC2 instances with different pricing models.
# Launch EC2 instances with spot and on-demand pricing models; purchase Reserved Instance
aws ec2 run-instances --image-id ami-12345678 --count 1 --instance-type t3.micro --instance-market-options MarketType=spot
aws ec2 run-instances --image-id ami-12345678 --count 1 --instance-type t3.micro
aws ec2 purchase-reserved-instances-offering --reserved-instances-offering-id abcdef12-3456-7890-abcd-ef1234567890 --instance-count 1
This sequence launches one spot instance, one on-demand instance, and purchases one Reserved Instance.
Look at the main actions that happen repeatedly when scaling instances.
- Primary operation: Launching EC2 instances (run-instances API call)
- How many times: Once per instance requested
- Other operations: Purchasing reserved instances (one-time per reservation)
When you increase the number of instances, the number of API calls to launch them grows directly with that number.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 run-instances calls + reserved purchases if any |
| 100 | 100 run-instances calls + reserved purchases if any |
| 1000 | 1000 run-instances calls + reserved purchases if any |
Pattern observation: The number of instance launches grows linearly with the number of instances requested.
Time Complexity: O(n)
This means the effort to launch instances grows directly in proportion to how many you want to run.
[X] Wrong: "Launching more spot instances costs less time and effort because they are cheaper."
[OK] Correct: The pricing affects cost, but the number of API calls and provisioning steps still grows with the number of instances regardless of pricing model.
Understanding how resource requests scale helps you design systems that handle growth smoothly and predict costs clearly.
"What if we switched from on-demand to spot instances for all launches? How would the time complexity of launching instances change?"
Practice
Solution
Step 1: Understand On-demand pricing
On-demand instances let you pay per hour or second without any upfront commitment.Step 2: Compare with other models
Reserved requires commitment; Spot can be interrupted; Dedicated hosts are physical servers.Final Answer:
On-demand instances -> Option DQuick Check:
Pay-as-you-go = On-demand [OK]
- Confusing Reserved with On-demand
- Thinking Spot is always available
- Mixing Dedicated hosts with pricing models
Solution
Step 1: Define Spot instances
Spot instances use spare AWS capacity and are cheaper but can be stopped anytime.Step 2: Eliminate other options
Fixed monthly fee applies to Reserved; dedicated hardware is Dedicated hosts.Final Answer:
Instances that use spare capacity and can be interrupted -> Option BQuick Check:
Spot = spare capacity + interruption [OK]
- Thinking Spot has fixed fees
- Confusing Reserved with Spot
- Mixing Dedicated hosts with Spot
Solution
Step 1: Understand Spot instance interruptions
AWS can reclaim Spot instances anytime, giving a 2-minute warning before stopping or terminating.Step 2: Check other options
Instances do not continue uninterrupted; no automatic price change or conversion to Reserved.Final Answer:
Your instance is stopped or terminated with a 2-minute warning -> Option AQuick Check:
Spot interruption = 2-minute warning stop [OK]
- Assuming Spot instances never stop
- Thinking Spot switches to On-demand automatically
- Believing Spot instances run like Reserved
Solution
Step 1: Understand On-demand pricing characteristics
On-demand pricing charges per use with no upfront or commitment discounts.Step 2: Identify the cost-saving option
Reserved instances offer savings with 1 or 3 year commitments, unlike On-demand.Final Answer:
On-demand pricing does not offer cost savings for long-term commitment -> Option AQuick Check:
Commitment savings = Reserved, not On-demand [OK]
- Thinking On-demand requires upfront payment
- Believing On-demand instances are delayed
- Confusing interruption with Spot pricing
Solution
Step 1: Analyze availability and cost needs
Critical 24/7 apps need stable availability and predictable costs.Step 2: Match pricing models to needs
Reserved instances provide cost savings with 1 or 3 year commitment and stable availability; Spot can be interrupted; On-demand is flexible but more costly.Final Answer:
Reserved instances, because they offer cost savings with a commitment and stable availability -> Option CQuick Check:
Critical + savings + commitment = Reserved [OK]
- Choosing Spot for critical apps despite interruptions
- Picking On-demand for cost savings
- Confusing Dedicated hosts with pricing benefits
