You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Preemptible and Spot VMs offer deep discounts (60-91% off on-demand prices) in exchange for the possibility that Google Cloud may terminate (preempt) the instance when it needs the capacity back. Understanding how they work, their limitations, and how to design fault-tolerant architectures around them is key to significant cost savings.
Spot VMs are the current recommended type of discounted, preemptible compute on Google Cloud. They replaced the legacy "Preemptible VMs" and offer the same deep discounts with additional features.
| Feature | Spot VMs | Preemptible VMs (Legacy) |
|---|---|---|
| Discount | 60-91% off on-demand | 60-91% off on-demand |
| Max lifetime | No maximum (can run indefinitely if not preempted) | 24-hour maximum |
| Availability | Based on spare capacity in the zone | Based on spare capacity in the zone |
| Preemption notice | 30-second warning via ACPI signal | 30-second warning via ACPI signal |
| Recommended | Yes — use for all new workloads | No — legacy, use Spot VMs instead |
Important: Spot VMs have replaced Preemptible VMs. Google recommends using Spot VMs for all new workloads.
# Create a Spot VM
gcloud compute instances create batch-worker-1 \
--zone=europe-west2-a \
--machine-type=n2-standard-8 \
--provisioning-model=SPOT \
--instance-termination-action=STOP \
--image-family=debian-12 \
--image-project=debian-cloud
| Action | Behaviour |
|---|---|
| STOP | VM is stopped (can be restarted later if capacity is available) |
| DELETE | VM is deleted permanently |
# Instance template with Spot provisioning
gcloud compute instance-templates create spot-worker-template \
--machine-type=n2-standard-8 \
--provisioning-model=SPOT \
--instance-termination-action=DELETE \
--image-family=debian-12 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
# Pull work from queue and process it
python3 /opt/worker/process.py'
# MIG with Spot instances
gcloud compute instance-groups managed create spot-worker-mig \
--zone=europe-west2-a \
--template=spot-worker-template \
--size=10
When Google needs the capacity back, your Spot VM receives a 30-second warning. You can detect this by polling the metadata server or listening for the ACPI G2 signal.
# Poll the metadata server for preemption notice
curl -s -H "Metadata-Flavor: Google" \
http://metadata.google.internal/computeMetadata/v1/instance/preempted
# Returns: TRUE if preemption is imminent
gcloud compute instances create batch-worker \
--zone=europe-west2-a \
--machine-type=n2-standard-4 \
--provisioning-model=SPOT \
--instance-termination-action=STOP \
--image-family=debian-12 \
--image-project=debian-cloud \
--metadata=shutdown-script='#!/bin/bash
echo "Preemption detected — saving checkpoint..."
python3 /opt/worker/save_checkpoint.py
echo "Checkpoint saved."'
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.