By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Topic: Preemptible VMs, Spot VMs, Commitments, and Reducing Serving Costs
Cost optimization in GCP ML pipelines is about balancing performance, reliability, and expense—especially for compute-heavy workloads like training deep learning models, batch inference, or real-time serving. For example, a fintech company running real-time fraud detection on Vertex AI might use Spot VMs for training models (saving 80% on compute) but switch to reserved commitments for their always-on prediction endpoints to avoid cold starts. Poor cost choices can balloon cloud bills (e.g., leaving GPUs idle) or degrade user experience (e.g., preemptible VMs killing a long-running job).
Preemptible VMs (GCP): Short-lived (max 24h) Compute Engine VMs that can be terminated by GCP with 30-second notice. Best for: Fault-tolerant workloads like batch training, hyperparameter tuning, or data preprocessing. Cost: Up to 80% cheaper than regular VMs.
Spot VMs (GCP): Similar to preemptible VMs but with no 24-hour limit and a dynamic pricing model (billed per second). Best for: Longer-running jobs (e.g., distributed training) where interruptions are acceptable. Key difference: Spot VMs use a market-based pricing model (like AWS Spot Instances).
Committed Use Discounts (CUDs): 1- or 3-year commitments for Compute Engine or GPU resources in exchange for up to 70% discount. Best for: Predictable workloads (e.g., always-on Vertex AI endpoints, production inference). Trap: Must commit to specific machine types/regions.
Sustained Use Discounts (SUDs): Automatic discounts (up to 30%) for VMs running >25% of a month. Best for: Workloads with variable but consistent usage (e.g., nightly batch jobs). No commitment required.
Vertex AI Prediction Endpoints: Managed serving infrastructure for ML models. Cost drivers: Instance type (CPU/GPU), autoscaling settings, and request volume. Optimization: Use autoscaling to 0 for dev/test, batch predictions for offline jobs, and custom containers for cost-efficient runtimes.
Batch Predictions (Vertex AI): Offline inference for large datasets (e.g., processing millions of images overnight). Cost: Pay per prediction (no idle compute). Best for: Non-real-time use cases (e.g., monthly customer churn analysis).
Custom Containers (Vertex AI): Bring your own Docker image for prediction endpoints. Optimization: Use lightweight runtimes (e.g., FastAPI + ONNX) to reduce memory/CPU needs. Trap: Over-provisioning resources (e.g., requesting 8 vCPUs when 2 suffice).
Cloud TPUs: Google’s custom hardware for training deep learning models (e.g., transformers, CNNs). Cost: Pay per TPU-hour (cheaper than GPUs for large-scale training). Best for: Models with heavy matrix operations (e.g., LLMs, image segmentation).
Cloud GPUs (NVIDIA A100/T4): Accelerators for training/inference. Cost: Expensive (e.g., A100 ~$2.93/hour). Optimization: Use preemptible GPUs for training, reserved commitments for production.
BigQuery ML: Train ML models directly in BigQuery (no separate VMs needed). Cost: Pay per query + model storage. Best for: Simple models (linear regression, k-means) on structured data.
Dataflow (Apache Beam): Serverless batch/streaming data processing. Cost: Pay per vCPU/memory/hour. Optimization: Use FlexRS for batch jobs (cheaper than streaming) and autoscaling.
Cloud Functions / Cloud Run: Serverless options for lightweight inference (e.g., REST APIs). Cost: Pay per request + compute time. Best for: Low-latency, sporadic traffic (e.g., chatbot responses).
Scenario: You’re training a PyTorch model on 100GB of image data. The job takes ~6 hours but can restart if interrupted.Steps:1. Estimate job duration: Use gcloud compute instances list to check historical runtimes.2. Select a preemptible/Spot VM: - For short jobs (<24h): gcloud compute instances create my-trainer --preemptible --machine-type=n2-standard-8 --accelerator=type=nvidia-tesla-t4,count=1 - For longer jobs: gcloud compute instances create my-trainer --provisioning-model=SPOT --machine-type=n2-standard-8 3. Enable checkpointing: Save model weights every 30 minutes to Cloud Storage (gs://my-bucket/checkpoints/).4. Use a managed service (if possible): - For Vertex AI Training, set training_job.preemptible=True in the Python SDK.5. Monitor costs: Use Cloud Billing reports to compare preemptible vs. on-demand spend.
gcloud compute instances list
gcloud compute instances create my-trainer --preemptible --machine-type=n2-standard-8 --accelerator=type=nvidia-tesla-t4,count=1
gcloud compute instances create my-trainer --provisioning-model=SPOT --machine-type=n2-standard-8
gs://my-bucket/checkpoints/
training_job.preemptible=True
Scenario: Your production endpoint serves 100K predictions/day with 90% traffic during business hours.Steps:1. Right-size the instance: - Test with n1-standard-2 (2 vCPUs) vs. n1-standard-4 using Vertex AI’s online prediction latency metrics. - Use custom containers to optimize runtime (e.g., FastAPI + ONNX instead of TensorFlow Serving).2. Autoscale efficiently: - Set min_replica_count=1 (to avoid cold starts) and max_replica_count=10. - Use CPU utilization (not request count) as the scaling metric (e.g., scale at 60% CPU).3. Use batch predictions for offline jobs: - For monthly reports, run gcloud ai batch-prediction-jobs create instead of hitting the endpoint.4. Commit to CUDs for production: - Purchase a 1-year commitment for the n1-standard-4 machine type in your region.5. Monitor and adjust: - Use Cloud Monitoring to track aiplatform.googleapis.com/prediction/latency and cost_per_prediction.
n1-standard-2
n1-standard-4
min_replica_count=1
max_replica_count=10
gcloud ai batch-prediction-jobs create
aiplatform.googleapis.com/prediction/latency
cost_per_prediction
Scenario: You’re preprocessing 1TB of text data for an NLP model using Dataflow.Steps:1. Choose the right runner: - For batch jobs: Use Dataflow Prime (cheaper than streaming) with runner=DataflowRunner. - For streaming: Use FlexRS (autoscaling) with flexRSGoal=COST_OPTIMIZED.2. Optimize worker settings: - Set worker_machine_type=n1-standard-4 (balance cost/performance). - Use num_workers=10 and autoscalingAlgorithm=THROUGHPUT_BASED.3. Leverage Spot VMs for workers: - Add --worker_harness_container_image=gcr.io/cloud-dataflow/v1beta3/spot-worker-harness to use Spot VMs.4. Cache intermediate data: - Store processed data in BigQuery or Cloud Storage to avoid reprocessing.5. Monitor and kill idle jobs: - Set a Cloud Scheduler to cancel jobs running >24h.
runner=DataflowRunner
flexRSGoal=COST_OPTIMIZED
worker_machine_type=n1-standard-4
num_workers=10
autoscalingAlgorithm=THROUGHPUT_BASED
--worker_harness_container_image=gcr.io/cloud-dataflow/v1beta3/spot-worker-harness
A startup is training a PyTorch model on 500GB of image data. The job takes ~8 hours but can restart if interrupted. Which GCP service offers the lowest cost with minimal management overhead? Answer: Vertex AI Training with preemptible VMs.Why: Preemptible VMs are 80% cheaper than on-demand, and Vertex AI Training handles job orchestration.
A production Vertex AI endpoint serves 50K predictions/day with 90% traffic between 9 AM–5 PM. How can you reduce costs without affecting latency? Answer: Use autoscaling with min_replica_count=1 and max_replica_count=10, and purchase a 1-year CUD for the machine type.Why: Autoscaling reduces idle costs, and CUDs provide a 70% discount for predictable workloads.
A data team runs a weekly Dataflow job to preprocess 1TB of text data. The job takes ~6 hours. How can they reduce costs? Answer: Use Dataflow Prime with FlexRS and Spot VMs for workers.Why: FlexRS optimizes for cost, and Spot VMs reduce worker costs by up to 80%.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.