Cloud Run: The Serverless Container Revolution
Google Cloud Associate Cloud Engineer Study Guide
Overview
Cloud Run is a fully managed compute platform that enables you to run stateless containers that are invocable via web requests or Pub/Sub events. It abstracts away all infrastructure management, allowing developers to focus solely on writing code. Built on Knative, it offers portability across different environments while providing the “serverless” benefits of scaling to zero and pay-per-use billing.
Core Concepts & Best Practices
1. Reliability & Scalability
Cloud Run scales automatically based on incoming requests. Unlike traditional VMs, you don’t manage Instance Groups. It handles “Cold Starts” by keeping a minimal footprint and can scale to thousands of instances in seconds.
2. Security
Containers are isolated by a sandbox environment (gVisor). Integration with Identity and Access Management (IAM) allows you to control exactly who can invoke your service and what GCP resources the service can access using Service Accounts.
3. Cost Optimization
You are billed to the nearest 100ms only when the container is processing a request. If there is no traffic, there is no cost (Scale-to-Zero), making it significantly cheaper than GKE or Compute Engine for variable workloads.
Service Comparison: Compute Options
| Feature | Cloud Run | App Engine (Standard) | GKE |
|---|---|---|---|
| Abstraction | Container-based | Language-based | Cluster-based |
| Scaling | Request-based (to 0) | Request-based (to 0) | Node/Pod-based |
| Portability | High (Knative/Docker) | Low (Proprietary) | Very High (K8s) |
| Configuration | Minimal | Minimal | Complex |
Scenario-Based Decision Matrix
| Requirement | Recommended Action |
|---|---|
| Need to run a Docker image with zero management. | Use Cloud Run. |
| Need to run a background job on a schedule. | Use Cloud Run Jobs. |
| Need GPU support or specialized networking. | Use GKE or Compute Engine. |
| Need to split traffic between two versions (A/B testing). | Use Cloud Run Revisions with traffic tagging. |
ACE Exam Tips: Golden Nuggets
- The Default Port: Cloud Run containers must listen for requests on the port defined by the
PORTenvironment variable (default is usually 8080). - Concurrency: Unlike Cloud Functions (1 request per instance), Cloud Run can handle multiple concurrent requests on a single container instance (up to 1000).
- Memory vs CPU: You can allocate up to 32GB of RAM and 8 vCPUs. If your app needs more, it might not be a fit for Cloud Run.
- Authentication: By default, Cloud Run services are private. Use the
--allow-unauthenticatedflag during deployment to make a service public.
Cloud Run Architecture Flow
Key GCP Services
- Artifact Registry: Stores your container images.
- Cloud Build: Automates the CI/CD pipeline to deploy.
- Secret Manager: Injects API keys securely.
Common Pitfalls
- Local Storage: Files written to the container disk are lost when it scales down. Use Cloud Storage instead.
- Long Timeouts: Max timeout is 60 mins, but usually, it should be seconds.
Architecture Patterns
Web API Microservices with Global Load Balancing.
Data Processing Pub/Sub messages asynchronously.
Scheduled Database cleanup using Cloud Scheduler.