Spot VMs for cost savings in Azure - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to deploy and manage Spot VMs changes as we increase the number of VMs.
How does adding more Spot VMs affect the number of operations and time needed?
Analyze the time complexity of creating multiple Spot VMs in Azure.
// Create multiple Spot VMs
for (int i = 0; i < vmCount; i++) {
var vm = new VirtualMachine()
.WithSpotPriority(true)
.WithEvictionPolicy(VirtualMachineEvictionPolicy.Deallocate)
.Create();
}
This sequence creates a number of Spot VMs, each with a spot priority and eviction policy set.
Look at what repeats as we add more VMs.
- Primary operation: Creating a single Spot VM with specific settings.
- How many times: Once per VM, so vmCount times.
Each VM creation is a separate operation, so more VMs mean more operations.
| Input Size (vmCount) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 VM creation calls |
| 100 | 100 VM creation calls |
| 1000 | 1000 VM creation calls |
Pattern observation: The number of operations grows directly with the number of VMs.
Time Complexity: O(n)
This means the time to create Spot VMs grows linearly as you add more VMs.
[X] Wrong: "Creating multiple Spot VMs happens all at once, so time stays the same no matter how many VMs."
[OK] Correct: Each VM creation is a separate call and takes time, so more VMs mean more total time.
Understanding how operations scale with input size helps you design efficient cloud deployments and explain your reasoning clearly.
What if we used a batch API to create multiple Spot VMs at once? How would the time complexity change?
Practice
Solution
Step 1: Understand Spot VM purpose
Spot VMs use unused cloud capacity to offer lower prices.Step 2: Compare benefits
Unlike regular VMs, Spot VMs are cheaper but can be evicted when capacity is needed.Final Answer:
They provide cheaper compute by using spare capacity. -> Option CQuick Check:
Spot VMs = cheaper compute [OK]
- Thinking Spot VMs guarantee uptime
- Confusing Spot VMs with auto-scaling
- Assuming Spot VMs provide extra storage
Solution
Step 1: Recall Azure CLI syntax for Spot VMs
The correct parameter to set Spot VM priority is --priority Spot.Step 2: Evaluate options
The other options use incorrect or non-existent flags.Final Answer:
az vm create --priority Spot -> Option BQuick Check:
Spot VM priority flag = --priority Spot [OK]
- Using incorrect flags like --spot-priority
- Setting priority to High instead of Spot
- Assuming --enable-spot is valid
az vm create --name mySpotVM --image UbuntuLTS --priority Spot --max-price 0.05What happens if the current Spot price exceeds 0.05 USD/hour?
Solution
Step 1: Understand max-price setting
The max-price limits the Spot VM cost; if price rises above it, eviction occurs.Step 2: Analyze behavior when price exceeds max-price
Spot VMs are stopped or deallocated automatically when price exceeds max-price.Final Answer:
The VM is evicted (stopped) automatically. -> Option DQuick Check:
Price > max-price = VM eviction [OK]
- Thinking VM keeps running at higher price
- Assuming price is capped automatically
- Believing VM converts to regular VM
az vm create --name testVM --image UbuntuLTS --priority Spot --max-price -2What is the issue with this command?
Solution
Step 1: Check max-price parameter rules
max-price must be greater than or equal to -1; other negative values are invalid.Step 2: Identify error cause
Using -2 for max-price causes a validation error during VM creation.Final Answer:
max-price cannot be negative; it causes an error. -> Option AQuick Check:
max-price < -1 = error [OK]
- Using negative max-price values
- Thinking Spot priority syntax is wrong
- Assuming UbuntuLTS is unsupported
Solution
Step 1: Choose Spot VM with cost control
Setting priority to Spot and max-price low saves cost but risks eviction.Step 2: Enable automatic redeployment
Automatic redeployment restarts the VM if evicted, ensuring job resumes.Final Answer:
Set VM priority to Spot, max-price to a low value, and enable automatic redeployment. -> Option AQuick Check:
Spot + max-price + auto redeploy = cost saving + restart [OK]
- Using negative max-price to avoid eviction
- Disabling automatic redeployment
- Choosing regular VMs for cost savings
