Give Cloud Run health probes a recovery policy
In brief
Match each Cloud Run health probe to a recovery action.
- Startup gates the first request. Readiness withdraws an instance from routing, so guard requests while routing catches up.
- Reserve liveness failures for local problems a restart can fix.
We’d design a health check by deciding what should happen when it fails.
Waiting for initialization, sending traffic elsewhere, and replacing a broken process solve different problems. Wiring the same dependency test to all three actions can turn a supplier outage into a fleet of restarting containers.
For a Cloud Run HTTP service, use startup to confirm that the application can safely serve its first request. Use readiness when an instance should temporarily stop receiving new traffic. Reserve liveness for an instance-local failure that replacement can plausibly repair.
Ask whether the action improves the condition. An unavailable shared database doesn’t become available because the web process restarts. A deadlocked request executor may recover when the process is replaced.
Give each signal a specific meaning
Cloud Run defines startup and liveness probes separately. Startup gates the other probes and service admission.
A liveness failure causes replacement. Readiness checks can withdraw an instance from new traffic and later return it without terminating the process.
For an application that serves stored document metadata and optionally enriches it from a supplier, we’d use this policy:
| Condition | Probe results |
|---|---|
| Required configuration still loading | Startup and readiness fail. Liveness doesn’t request a restart. |
| Local serving state loaded and usable | All three pass. |
| Optional supplier unavailable | Startup and readiness report the instance as usable. Liveness passes. |
| Local serving state being safely rebuilt | Startup no longer applies. Readiness fails until the state is usable. Liveness passes while the repair progresses. |
| Local executor permanently wedged | Startup can’t pass. Readiness fails when it can observe the failure. Liveness fails or times out. |
The table describes application policy. The platform samples endpoints and applies thresholds rather than acting at the exact moment a condition changes.
Startup also stops being a gate once it succeeds. After that, a local failure belongs in the matching runtime check and in the request handler.
Liveness should cover the execution path you need to recover. A separate health thread that always returns success can stay responsive while every application worker is deadlocked. A probe that waits behind an ordinary slow supplier has the opposite problem: it can mistake external latency for a broken process. Point the probe at the executor or local progress signal it’s meant to observe.
Startup success must mean the first request is safe
Make startup and readiness logic agree.
Cloud Run can send traffic after startup succeeds, before the first readiness result. A listening socket isn’t enough while the process still needs to load its routing table, validate mandatory configuration, or initialize a required local model.
Expose the probe handler early if initialization is asynchronous. Keep startup failing until that work finishes. Then publish the completed serving state in one step. Avoid setting a general started flag and then loading the object a handler immediately dereferences.
This TypeScript policy uses one read-only snapshot for the local decision:
type ServingState = Readonly<{
initialized: boolean;
accepting: boolean;
restartRequired: boolean;
}>;
function canServe(state: ServingState): boolean {
return state.initialized && state.accepting && !state.restartRequired;
}
function probeStatus(path: string, state: ServingState): 200 | 503 | undefined {
if (path === '/health/startup' || path === '/health/ready') {
return canServe(state) ? 200 : 503;
}
if (path === '/health/live') {
return state.restartRequired ? 503 : 200;
}
return undefined;
}Mount these paths as HTTP/1 GET handlers in the existing server. Read the current snapshot once per request, call probeStatus, and return its status with a small fixed body. When it returns undefined, the normal router handles that path. Keep configuration, credentials, stack traces, and dependency details out of the response.
Application code owns these state transitions and publishes replacement snapshots under the server’s normal concurrency protection. Initialization sets initialized and accepting together only after required objects are usable. A recoverable local rebuild clears accepting, while an irrecoverable local failure sets restartRequired.
In a process with several workers, define whose state each probe observes. One healthy worker shouldn’t hide a failure that disables the others.
Normal handlers also need an admission check using canServe(state) before accessing those objects. When it’s false, refuse the request safely. Protect object lifetime and publication with the same synchronization, so a state change can’t invalidate an object between the check and its use. The locking and the framework integration around this function are yours to write.
Configure enough time for the intended recovery
For an existing service, the following fragment goes inside its container definition in a service YAML configuration. It assumes port 8080, HTTP/1 probes, and instance-based billing. Account for the whole instance lifetime: the current billing guide lists instance-based billing for health probes.
startupProbe:
httpGet:
path: /health/startup
port: 8080
periodSeconds: 5
timeoutSeconds: 1
failureThreshold: 12
livenessProbe:
httpGet:
path: /health/live
port: 8080
periodSeconds: 10
timeoutSeconds: 1
failureThreshold: 3
readinessProbe:
httpGet:
path: /health/ready
port: 8080
periodSeconds: 5
timeoutSeconds: 1
failureThreshold: 3
successThreshold: 2The values are examples. The startup budget assumes an initialization that normally finishes well within a minute. Measure your own initialization times and leave room for expected variation. A permanently invalid configuration should fail deployment visibly instead of spending minutes waiting for a repair the process can’t perform.
The liveness threshold tolerates an isolated missed check before replacing the container. Set it from the longest acceptable local stall and the cost of an unnecessary restart. Readiness requires repeated success before returning traffic, which can reduce rapid removal and re-entry during an unstable recovery.
Return a plain 200 or 503. The documented HTTP probe success range includes redirects, so login middleware that answers with a redirect can satisfy a probe by accident. Keep the service’s access controls on these paths. Inspect the response without following redirects.
Shared failures need a different policy
For the optional supplier lookup, leave local readiness intact and apply the route’s existing timeout, admission, and fallback rules. Withdrawing the whole instance would also remove its working metadata route. Probe requests shouldn’t create fresh supplier work on every check.
If every safe request requires a dependency, failing readiness can be appropriate. That keeps knowingly unusable instances from taking new work.
When every instance shares the outage, though, there’s still no healthy destination. Limit retry pressure and make the service-level failure visible.
A cached dependency observation also needs a freshness rule. “Last check succeeded” from an hour ago is a weak readiness signal. Define the observation’s maximum age, timeout, and recovery behavior. Make sure the observer runs under the configured CPU lifecycle. A continuous heartbeat stops progressing whenever its process has no CPU allocated.
Keep the liveness decision local unless there’s evidence that replacing this particular process fixes its dependency state. A permanently poisoned client may justify replacement after bounded local repair fails, but ordinary connection loss that the client can recover from doesn’t. Write the distinction down so the next maintainer doesn’t broaden the check into a general “everything is reachable” test.
Check the routing limits before relying on them
Readiness for Cloud Run services reached general availability in the June 29, 2026 release. This policy assumes session affinity is disabled, because affinity can keep directing requests to an instance after readiness fails. That’s another reason the application must reject unsafe work itself.
Services that used a v1 readiness configuration before November 2025 need a migration across two revisions. Remove that definition in one revision, then add the supported definition in another.
An old field being present doesn’t mean routing responds to it. Check the service’s configuration history and verify the behavior in an isolated revision before depending on it.
Liveness failure is also a destructive recovery action. Cloud Run shuts the container down with SIGKILL. Correctness can’t depend on a graceful shutdown handler, and work already accepted must stay recoverable if the process disappears.
Test failures that require different actions
Start the server with initialization held open. Confirm that the HTTP listener responds, startup fails, and a normal request is refused. Complete initialization, then send a real application request immediately after the first successful startup response. It must work without waiting for a readiness sampling cycle.
Next remove only the optional supplier. The local metadata route should still work, and liveness should keep passing.
Exercise a recoverable local-state rebuild separately: readiness and normal admission fail, liveness keeps passing, and restoring a usable snapshot lets requests through again.
Finally, block the request executor and verify that the probe times out, or inject the irrecoverable local condition the probe observes. If your architecture serves the probe from an independent executor, repeat the test that way to expose false health. Setting a boolean in a unit test doesn’t exercise deadlock detection.
In a staging revision, observe routing withdrawal, recovery, instance replacement, and request outcomes with the deployed thresholds. Record probe failures by reason in private logs, along with initialization duration and restart frequency. If readiness stays stable while work stalls, check the work’s own progress signal.
More in this domain: Infrastructure
Browse allWhen a BigQuery service perimeter earns its operational cost
Adopt a service perimeter for a named prohibited data-transfer path, then prove that required queries still work and the prohibited transfer fails.
Rotating a Cloud Run credential while old revisions still exist
Keep both credentials valid while Cloud Run consumers move to a verified replacement.
Detect stalled Cloud Run work behind a healthy endpoint
Detect stalled Cloud Run work from overdue operations and their last meaningful progress.
Cloud Tasks vs. Pub/Sub for Cloud Run: routing, retries, and rate limits
Use Cloud Tasks to schedule or throttle a specific operation. Use Pub/Sub to deliver one event to independent consumers.
Cloud Run scale to zero: cold starts and minimum instances
Let Cloud Run scale to zero when the first useful request after idle time meets the caller's deadline.
Related patterns
Cloud Run request timeouts don't kill your code (so your architecture has to)
A Cloud Run request timeout returns a 504 while the handler may keep running.
Direct VPC egress vs. Serverless VPC Access for Cloud Run
Start with Direct VPC egress for new Cloud Run connections to a VPC.
Internal-only Cloud Run: ingress, identity, and the caller's network path
Internal Cloud Run access requires both an allowed network path and an authorized caller.
How we decide between Cloud SQL connectors, Auth Proxy, and private IP
Choose the Cloud SQL network route and the connection helper separately. Either a language connector or the Auth Proxy can use private IP.