Compute Services
Master Compute Engine VMs, Instance Groups, App Engine, Cloud Functions, and GKE for every application pattern.
Simple Explanation (ELI5)
GCP offers many ways to run code. Compute Engine lets you rent VMs and customize everything (like buying a laptop but in the cloud). Instance Groups let you manage multiple VMs and auto-scale. App Engine is simpler �" you just upload code and it handles servers automatically. Cloud Functions run single functions in response to events (serverless). GKE runs Kubernetes containers if you need orchestration. Pick the right tool based on control vs convenience.
Why Do We Need Different Compute Services?
- Compute Engine: Full control, custom OS, long-running services.
- Instance Groups: Auto-scaling, load balancing, fault tolerance.
- App Engine: Fastest path from code to production, no infrastructure management.
- Cloud Functions: Run code only when triggered, pay only for execution.
- GKE: Orchestrate containers with Kubernetes if you need advanced deployments.
Technical Explanation
1. Compute Engine (IaaS)
Virtual machines you fully control. Choose machine type, OS, storage, networking, firewall rules, and monitoring. You manage patches and security.
# Create a VM gcloud compute instances create my-vm \ --zone us-central1-a \ --machine-type n1-standard-1 \ --image-family debian-11 \ --image-project debian-cloud # SSH into the VM gcloud compute ssh my-vm --zone us-central1-a # Stop the VM (you still pay for disk) gcloud compute instances stop my-vm --zone us-central1-a # Delete the VM gcloud compute instances delete my-vm --zone us-central1-a
2. Machine Types
| Machine Type | vCPU | Memory | Use Case |
|---|---|---|---|
| n1-standard-1 | 1 | 3.75 GB | Light dev/test workloads |
| n1-standard-4 | 4 | 15 GB | Small production apps |
| n2-standard-8 | 8 | 32 GB | Medium production workloads |
| n2d-highcpu-32 | 32 | 128 GB | CPU-intensive, big data |
3. Instance Groups
Managed or unmanaged groups of VMs with auto-scaling and load balancing:
# Create a managed instance group template gcloud compute instance-templates create my-template \ --machine-type n1-standard-1 \ --image-family debian-11 # Create a managed instance group with auto-scaling gcloud compute instance-groups managed create my-ig \ --base-instance-name my-instance \ --template my-template \ --size 3 \ --zone us-central1-a # Set auto-scaling policy gcloud compute instance-groups managed set-autoscaling my-ig \ --max-num-replicas 10 \ --min-num-replicas 2 \ --zone us-central1-a
4. App Engine (PaaS)
Upload your app code, GCP handles infrastructure. Choose Standard (cheaper, simpler limits) or Flexible (more control).
# Deploy a Python app gcloud app deploy # View logs gcloud app logs read -n 50 # Set traffic to new version gcloud app services set-traffic default --splits v2=1
5. Cloud Functions (Serverless)
Run code triggered by events: HTTP requests, Cloud Storage uploads, Pub/Sub messages, etc. Pay strictly for execution time.
gcloud functions deploy my-function \ --runtime python39 \ --trigger-topic my-topic \ --allow-unauthenticated
6. GKE (Kubernetes)
Managed Kubernetes. GCP handles the control plane; you manage worker nodes and container deployments.
# Create a GKE cluster gcloud container clusters create my-cluster \ --zone us-central1-a \ --num-nodes 3 # Connect kubectl gcloud container clusters get-credentials my-cluster --zone us-central1-a # Deploy an app kubectl apply -f deployment.yaml
Compute Services Comparison
| Service | Control | Scalability | Cost | Best For |
|---|---|---|---|---|
| Compute Engine | Full | Manual or groups | Per VM/hour | Custom needs, long-running |
| Instance Groups | Moderate | Auto-scale | Per VM/hour | Predictable traffic, HA |
| App Engine | Low | Automatic | Per request + CPU/memory | Rapid deployment |
| Cloud Functions | Very low | Automatic | Per 100ms execution | Event-driven, low usage |
| GKE | High | Automatic (pods) | Per node/hour | Container orchestration |
Real-World Scenarios
Scenario 1: Web App That Scales
Use an Instance Group with Application Load Balancer. Define an autoscaling policy based on CPU or custom metrics. As traffic increases, new VMs spin up automatically.
Scenario 2: Event-driven Processing
Use Cloud Functions triggered by Cloud Storage. When a file is uploaded to a bucket, a function automatically processes it (resize images, extract text, etc.).
Scenario 3: Microservices on Kubernetes
Deploy containers on GKE. Use Helm or Kustomize to manage releases, Istio for service mesh, and Prometheus for metrics.
Interview Questions
Beginner
Compute Engine lets you rent VMs and manage everything. App Engine handles infrastructure; you just upload code. Compute Engine gives you control; App Engine gives you simplicity.
For development/testing, start with n1-standard-1 (1 vCPU, 3.75 GB). For light production, n1-standard-2. Monitor CPU/memory and right-size based on actual usage.
A group of identical VMs created from a template. GCP automatically replaces failed VMs and scales the group based on metrics (CPU, memory, custom). You define the template once; GCP manages the fleet.
When you have event-driven, short-lived workloads: HTTP webhooks, file processing, scheduled tasks. You pay only during execution, making it cheap for infrequent tasks.
GKE is Google's managed Kubernetes service. Use it if you have containerized applications needing orchestration (rolling updates, health checks, scaling, service discovery).
Intermediate
Standard is simpler and cheaper but has sandbox restrictions (no raw sockets, specific runtimes, 15-minute request timeout). Flexible lets you run custom Docker images, longer timeouts, and more control at higher cost.
GCP detects the failure and automatically deletes the broken VM. If the group size is below the desired count, it creates a replacement immediately using the template.
You set CPU, memory, custom metric thresholds (e.g., scale up if CPU > 70% for 3 minutes). GCP monitors VMs and adds/removes VMs to keep metrics within range. Always set both min and max replicas to prevent runaway costs.
If a function hasn't been invoked recently, the first request may take seconds (initialization of the runtime). Subsequent requests are fast. For latency-sensitive use cases, consider App Engine Flexible or Compute Engine.
Summary
GCP provides compute options from full control (Compute Engine) to maximum simplicity (App Engine, Cloud Functions). Choose based on your need for customization vs convenience. Instance Groups and GKE handle scaling automatically. Most production deployments use a mix: Cloud Functions for events, Instance Groups for stateless services, GKE for complex microservices.