Cloud Run in Google Cloud: Serverless Containers from First Principles to Production
A deep, plain-English guide to how Cloud Run really works: its architecture, scaling brain, networking, security, cost model, and the patterns that keep production services calm.
Imagine a restaurant that has no fixed kitchen staff. When a customer walks in, a chef appears, cooks the meal, and the moment the customer leaves, that chef can go home. If one hundred customers arrive at once, one hundred chefs appear. If nobody comes at three in the morning, nobody is paid. That is the promise of Cloud Run: you hand Google a packaged application, and Google supplies the “chefs” (computers) exactly when requests show up, and takes them away when they do not.
1What Cloud Run Is and Why It Exists
Cloud Run is Google Cloud’s fully managed platform for running containers without managing servers.
A container is a sealed box that holds your program together with everything it needs to run: the language runtime, libraries, and settings. Because the box carries its own supplies, it behaves the same on a laptop, in a test environment, and in production. Cloud Run takes that box, runs it on Google’s infrastructure, gives it a web address, and scales the number of running copies up and down automatically.
Before platforms like this, a team that wanted to run a web service had to rent virtual machines, install an operating system, patch it, set up a load balancer, configure autoscaling rules, and watch for failures. Each of those jobs is real work that has nothing to do with the product being built. Cloud Run exists to remove that work. You supply the container, and the platform handles servers, operating system patching, capacity planning, load balancing, certificates, and scaling.
Think of ride-hailing versus owning a car. Owning a car means paying for insurance, parking, and repairs whether you drive or not. Ride-hailing means you pay only for the trips you take. Cloud Run is ride-hailing for your code: you pay while a request is being served, not for a machine sitting idle.
Why advanced engineers care
Cloud Run is not a toy for small websites. It runs request-driven APIs, background batch work, event consumers, AI inference with GPUs, and internal tools at large scale. Its design choice is simple but powerful: it accepts standard containers and standard web traffic, so there is no proprietary framework to learn and moving away is relatively painless. That portability, combined with scale-to-zero economics, is why it has become a default landing zone for new workloads on Google Cloud.
The three families of Cloud Run workloads
Cloud Run offers distinct resource types for distinct jobs. Services respond to incoming requests such as web calls and webhooks. Jobs run a task to completion and then stop, which suits data processing and scheduled work. Worker pools, a newer addition, keep long-running background consumers alive without an HTTP front door. Understanding which one fits your workload is the first design decision you make, and it prevents many expensive mistakes later.
Cloud Run is built on the open Knative serving model, which is one reason a Cloud Run service feels familiar to people who already know Kubernetes concepts, even though you never touch a cluster.
2Core Concepts You Must Know Cold
Every Cloud Run conversation uses the same small vocabulary, so learn each word properly once.
New terms are the biggest barrier when learning any platform. Below, every concept is defined by what it is, why it exists, and where you meet it. Read the cards slowly, because later chapters build on them without re-explaining.
Service
A named, long-lived deployment that answers requests. It owns a stable URL and always routes traffic to one or more revisions.
Revision
An immutable snapshot of your container image plus its settings. Every deployment or configuration change creates a new revision, and old ones remain for rollback.
Instance
One running copy of a revision. Cloud Run adds and removes instances automatically as demand changes.
Container
The packaged application inside an instance. A revision can run one main container plus helper containers called sidecars.
Concurrency
How many requests a single instance may handle at the same moment. This setting strongly shapes cost and performance.
Job and Task
A job is a run-to-completion workload. Each execution of a job splits into tasks, which are independent units that can run in parallel.
Service Identity
The service account a revision runs as. It decides what the code is allowed to call inside Google Cloud.
Region
The Google Cloud location where a service runs. Services are regional resources, which matters for latency and resilience planning.
A service is like a restaurant brand. A revision is one version of the menu and recipes printed on a specific date. An instance is one chef actually cooking from that menu right now. Customers only see the brand’s front door; behind it, the menu version and the number of chefs can change freely.
Why immutability matters
Revisions can never be edited after creation. If you want a different memory size or a new environment variable, you create a new revision. This sounds restrictive, but it gives you something valuable: a permanent, auditable record of exactly what ran at any time, and a guaranteed way to roll back by pointing traffic at an older revision that is known to work.
Beginners often think an “instance” is a virtual machine they can log into. It is not. You cannot connect to an instance, and it may disappear at any moment. Treat instances as disposable and anonymous.
3Architecture and Components
Cloud Run feels simple on the surface because a lot of machinery is hidden beneath it.
At a high level, Cloud Run has a control plane and a data plane. The control plane is the part that accepts your deployment commands, stores your configuration, creates revisions, and decides how many instances should exist. The data plane is the part that actually receives user requests and runs your containers. Separating these two means that a busy moment in request traffic does not slow down deployments, and an ongoing deployment does not disturb live traffic.
flowchart TD
U["User or Client"] --> GFE["Google Front End and Load Balancing"]
GFE --> RT["Cloud Run Request Router"]
RT --> I1["Instance A"]
RT --> I2["Instance B"]
RT --> I3["Instance C"]
CP["Control Plane: Revisions, Autoscaler, Config"] -.-> RT
CP -.-> I1
CP -.-> I2
CP -.-> I3
I1 --> SVC["Other Google Cloud Services"]
I2 --> SVC
I3 --> SVC
Requests first reach Google’s global front end infrastructure, which terminates encrypted connections and applies protections. From there a router picks a ready instance of the correct revision. If none has spare capacity, the autoscaler is told to start another. The autoscaler is therefore part of the control plane, but its decisions are driven by signals coming from the data plane.
Execution environments
Each instance runs inside a sandbox so that one customer’s code cannot affect another’s. Cloud Run offers two execution environments. The first generation uses a lightweight sandbox that starts very quickly and is cost-efficient. The second generation provides a fuller Linux environment with broader system call compatibility, better network throughput in many cases, and support for features such as network file system mounts. If your application needs behavior that the lighter sandbox does not fully emulate, the second generation is the safer choice. If you want the fastest cold starts and your application is simple, the first generation can be attractive. Newer features increasingly assume the second generation, so check your requirements before choosing.
Supporting components
Artifact Registry
Stores the container images Cloud Run pulls when creating instances. Keeping images close to the region reduces startup delay.
Cloud Build
Turns source code into container images, using Dockerfiles or automatic buildpacks that detect your language.
IAM
Controls who can deploy, who can invoke, and what the running code can access.
Cloud Logging and Monitoring
Collects everything your container writes to standard output and publishes built-in service metrics automatically.
The control plane is the restaurant manager who decides how many chefs to call in and which menu is active. The data plane is the kitchen floor where food is actually made. Customers never talk to the manager, yet the manager’s decisions shape every meal.
4Request Lifecycle and Data Flow
Following a single request from the browser to your code reveals most of Cloud Run’s behavior.
When someone calls your service URL, the request travels through several hands before your code sees it. Understanding each hand helps you diagnose delays, because a slow response might be slow at any of these stages and the fix differs for each.
sequenceDiagram
participant C as Client
participant G as Google Front End
participant R as Router
participant A as Autoscaler
participant I as Instance
C->>G: HTTPS request
G->>R: Authenticated, forwarded request
R->>R: Check IAM invoker permission
alt Instance has free capacity
R->>I: Deliver request
else No free capacity
R->>A: Signal need for new instance
A->>I: Start container
I-->>R: Startup check passed
R->>I: Deliver queued request
end
I-->>C: Response
Connection and encryption
The client connects to the service URL. Google’s front end ends the encrypted connection and presents a managed certificate, so you never handle certificates yourself.
Authorization check
If the service requires authentication, the platform verifies the caller’s identity token and checks the invoker permission before your code is ever touched. Unauthorized callers are rejected at the edge.
Routing and instance selection
The router picks an instance with free concurrency slots. If all are busy, the request waits briefly while a new instance starts.
Your code runs
The container receives a normal HTTP request on its listening port. Your application does its work, perhaps calling a database or another service.
Response and billing close
The response travels back through the same path. For request-based billing, the clock for that request stops when the response is complete.
The instance lifecycle
An instance moves through stages: it is created, it starts and passes its startup check, it serves requests, it becomes idle when no requests are active, and eventually it is shut down. Before shutdown, Cloud Run sends a termination signal so your application can finish work and release resources. Treat that signal seriously, because applications that ignore it lose in-flight work or leave connections hanging.
When latency looks bad, first check whether the request hit a cold start. A single slow request followed by many fast ones is the classic fingerprint of a new instance starting up.
5Autoscaling, Concurrency and Cold Starts
Scaling is the heart of Cloud Run, and three settings decide almost everything.
Cloud Run scales the number of instances based mainly on incoming request pressure and CPU utilization. Unlike older platforms where you wrote scaling rules by hand, here the platform watches how many requests are in flight, compares that to each instance’s concurrency limit, and decides how many instances are needed. Your job is to set the boundaries and make your container healthy under them.
Concurrency: the most misunderstood setting
Concurrency is the number of simultaneous requests one instance may handle. A high value lets one instance serve many callers, which lowers cost and reduces how often new instances must start. A low value, even one, makes each request get a dedicated instance, which isolates requests from each other but multiplies instance count and cold starts. The right number depends on how your application behaves. Programs that mostly wait on databases and other services handle high concurrency well. Programs that burn CPU on each request, or that keep large per-request state in memory, need lower concurrency or they will slow down and run out of memory.
A waiter can serve several tables at once because much of the time is spent waiting for the kitchen. But a waiter carrying heavy trays for every table cannot take on more tables without dropping something. Concurrency is how many tables you allow each waiter to cover.
Minimum and maximum instances
Minimum instances keep a set number of copies warm even when traffic is zero, which removes cold starts for the first requests at the price of paying for those idle copies at a reduced rate. Maximum instances cap how far the service can scale, which protects downstream systems like databases from being overwhelmed and protects your budget from runaway traffic or attacks. A sensible production service sets both on purpose, rather than accepting defaults.
Understanding cold starts
A cold start is the delay when a request arrives and no ready instance exists. The platform must fetch the image, start the sandbox, launch your process, and wait for your application to report that it is ready. The time spent inside your own application, such as loading frameworks and warming connections, is usually the largest and most controllable part.
Smaller images
Lean images with fewer layers pull and start faster. Remove build tools and unused files from the final image.
Startup CPU boost
Temporarily gives extra CPU during startup, which speeds up heavy initialization at small cost.
Lazy initialization
Delay expensive setup until a feature is actually used, so the container reports ready sooner.
Minimum instances
Keep warm copies for latency-sensitive paths so users rarely meet a cold instance.
Setting a very high maximum instance count with a database that allows only a few hundred connections is a recipe for outages. Scale limits and connection limits must be planned together.
6CPU Allocation, Resources and the Cost Model
Cloud Run bills for what you use, but “what you use” depends on one key choice.
Every instance is configured with an amount of CPU and memory. You pick these per revision, from small fractions of a CPU up to several full CPUs, with generous memory ceilings for heavier workloads. Choosing the right size is a balancing act: too small and requests time out or the container is killed for exceeding memory, too large and you pay for capacity you never touch.
Two billing modes
The first mode is request-based billing. Here the CPU is available only while the instance is handling at least one request, and you are billed for the time requests are active, rounded up in small increments, plus a small charge per request count. Outside request handling, the CPU is throttled and effectively paused. This is ideal for typical web APIs because idle time costs nothing.
The second mode is instance-based billing, sometimes called always-on CPU. The CPU is fully available for the entire life of the instance, even between requests, and you are billed for the whole time the instance exists, without a per-request fee. This suits workloads that do background work after responding, keep websocket-style long connections busy, or run in-memory caches that need steady processing.
| Aspect | Request-based | Instance-based |
|---|---|---|
| CPU outside requests | Throttled | Fully available |
| You pay for | Active request time and request count | Entire instance lifetime |
| Best for | Spiky APIs, low or unpredictable traffic | Steady traffic, background threads, long-lived connections |
| Idle cost | Near zero | Continuous while instances exist |
| Risk | Background work after response may stall | Higher baseline bill if traffic is low |
Request-based billing is paying a taxi per ride. Instance-based billing is renting a car by the day. If you travel rarely, the taxi wins. If you are on the road constantly, the rental becomes cheaper per trip.
Hidden cost drivers
Compute is rarely the only line on the invoice. Network egress to the internet or across regions, calls to paid downstream services, logging volume, and build minutes all add up. A chatty service that logs every request body can spend more on logging than on compute. Cost discipline means looking at the entire bill, not only the Cloud Run line.
Committed use and free allowances
Google Cloud provides a monthly free allowance for Cloud Run usage and offers discounts for steady, predictable usage through committed use arrangements. Both are useful, but they should follow a measured baseline of real usage rather than guesses. Always confirm current pricing and allowances on the official pricing page, since amounts change over time.
Raising concurrency is often the cheapest optimization available. If one instance can safely serve more requests at once, you need fewer instances for the same traffic.
7The Container Contract, Probes and Graceful Shutdown
Cloud Run asks your container to follow a few simple rules, and breaking them causes confusing failures.
The contract is short. Your container must listen for HTTP requests on the port the platform provides through an environment variable, and it must start listening within the startup window. It should be stateless, meaning it does not rely on local files or memory surviving between requests, because the next request may land on a different instance. It should write logs to standard output and standard error, and it should handle the termination signal calmly.
Startup, liveness and readiness thinking
Cloud Run supports startup probes and liveness probes. A startup probe tells the platform when your application has finished initializing and can start receiving traffic. Without a well-designed startup check, the platform may consider the container ready as soon as the port opens, even though your application is still loading data. A liveness probe periodically asks whether a running instance is still healthy; if it keeps failing, the instance is restarted. These probes turn vague “it sometimes hangs” problems into predictable, self-healing behavior.
Startup probe
Answers “are you finished starting?” Traffic is held back until it passes.
Liveness probe
Answers “are you still working?” Repeated failure causes the instance to be restarted.
Termination signal
Sent before shutdown, giving a short grace period to finish work and close connections.
Request timeout
The maximum time a request may run. The default is a few minutes and it can be raised to as long as an hour.
Graceful shutdown in practice
Instances are shut down during scale-in, during new deployments, and during maintenance. The platform gives a short grace period after the termination signal. A well-behaved application stops accepting new work, finishes in-flight requests, flushes buffered logs and metrics, and closes database connections. An application that ignores the signal may be forcefully stopped, which shows up as dropped requests during every deployment.
A shop closing for the day should stop letting new customers in, finish serving the people at the counter, then lock up. Walking out mid-sale leaves customers confused and cash drawers unbalanced. Graceful shutdown is the polite version of closing time.
Starting background threads after sending a response, in request-based billing mode, is risky. Once the response is sent, the CPU may be throttled and that work can freeze or be lost. Finish the work before responding, or use an instance-based setup, or hand the work to a queue.
8Revisions, Traffic Splitting and Safe Deployments
Because revisions are immutable, Cloud Run gives you powerful, low-risk release tools for free.
Every deployment creates a new revision. By default, the newest revision receives all traffic, but you can change that. Cloud Run lets you split traffic by percentage between revisions, give a revision a named tag with its own test URL, and roll back instantly by moving traffic to an older revision. No servers are rebuilt and no load balancers are reconfigured; the router simply changes its weights.
flowchart LR
D["Deploy new revision with no traffic"] --> T["Test via tagged URL"]
T --> C1["Send 5 percent of traffic"]
C1 --> M{"Metrics healthy?"}
M -- Yes --> C2["Raise to 50 percent"]
C2 --> F["Send 100 percent"]
M -- No --> RB["Roll back to previous revision"]
Release strategies compared
| Strategy | How it works on Cloud Run | Best used when |
|---|---|---|
| All at once | New revision takes 100 percent of traffic immediately | Low-risk changes and internal tools |
| Canary | Small percentage goes to the new revision while metrics are watched | User-facing services where regressions are costly |
| Blue-green | Deploy with no traffic, test by tag, then switch everything in one step | You need a clean, instantly reversible cutover |
| Rollback | Point traffic back to a known good revision | An incident is in progress and speed matters |
Why a tagged URL is a hidden gem
A tag gives a revision a separate address that receives only deliberate test traffic. Teams use it to run smoke tests in a production-like environment before any customer sees the change. This closes the gap between a staging environment and reality, because the revision uses the real configuration, identity, and network paths.
Database changes and compatibility
During a rollout, two revisions run at the same time. That means every database change must be compatible with both the old and new code. The safe pattern is to add new structures first, deploy code that can use both, migrate, and only then remove the old structures. Ignoring this is a common reason canary releases fail even when the code is correct.
Always deploy by an immutable image identifier rather than a moving label like “latest”. Then the revision records exactly which build is running, and a rollback truly returns to the same bits.
9Networking: Ingress, Egress and Load Balancing
Networking decides who can reach your service and what your service can reach.
Cloud Run networking has two directions. Ingress is traffic coming in. Egress is traffic going out. Each direction has its own controls, and a secure design configures both deliberately.
Ingress controls
A service can accept traffic from anywhere on the internet, only from internal sources such as your private networks and other Google Cloud services, or only from internal sources plus a Google Cloud load balancer. The last option is the common choice for public production systems that need extra protection, because it forces every external request to pass through the load balancer where you can attach security policies and caching.
Egress and private networks
By default a service reaches the internet and public Google APIs directly. To talk to private resources, such as a database that has no public address, the service needs a path into a Virtual Private Cloud network. Two mechanisms exist. Direct VPC egress places the instance’s network interface directly into your subnet, which is simpler and generally faster. A Serverless VPC Access connector is an older approach that routes traffic through a small fleet of connector machines. Most new designs prefer the direct option. You can also choose whether all outbound traffic goes through the private network or only traffic destined for private addresses, which affects cost and how you control outbound internet access.
flowchart LR
Internet["Internet Users"] --> LB["Global External Load Balancer"]
LB --> ARM["Cloud Armor Policy"]
ARM --> NEG["Serverless NEG"]
NEG --> CR["Cloud Run Service"]
CR --> VPC["VPC Subnet via Direct Egress"]
VPC --> DB["Private Database"]
VPC --> NAT["Cloud NAT"]
NAT --> EXT["Approved External APIs"]
Why put a load balancer in front
Web application firewall
Cloud Armor filters malicious traffic and enforces rate limits before requests reach your service.
Caching and CDN
Static or cacheable responses are served from Google’s edge, cutting latency and instance load.
Multi-region routing
One global address can send users to the nearest healthy region.
Custom domains and certificates
Serve your own domain names with managed certificates and flexible URL routing.
Identity-aware access
Protect internal web apps by requiring users to sign in with approved corporate identities.
Ingress settings are the guest list at the front door. Egress settings are the rules for where your staff may go when they leave the building. A load balancer is a security desk in the lobby that checks bags and directs visitors to the right floor.
Restricting ingress to the load balancer but leaving the default service URL reachable is a frequent gap. Make sure ingress is set so that direct calls to the default address are not an accidental side door.
10Security: Identity, Secrets and Supply Chain
Cloud Run security is about three questions: who may call you, who are you, and what do you trust.
Who may call you
Access to invoke a service is controlled by IAM through the invoker role. A service can be public, meaning anyone may call it, or private, meaning callers must present a valid identity token from an account that holds the invoker permission. Private by default is the healthier posture. Make a service public only when it truly serves anonymous users, and even then consider placing a load balancer and a firewall in front.
Who are you: the service identity
Every revision runs as a service account. This identity is what your code uses when it calls other Google Cloud services. The most common security failure is using the broad default account, which often carries wide permissions. The correct approach is to create a dedicated service account per service and grant it only the specific permissions it needs. If the service is compromised, the damage is limited to those few permissions.
A building can give every employee a master key, or it can give each person a keycard that opens only the doors they need. A dedicated service account is the second approach. If one card is stolen, only a few doors are at risk.
Service-to-service calls
When one Cloud Run service calls another private one, the caller obtains an identity token for the target and sends it with the request. The platform checks the token and the invoker permission. This means internal microservices can be locked down without managing shared secrets or network allow lists.
Secrets and configuration
Passwords, API keys, and certificates belong in Secret Manager, not in the container image or plain environment variable files. Cloud Run can expose a secret to the container as an environment value or as a mounted file, and it can pin to a specific secret version or follow the latest. Pinning gives repeatability; following latest gives easier rotation. Access to each secret is granted to the service account individually.
Protecting the software supply chain
Binary Authorization
Allows only images that were built and signed by trusted pipelines to be deployed.
Vulnerability scanning
Artifact Registry can scan stored images for known flaws so risky builds are caught early.
Minimal base images
Smaller images contain fewer packages and therefore fewer things to attack or patch.
Customer-managed encryption keys
For regulated workloads, you can control the keys protecting stored images and secrets.
Organization policies
Central guardrails can forbid public services, force ingress limits, or restrict regions across all projects.
Cloud Run’s sandbox isolates instances from each other and from the host, and Google patches the underlying operating system for you. Your responsibility starts at your container contents and your IAM design.
11Data, State and Storage
Cloud Run instances forget everything, so durable data must live elsewhere.
Because instances appear and vanish, anything written to the instance’s local disk is temporary. In fact, local disk space is an in-memory file system, so writing large temporary files consumes the memory you allocated. The design rule is simple: keep the container stateless, and place state in a service built for it.
Choosing a data home
| Need | Typical choice | Why it fits |
|---|---|---|
| Relational data with transactions | Cloud SQL or AlloyDB | Familiar engines, strong consistency, managed backups |
| Global scale relational data | Spanner | Horizontal scale with strong consistency across regions |
| Flexible documents | Firestore | Serverless scaling that matches Cloud Run’s behavior |
| Large files and media | Cloud Storage | Cheap, durable object storage with direct access options |
| Fast shared cache | Memorystore | In-memory speed for sessions and repeated lookups |
| Analytics | BigQuery | Serverless warehouse for large queries |
The connection-count problem
A subtle trap appears when many instances each open several database connections. Fifty instances with ten connections each can exhaust a modest database. Solve this by capping maximum instances, keeping per-instance connection pools small, reusing connections across requests, and using a connection pooler or the managed connection features of the database service where available. Cloud Run can connect to Cloud SQL through a built-in secure path, so you do not need to open the database to the public internet.
Mounting storage as volumes
Cloud Run can mount Cloud Storage buckets as folders and can attach network file shares so that code expecting a normal file path keeps working. It can also provide a memory-backed volume for scratch space shared between containers in the same instance. These mounts are convenient for machine learning model files, shared assets, and legacy applications. They are not a replacement for a database, because object storage has different latency and consistency traits than a local disk.
A pop-up food stall should not store its accounting ledger on the stall’s counter. When the stall packs up, the ledger would vanish. The ledger belongs in a safe office. In the same way, the container is the pop-up stall and the database is the safe office.
Keeping user sessions in instance memory breaks as soon as a second instance appears, because the next request may reach a different instance. Store sessions in a shared cache or database, or use signed tokens the client carries.
12Event-Driven and Asynchronous Designs
Cloud Run is not only for web pages; it is an excellent reaction engine for events.
An event is a statement that something happened: a file arrived, a message was published, a record changed. Event-driven designs let services react without being tightly coupled, which makes systems easier to extend and more resilient to partial failures. Cloud Run fits naturally because it can wake from zero when an event arrives, do its job, and sleep again.
flowchart LR
S["Storage Upload"] --> E["Eventarc"]
P["Pub/Sub Topic"] --> R1["Cloud Run Service"]
E --> R1
SCH["Cloud Scheduler"] --> J["Cloud Run Job"]
Q["Cloud Tasks Queue"] --> R2["Cloud Run Worker Service"]
R1 --> Q
R1 --> BQ["BigQuery"]
R2 --> DB["Database"]
The main delivery tools
Pub/Sub push
Delivers messages as web calls to your service, with retries if your service reports failure.
Eventarc
Routes events from many Google Cloud sources and audit logs to your service in a standard format.
Cloud Tasks
Gives controlled, rate-limited, retryable delivery of individual tasks to a target service.
Cloud Scheduler
Triggers services or jobs at set times, like a managed cron.
Designing for at-least-once delivery
Most messaging systems promise that a message will be delivered at least once, which means duplicates can happen. Your handler must be idempotent, meaning that processing the same message twice has the same final effect as processing it once. Common techniques include storing a unique message identifier and skipping repeats, or writing records using operations that naturally overwrite rather than add.
Acknowledgement and timeouts
For push-style delivery, the HTTP response is the acknowledgement. A success response says the message is handled; an error response asks for a retry. If your processing takes longer than the sender’s acknowledgement deadline, the message may be redelivered while you are still working on it. Keep handlers short, or accept the message quickly and hand long work to a job or a queue.
A postal sorting office sometimes delivers a letter twice by mistake. A careful recipient checks the letter’s reference number and ignores the second copy. Idempotency is that reference number check.
Add a dead-letter destination for messages that keep failing. This isolates poison messages so that one bad record does not block or endlessly retry while good traffic waits.
13Cloud Run Jobs and Worker Pools
Not all work answers a request; some work simply has to be finished.
A Cloud Run job runs a container until it completes, then stops. There is no web address and no request handling. Jobs suit tasks like nightly report generation, data migration, image batch processing, and machine learning preparation. Each execution of a job can be split into many tasks that run at the same time, and each task learns its own index so it can pick its slice of the work.
How a job execution behaves
Execution starts
A person, a schedule, or a workflow starts the job. Cloud Run creates the requested number of tasks.
Tasks run in parallel
Up to the parallelism limit you set, tasks run together. Each receives its task index and the total task count through environment values.
Failures retry
A task that exits with failure can be retried up to a configured number of times. Tasks must therefore be safe to run again.
Execution completes
When every task succeeds or retries are exhausted, the execution is marked finished and compute charges stop.
Designing parallel tasks well
The key skill is partitioning. If you must process one million files, you can divide them so that each of one hundred tasks handles ten thousand, selected by task index. This is far faster than a single long process and costs about the same, because you pay for total compute time rather than elapsed time. Plan for uneven slices, because tasks that finish early cannot help tasks that are still running unless you design a shared work queue.
Worker pools for continuous background consumers
Some workloads need to run all the time without receiving web requests, for example a consumer that pulls messages from a queue or a stream. Worker pools are designed for this: they keep a configured amount of capacity running and do not require your code to expose an HTTP endpoint. They are an alternative to forcing a background consumer into a service that must pretend to answer web calls.
| Resource | Triggered by | Lifetime | Typical use |
|---|---|---|---|
| Service | Incoming requests or events | Always available, scales to demand | APIs, webhooks, websites |
| Job | Manual start, schedule, or workflow | Runs to completion then stops | Batch processing, migrations, reports |
| Worker pool | Pulls its own work continuously | Runs continuously | Queue and stream consumers |
Using a service with a very long request timeout to run batch work is fragile. If the connection drops or the instance is replaced, the work is lost. Jobs are built for this purpose and handle retries and parallelism properly.
14Observability: Logging, Metrics and Tracing
You cannot log into an instance, so what your service reports about itself is everything you have.
Observability is the ability to understand what a system is doing from the outside. On Cloud Run, three signals matter: logs, metrics, and traces. Together they answer what happened, how much and how fast, and where time was spent.
Logs
Anything your container prints to standard output or standard error is collected automatically into Cloud Logging, along with request logs written by the platform. The best practice is structured logging, where each log entry is a small structured record with fields such as severity, message, and a request identifier, rather than a free-form sentence. Structured logs can be filtered and counted precisely, and the platform can link them to the request that produced them and to the trace.
Metrics
Cloud Run publishes built-in metrics without any setup. The important ones describe the shape of your service’s health.
Request count and status
Shows traffic volume and the share of client errors and server errors.
Request latency
Shows how long requests take, best read as percentiles rather than averages.
Instance count
Shows how many instances are active or idle, revealing scaling behavior and cost.
CPU and memory utilization
Shows whether instances are starved or oversized, guiding right-sizing and concurrency.
Startup latency
Shows how long new instances take to become ready, exposing cold start problems.
Traces
A trace follows one request across several services, with each step timed. Cloud Run forwards tracing headers, so if your code propagates them to downstream calls, Cloud Trace can draw the full picture. Traces are the fastest way to discover that a slow API is actually waiting on one slow database query three services away.
Service level objectives
Instead of staring at raw graphs, define a promise: for example, that ninety-nine percent of requests succeed over a rolling month, or that ninety-five percent finish within a chosen time. Cloud Monitoring can track these objectives and alert when the error budget is being spent too fast. This turns vague unease into clear, agreed thresholds.
A hospital monitors a patient with several instruments: heart rate, temperature, and blood pressure. No single number tells the whole story, but together they show trouble early. Logs, metrics, and traces are the three instruments for your service.
Alert on symptoms users feel, such as error rate and latency, rather than only on causes such as high CPU. A busy instance that still answers quickly is not an emergency.
15Design Patterns and Anti-Patterns
Experienced teams repeat a handful of patterns, and avoid a handful of traps.
Patterns that work well
Backend for frontend
A small service shapes data specifically for one client, such as a mobile app, calling several internal services and returning one tidy response. Cloud Run’s fast scaling suits these thin aggregation layers.
Sidecar for cross-cutting needs
Run a helper container beside your main container to handle logging agents, proxies, or telemetry collection. Both containers share the instance and its network, so the main application stays focused on business logic.
Queue-based load leveling
Accept requests quickly, place the heavy work on a queue, and let a worker service process it at a controlled rate. This protects downstream systems from traffic spikes and keeps user-facing latency low.
Strangler migration
Move a legacy application gradually by placing a router in front and sending individual routes to new Cloud Run services one at a time, until the old system can be retired.
Fan-out with jobs
A coordinator splits a large dataset into slices and starts a parallel job, with each task handling one slice. Results are written to shared storage and combined at the end.
Anti-patterns to avoid
Problem
The service stores user sessions, uploaded files, or counters in instance memory or local disk.
Why It’s Harmful
Instances are replaced and multiplied at will. Data disappears at random, and users experience being logged out or losing uploads depending on which instance answers.
Correct Approach
Keep the container stateless. Use a shared database, cache, or object storage for anything that must persist or be shared.
Problem
One giant service holds every feature of a large product, deployed as a single unit.
Why It’s Harmful
Every change risks everything. Scaling, memory, and permissions must satisfy the most demanding feature, so the whole service is oversized and over-privileged.
Correct Approach
Split along real boundaries, giving each service its own identity, scaling limits, and release cadence. Avoid splitting so finely that calls between services dominate latency.
Problem
Unbounded scaling with no maximum instance limit, calling a database or a paid external service.
Why It’s Harmful
A traffic spike or a retry storm can scale the service enormously, exhausting database connections, hitting external rate limits, or producing a surprising bill.
Correct Approach
Set a maximum instance count based on downstream capacity, add rate limiting at the edge, and use queues to smooth bursts.
Problem
Services call each other synchronously in long chains, such as A calls B calls C calls D, with no timeouts.
Why It’s Harmful
Latency adds up and failures multiply. One slow link makes the whole chain slow, and retries at each level can create a storm.
Correct Approach
Shorten chains, set explicit timeouts, use circuit breakers, and move non-urgent steps to asynchronous events.
16Cloud Run Compared with Other Google Cloud Compute Options
Choosing a compute platform is about matching control, effort, and workload shape.
Google Cloud offers several ways to run code. No option is universally best. The right choice depends on how much control you need, how much operational work your team can carry, and how your traffic behaves.
| Platform | You manage | Scaling | Best for |
|---|---|---|---|
| Cloud Run | Container and its settings | Automatic, down to zero | HTTP services, event handlers, batch jobs, containerized apps |
| Cloud Run functions | Just the function code | Automatic, down to zero | Small single-purpose event reactions |
| GKE Autopilot or Standard | Cluster resources and workloads | Configurable pods and nodes | Complex platforms, custom networking, stateful systems, many coordinated services |
| App Engine | Application code and runtime config | Automatic | Existing App Engine applications and simple web apps |
| Compute Engine | Whole virtual machines | Managed instance groups you configure | Legacy lift and shift, special hardware, full OS control |
When Cloud Run is the natural choice
Choose Cloud Run when your workload is stateless or keeps its state elsewhere, speaks HTTP or gRPC, tolerates instances coming and going, and benefits from scaling to zero. It is also an excellent default when the team is small and wants to spend time on product features instead of cluster care.
When to look elsewhere
Cloud Run Advantages
- No cluster or server management
- Fast scaling and scale to zero
- Standard containers, easy portability
- Built-in revisions, traffic splitting, and rollbacks
- Simple pay-for-use pricing model
- Integrates tightly with IAM, logging, and monitoring
Cloud Run Trade-offs
- Instances are ephemeral, so stateful systems fit poorly
- Less low-level control than a cluster or a virtual machine
- Cold starts can affect latency-sensitive paths
- Limits on resources per instance and request duration
- Some network and daemon-style patterns are not supported
Choose a cluster platform when you need fine control over scheduling, custom operators, privileged workloads, or tightly coupled stateful systems. Choose virtual machines when you need a specific operating system, kernel behavior, or hardware arrangement. Many mature organizations use several of these together, placing each workload on the platform whose strengths it needs.
Compute Engine is buying a car and maintaining it. GKE is running your own taxi company with a garage and mechanics. Cloud Run is simply calling a ride when you need one. The more control you want, the more upkeep you accept.
17Best Practices and Common Mistakes
A short checklist of habits separates smooth Cloud Run operations from noisy ones.
Best practices
One identity per service
Create a dedicated service account with narrowly scoped permissions for every service.
Tune concurrency by testing
Run load tests and watch CPU, memory, and latency to find the highest safe value.
Set max instances deliberately
Base the limit on downstream capacity and budget, not on hope.
Keep images small and pinned
Use minimal base images and deploy by immutable digest for repeatable releases.
Use startup probes
Make readiness explicit so traffic never reaches a half-started instance.
Handle the termination signal
Finish in-flight work and close connections so deployments are invisible to users.
Log in a structured way
Emit structured entries with request identifiers and keep sensitive data out of logs.
Automate delivery
Build, scan, deploy, and promote through a pipeline, with canary steps and automated rollback conditions.
Common mistakes
Leaving a service public by accident. Review invoker permissions and ingress settings on every service, and use organization policies to prevent unintended public exposure.
Ignoring the startup window. If the application needs longer than expected to become ready, the platform treats the deployment as failed. Measure startup time under realistic conditions.
Over-allocating memory to avoid thinking. Memory above what the application needs raises cost without improving speed, while memory below the real peak causes sudden out-of-memory terminations.
Treating logs as free. Verbose logging at high volume creates real cost and makes true problems harder to see. Log what you will actually use.
Skipping idempotency in event handlers. Duplicate deliveries will eventually happen, and without protection they produce duplicate charges, emails, or records.
After every incident, ask which of these checklist items would have prevented it, and add it to your service template so every new service starts with the lesson already built in.
18Real-World and Industry Scenarios
Seeing how the pieces combine in realistic situations makes the concepts stick.
Retail flash sale API
An online retailer expects traffic to jump a hundredfold for a few hours. The product catalog API runs on Cloud Run behind a global load balancer with edge caching. Minimum instances keep latency low at the start, concurrency is tuned from load tests, and a maximum instance limit protects the inventory database. Checkout events are placed on a queue so payment processing proceeds at a safe rate. After the sale, the service shrinks back and costs fall with it.
Media processing pipeline
A publishing company receives images and videos uploaded to storage. Each upload triggers an event that starts a Cloud Run service to create thumbnails, and larger transcoding work is handed to a Cloud Run job with many parallel tasks. Results are stored back in object storage and metadata is written to a database. Because compute runs only while files are processed, idle time costs almost nothing.
Internal business tools
A finance team needs small web tools that only employees can open. Each tool is a Cloud Run service with ingress restricted and identity-aware access in front, so users sign in with company accounts. There are no servers to patch, and rarely used tools simply scale to zero.
Machine learning inference
A team serves a trained model behind an API. Cloud Run with GPU support runs the model container, loading model files from a mounted storage volume. Scale-to-zero keeps costs low between bursts, while minimum instances can keep one warm copy for steady demand. The team watches startup latency closely because model loading is the slowest part of a cold start.
Webhook receiver for partner integrations
A software company receives unpredictable bursts of callbacks from partner systems. A thin Cloud Run service validates each callback quickly, places it on a queue, and returns success at once. A second service processes the queue at a controlled pace and records outcomes. If a partner sends a flood, the queue absorbs it instead of the database.
Scheduled data synchronization
An operations team must copy and reconcile data between systems every night. A Cloud Scheduler entry starts a Cloud Run job that splits the work across tasks, retries failures automatically, and writes a summary to a monitoring channel. The team pays only for the hours the job actually runs.
In every scenario, Cloud Run handles the bursty, stateless compute, while durable state lives in dedicated data services and flow control is handled by queues or load balancers.
19Resilience, Multi-Region Design and Quotas
Production readiness means planning for the day something breaks.
A Cloud Run service is a regional resource. Within a region, Google runs your instances across multiple zones, so the loss of a single zone does not normally take your service down. But a whole-region problem, a bad deployment, or a failing downstream dependency can still hurt. Resilience planning addresses each of these separately.
Going multi-region
To survive a regional outage and to serve users from nearby locations, deploy the same service to two or more regions and place a global external load balancer in front. The load balancer sends each user to the closest healthy region and shifts traffic away if a region stops responding. The cost of this design is extra complexity: data must be available in each region, deployments must be coordinated, and you must decide what happens to writes when regions disagree.
Make the service region-agnostic
Remove any assumption that data or caches are local to one region. Configuration should be identical across regions.
Choose a data strategy
Decide between a globally replicated database, regional replicas with a primary, or region-owned data. This choice drives everything else.
Deploy identically everywhere
Use one pipeline that deploys the same image digest to each region in a controlled order, with canary checks between regions.
Front with a global load balancer
Attach each regional service as a backend and configure health-based failover and sensible timeouts.
Rehearse failure
Practice taking a region out of rotation. A failover path that has never been tested usually has a surprise waiting inside it.
Protecting against bad releases
Most outages are caused by changes, not hardware. Canary rollouts, tagged test URLs, and rapid rollback by traffic shifting are your strongest defenses. Pair them with automated checks that compare error rate and latency of the new revision against the old one, and stop the rollout automatically when the numbers worsen.
Taming dependencies
Your service is only as reliable as the systems it calls. Set timeouts on every outbound call, retry carefully with growing delays and a limit, and use circuit breakers so that a failing dependency is skipped quickly instead of tying up all your concurrency slots. When a non-essential dependency fails, degrade gracefully by returning partial results rather than an error.
Quotas and limits
Projects have quotas on things such as the number of instances, CPU and memory in use per region, and request rates. Quotas protect everyone, but they can also surprise a fast-growing service. Review them before launches, request increases early, and set alerts on quota usage. Also remember that resource limits per instance and the maximum request duration are fixed ceilings you design around, so always confirm current values in the official documentation.
Deploying to two regions but keeping a single-region database means an outage in the database’s region still takes everything down. Resilience must cover data, not only compute.
20Frequently Asked Questions
Short answers to the questions engineers ask most when adopting Cloud Run.
Yes. When a service has no traffic and no minimum instances, it can drop to zero instances and cost nothing for compute. The first request after that waits for a new instance to start, which is the cold start. Its length depends mostly on image size and your application’s startup work.
Yes, within the request timeout limit, which can be raised up to about an hour. Long-lived connections count as active requests, so they hold concurrency slots and affect scaling. Clients should be ready to reconnect when a connection ends.
Cloud Run functions let you deploy just a function and the platform builds the container for you, which is quick for small event handlers. Cloud Run itself accepts any container, so you control the runtime, the dependencies, and the full application structure.
Generally no. Instances are temporary and local storage does not persist. Run the application on Cloud Run and keep data in a managed database or storage service.
Set a maximum instance count, put a load balancer with a web application firewall and rate limits in front, keep the service private where possible, and add budget alerts. Together these bound both traffic and spending.
In request-based billing, the CPU is throttled once no requests are active, so work that continues after the response may stall. Finish the work before responding, switch to instance-based CPU allocation, or send the work to a queue or job.
Yes. Set the callee to accept internal traffic, give the caller’s service account the invoker permission on the callee, and have the caller attach an identity token. No public exposure or shared passwords are required.
Shift traffic back to the previous known-good revision. Because revisions are immutable and remain stored, the rollback takes effect within moments and does not require rebuilding anything.
Split when parts of the system have different scaling needs, permissions, or release schedules. Keep things together when splitting would only add network calls and operational overhead without a clear benefit.
Load test it and tune concurrency, CPU, and memory together while watching latency and instance count. This one exercise usually improves both performance and cost more than any other change.
21Summary and Key Takeaways
Cloud Run gives you a simple promise with deep machinery behind it: bring a container, and Google runs it, scales it, secures its edge, and bills you for what you use. Mastering it means understanding the few ideas that drive everything else. Revisions give you safe releases. Concurrency and instance limits shape cost and stability. Request-based and instance-based CPU decide how your background work behaves. Identity and ingress settings decide who can reach you and what you can reach. Statelessness, idempotency, and good observability keep the system calm when things go wrong.
Used with discipline, Cloud Run lets a small team run services that scale from nothing to very large without owning a single server. Used carelessly, it can become a public, over-scaled, over-privileged, and surprisingly expensive set of containers. The difference is not luck; it is the habits described in this guide.
Key Takeaways
- Think in revisions — every change creates an immutable snapshot, so canary releases, tagged testing, and instant rollbacks are built in.
- Tune concurrency first — it is the setting that most affects cost, latency, and cold starts, and it must be validated by load testing.
- Choose the CPU mode on purpose — request-based billing suits spiky APIs, while instance-based allocation suits steady traffic and background processing.
- Keep containers stateless — place sessions, files, and records in managed data services, and cap instance counts to protect connection limits.
- Secure every layer — use private services by default, one least-privilege identity per service, Secret Manager for credentials, and a load balancer with a firewall for public traffic.
- Match the resource to the work — services for requests, jobs for run-to-completion tasks, and worker pools for continuous background consumers.
- Design for failure and observe everything — handle shutdown signals, make event handlers idempotent, set timeouts, define service level objectives, and rehearse regional failover.