Bird
Raised Fist0
GCPcloud~10 mins

Preemptible and Spot VMs in GCP - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Preemptible and Spot VMs
User requests VM
Check VM type
Preemptible?
Create VM
Run VM
Google may stop VM anytime
VM stopped or deleted
User can restart or create new VM
When you request a VM as preemptible or spot, Google creates it but can stop it anytime to save resources. You can restart or create new VMs as needed.
Execution Sample
GCP
gcloud compute instances create example-vm \
  --zone=us-central1-a \
  --preemptible
This command creates a preemptible VM that runs until Google stops it, usually within 24 hours.
Process Table
StepActionVM StateGoogle's DecisionUser Impact
1User requests preemptible VMVM not createdCheck resource availabilityWaiting for VM creation
2Google creates VMVM runningVM runs normallyUser can use VM
3Google needs resourcesVM runningDecides to preempt VMVM will be stopped soon
4Google stops VMVM stoppedVM preemptedUser loses VM, must restart or create new
5User restarts or creates new VMVM runningVM runs normallyUser continues work
6VM runs up to 24 hoursVM runningVM may be preempted anytimeUser should save work often
7VM reaches max runtimeVM stoppedVM automatically stoppedUser must restart or create new
8EndVM stopped or runningNo further actionUser manages VM lifecycle
💡 VM stops when Google reclaims resources or max runtime reached; user must manage restarts.
Status Tracker
VariableStartAfter Step 2After Step 4After Step 5After Step 7Final
VM StateNot createdRunningStoppedRunningStoppedStopped or Running
Google DecisionNoneRun VMPreempt VMRun VMStop VMNo action
User ImpactWaitingUsing VMVM lostUsing VMMust restartManage VM
Key Moments - 3 Insights
Why does the VM stop even if I did not ask it to?
Google can stop preemptible or spot VMs anytime to free resources, as shown in execution_table step 4 where Google preempts the VM.
Can I keep a preemptible VM running forever?
No, preemptible VMs have a max runtime (usually 24 hours) after which Google stops them automatically (see step 7).
What should I do when my preemptible VM stops?
You need to restart or create a new VM to continue work, as shown in step 5 where the user restarts the VM.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the VM state?
ARunning
BStopped
CNot created
DRestarting
💡 Hint
Check the 'VM State' column in row for step 4 in execution_table.
At which step does the user restart or create a new VM?
AStep 3
BStep 7
CStep 5
DStep 2
💡 Hint
Look for 'User restarts or creates new VM' in the 'Action' column.
If Google never needed resources, what would happen to the VM state in the table?
AVM would keep running until max runtime
BVM would stop at step 4
CVM would never be created
DVM would restart automatically
💡 Hint
Refer to step 6 and 7 about VM running and max runtime.
Concept Snapshot
Preemptible and Spot VMs are cheaper VMs that Google can stop anytime.
You create them like normal VMs but expect interruptions.
Google stops them to free resources or after max runtime (~24h).
You must save work often and be ready to restart or create new VMs.
Ideal for batch jobs or fault-tolerant workloads.
Full Transcript
When you create a preemptible or spot VM on Google Cloud, the VM runs normally but can be stopped by Google at any time to free resources. This stopping is called preemption. The VM can run up to about 24 hours before Google stops it automatically. When the VM stops, you lose it and must restart or create a new one to continue your work. This makes preemptible and spot VMs cheaper but less reliable. They are good for tasks that can handle interruptions. The execution table shows the VM state changes from creation, running, preemption, stopping, and restarting. The variable tracker shows how VM state, Google's decisions, and user impact change step by step. Key moments clarify why VMs stop unexpectedly and what users should do. The quiz tests understanding of VM states and lifecycle steps. Remember, always save your work often and plan for VM interruptions when using preemptible or spot VMs.

Practice

(1/5)
1. What is the main advantage of using Preemptible or Spot VMs in Google Cloud?
easy
A. They offer unlimited storage capacity
B. They provide guaranteed uptime for critical applications
C. They cost less but can be stopped at any time
D. They automatically scale without user input

Solution

  1. Step 1: Understand the cost and availability trade-off

    Preemptible and Spot VMs are cheaper because Google can stop them anytime to reclaim resources.
  2. Step 2: Identify the main benefit

    The main benefit is cost savings with the risk of interruption, not guaranteed uptime or unlimited storage.
  3. Final Answer:

    They cost less but can be stopped at any time -> Option C
  4. Quick Check:

    Cost savings with interruptions = D [OK]
Hint: Cheaper VMs can be stopped anytime, so cost saving is main benefit [OK]
Common Mistakes:
  • Thinking they guarantee uptime
  • Assuming they scale automatically
  • Confusing with storage features
2. Which of the following is the correct way to specify a Spot VM in a Google Cloud VM creation command?
easy
A. gcloud compute instances create my-vm --spot
B. gcloud compute instances create my-vm --interruptible
C. gcloud compute instances create my-vm --preemptible
D. gcloud compute instances create my-vm --ephemeral

Solution

  1. Step 1: Recall the flag for Spot VMs

    Spot VMs use the flag --spot in the gcloud command.
  2. Step 2: Differentiate from Preemptible flag

    --preemptible is for older Preemptible VMs, --spot is the newer recommended option.
  3. Final Answer:

    gcloud compute instances create my-vm --spot -> Option A
  4. Quick Check:

    Spot VM flag = --spot [OK]
Hint: Spot VMs use --spot flag, not --preemptible [OK]
Common Mistakes:
  • Using --preemptible for Spot VMs
  • Confusing --interruptible as a flag
  • Using unrelated flags like --ephemeral
3. Consider this snippet of a VM creation command:
gcloud compute instances create test-vm --zone=us-central1-a --spot --machine-type=e2-medium
What will happen if Google Cloud needs the resources back?
medium
A. The VM will automatically migrate to another zone
B. The VM will continue running without interruption
C. The VM will be stopped immediately without warning
D. The VM will receive a 30-second warning before stopping

Solution

  1. Step 1: Understand Spot VM behavior on resource reclamation

    Spot VMs are interruptible but Google Cloud sends a 30-second warning before stopping them.
  2. 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.
  3. Final Answer:

    The VM will receive a 30-second warning before stopping -> Option D
  4. Quick Check:

    Spot VMs get 30-second warning before stop = A [OK]
Hint: Spot VMs get 30-second warning before stopping [OK]
Common Mistakes:
  • Assuming immediate stop without warning
  • Thinking Spot VMs migrate automatically
  • Believing Spot VMs never stop
4. You created a VM with the command:
gcloud compute instances create my-vm --preemptible --zone=us-east1-b
But you want to switch to Spot VM instead. What is the correct fix?
medium
A. Add --spot flag without removing --preemptible
B. Replace --preemptible with --spot in the command
C. Change the zone to a Spot-only zone
D. Use --interruptible flag instead of --preemptible

Solution

  1. Step 1: Identify the correct flag for Spot VMs

    Spot VMs require the --spot flag, not --preemptible.
  2. Step 2: Correct the command by replacing flags

    Remove --preemptible and add --spot to switch VM type.
  3. Final Answer:

    Replace --preemptible with --spot in the command -> Option B
  4. Quick Check:

    Switching VM type requires flag replacement = B [OK]
Hint: Remove --preemptible, add --spot to switch VM type [OK]
Common Mistakes:
  • Adding --spot without removing --preemptible
  • Changing zone expecting Spot-only zones
  • Using non-existent --interruptible flag
5. You want to run a batch job that can tolerate interruptions and save costs. Which approach best uses Spot VMs to handle sudden stops and restarts?
hard
A. Design the job to save progress frequently and restart automatically on VM preemption
B. Use Spot VMs without any checkpointing or restart logic
C. Run the job on standard VMs to avoid interruptions
D. Use Spot VMs but disable automatic restarts

Solution

  1. Step 1: Understand Spot VM interruption nature

    Spot VMs can stop anytime, so jobs must handle interruptions gracefully.
  2. Step 2: Choose a strategy to handle interruptions

    Saving progress frequently and restarting automatically ensures job completion despite stops.
  3. Step 3: Eliminate unsafe options

    Running without checkpointing risks data loss; standard VMs cost more; disabling restarts loses fault tolerance.
  4. Final Answer:

    Design the job to save progress frequently and restart automatically on VM preemption -> Option A
  5. Quick Check:

    Checkpointing + auto-restart = reliable Spot VM use [OK]
Hint: Checkpoint progress and auto-restart for Spot VM jobs [OK]
Common Mistakes:
  • Ignoring checkpointing and restart logic
  • Choosing standard VMs for cost savings
  • Disabling automatic restarts on Spot VMs