Preemptible and Spot VMs in GCP - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to run workloads on Preemptible and Spot VMs changes as we increase the number of tasks.
Specifically, how does the chance of VM interruptions affect the total time to complete all tasks?
Analyze the time complexity of launching multiple Preemptible or Spot VMs to run tasks.
// Pseudocode for launching N Preemptible VMs
for i in 1 to N:
create VM instance with preemptible flag
run task on VM
if VM is preempted:
restart task on new VM
else:
complete task
This sequence runs N tasks on VMs that can be stopped anytime, requiring restarts.
Look at what happens repeatedly when running tasks on Preemptible or Spot VMs.
- Primary operation: Creating VM instances and running tasks.
- How many times: At least N times, but possibly more if VMs are preempted and tasks restart.
As the number of tasks (N) grows, the number of VM creations grows at least linearly.
| Input Size (n) | Approx. VM Creations |
|---|---|
| 10 | 10 to 15 (some restarts) |
| 100 | 100 to 150 (more restarts possible) |
| 1000 | 1000 to 1500 or more (many restarts) |
Pattern observation: The total VM creations grow roughly linearly with tasks, but preemptions add extra restarts increasing total operations.
Time Complexity: O(n)
This means the total time and VM operations grow roughly in direct proportion to the number of tasks, with some extra overhead from restarts.
[X] Wrong: "Preemptible VMs always finish tasks without interruption, so time grows exactly with task count."
[OK] Correct: Preemptible and Spot VMs can stop anytime, causing tasks to restart and increasing total operations beyond just the task count.
Understanding how interruptions affect workload time shows you can plan for real cloud behavior, a key skill for designing reliable systems.
"What if we switched from Preemptible VMs to regular VMs without interruptions? How would the time complexity change?"
Practice
Solution
Step 1: Understand the cost and availability trade-off
Preemptible and Spot VMs are cheaper because Google can stop them anytime to reclaim resources.Step 2: Identify the main benefit
The main benefit is cost savings with the risk of interruption, not guaranteed uptime or unlimited storage.Final Answer:
They cost less but can be stopped at any time -> Option CQuick Check:
Cost savings with interruptions = D [OK]
- Thinking they guarantee uptime
- Assuming they scale automatically
- Confusing with storage features
Solution
Step 1: Recall the flag for Spot VMs
Spot VMs use the flag--spotin the gcloud command.Step 2: Differentiate from Preemptible flag
--preemptibleis for older Preemptible VMs,--spotis the newer recommended option.Final Answer:
gcloud compute instances create my-vm --spot -> Option AQuick Check:
Spot VM flag = --spot [OK]
- Using --preemptible for Spot VMs
- Confusing --interruptible as a flag
- Using unrelated flags like --ephemeral
gcloud compute instances create test-vm --zone=us-central1-a --spot --machine-type=e2-mediumWhat will happen if Google Cloud needs the resources back?
Solution
Step 1: Understand Spot VM behavior on resource reclamation
Spot VMs are interruptible but Google Cloud sends a 30-second warning before stopping them.Step 2: Eliminate other options
Immediate stop without warning is incorrect; automatic migration does not happen for Spot VMs; uninterrupted running contradicts the interruptible nature.Final Answer:
The VM will receive a 30-second warning before stopping -> Option DQuick Check:
Spot VMs get 30-second warning before stop = A [OK]
- Assuming immediate stop without warning
- Thinking Spot VMs migrate automatically
- Believing Spot VMs never stop
gcloud compute instances create my-vm --preemptible --zone=us-east1-bBut you want to switch to Spot VM instead. What is the correct fix?
Solution
Step 1: Identify the correct flag for Spot VMs
Spot VMs require the--spotflag, not--preemptible.Step 2: Correct the command by replacing flags
Remove--preemptibleand add--spotto switch VM type.Final Answer:
Replace --preemptible with --spot in the command -> Option BQuick Check:
Switching VM type requires flag replacement = B [OK]
- Adding --spot without removing --preemptible
- Changing zone expecting Spot-only zones
- Using non-existent --interruptible flag
Solution
Step 1: Understand Spot VM interruption nature
Spot VMs can stop anytime, so jobs must handle interruptions gracefully.Step 2: Choose a strategy to handle interruptions
Saving progress frequently and restarting automatically ensures job completion despite stops.Step 3: Eliminate unsafe options
Running without checkpointing risks data loss; standard VMs cost more; disabling restarts loses fault tolerance.Final Answer:
Design the job to save progress frequently and restart automatically on VM preemption -> Option AQuick Check:
Checkpointing + auto-restart = reliable Spot VM use [OK]
- Ignoring checkpointing and restart logic
- Choosing standard VMs for cost savings
- Disabling automatic restarts on Spot VMs
