cloud run
Ready is not alive

The API is an Express process on Cloud Run with forty-odd scheduled jobs and a WebSocket server living inside the same container. Getting its health probes right took three passes over four weeks, and each pass was wrong in a way the one before couldn't have shown me.
The endpoints are now /v1/health/ready and /v1/health/live. They answer
different questions, and one of them does no I/O at all.
Pass one: the default is a port check
If you deploy a Cloud Run service with no startup probe, Cloud Run decides the revision is ready as soon as something accepts a TCP connection on the container port.
For an Express app app.listen() happens almost immediately. At that point the
database pool hasn't connected, Redis may not be reachable, and the process
hasn't established that any of its configuration is valid. So a revision whose
DATABASE_URL points at a private IP it cannot route to will bind the port,
pass the check, be marked ready, receive one hundred percent of traffic and
serve errors, and the deploy reports success.
The first fix was a startup probe pointed at the app's existing deep health endpoint:
startup_probe {
http_get {
path = var.startup_healthcheck_path
port = var.container_port
}
initial_delay_seconds = 0
period_seconds = 10
timeout_seconds = 10
failure_threshold = 24
}
Three numbers worth defending. timeout_seconds = 10 because Redis's client
uses a ten-second connect timeout and a cold first connection can use most of
it, so a five-second probe timeout turns a slow-but-fine dependency into a
failed deploy. period_seconds × failure_threshold = 240s, and that's Cloud
Run's maximum startup window. Going higher isn't permitted. Going much lower
means a container that is merely slow gets killed and retried, and then a
ten-second delay becomes a five-minute rollout.
This was a real improvement. A revision that can't reach its dependencies now fails its deploy instead of serving.
Pass two: coupling liveness to the same endpoint
I had a good deep check now, and the obvious next move was to use it as the liveness probe too, on the theory that an unhealthy container should be restarted. The theory is wrong. Readiness and liveness ask different questions.
Readiness asks whether this instance may receive traffic. A negative answer takes that one instance out of rotation untill it recovers.
Liveness asks whether this container should be destroyed and replaced. Fail it and the process gets killed.
Now imagine the database is unreachable for four minutes (a failover, a maintenance window, a network blip on the PSA path). Every instance's deep check fails, so every instance's liveness probe fails. Cloud Run kills all of them and starts fresh containers, and those fresh containers run a startup probe against the same unreachable database, fail it, and get killed too. You've converted a dependency outage into a restart storm and thrown away every warm connection pool and every in-flight request. And the database, the one component that is struggling, is now being hammered by a fleet of cold processes all opening new connections at once. The restarts don't help, because the problem was never inside the container.
So a liveness probe should only test things a restart can repair. A wedged event loop, a deadlock, an unrecoverable internal state. Never a dependency. A liveness check that reaches across the network will restart your containers every time something they depend on has an outage.
Pass three: two endpoints, two contracts
So they split. /v1/health/ready is the startup-probe target and gates deeply:
const [database, redis, lockDatabase, analyticsDatabase] = await Promise.all([
checkMainDatabase(),
checkRedis(),
checkLockDatabase(),
checkAnalyticsDatabase(),
]);
const gatingUnhealthy =
database.status === 'unhealthy' ||
redis.status === 'unhealthy' ||
lockDatabase.status === 'unhealthy';
Three dependencies gate and one doesn't.
Redis gates because without it sessions silently fall back to per-instance memory. That's worse than an outage. Everything appears to work while users get logged out whenever they land on a different instance, and I don't want a failure that looks like success to get past a readiness check.
The lock database gates because the scheduled jobs use it to decide which instance runs a given tick. Without it, every background job on the platform fails closed. An API that answers requests perfectly while nothing scheduled runs is the sort of half-alive state that can go unnoticed for days.
Analytics does not gate, and I chose that. It powers admin dashboards. If
the analytics database is down and analytics is a gating dependency, then a
reporting outage removes core API capacity - bookings stop, check-in stops,
payments stop - to protect a chart. So the endpoint reports degraded and
returns 200, and a reporting outage leaves the dashboards broken and the API
serving.
/v1/health/live is the other half, and its implementation is the shortest
function in the server:
app.get('/v1/health/live', (req, res) => {
if (isDraining()) {
return res.status(503).json({ status: 'draining', timestamp: new Date().toISOString() });
}
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
It does no I/O. It proves the event loop is turning and the process has not
begun shutting down. The probe settings match that: period_seconds = 30,
timeout_seconds = 5, failure_threshold = 3, so ninety seconds of sustained
unresponsiveness before a restart. The timeout can be tight because the handler
does nothing. If it can't answer in five seconds the event loop is blocked, and
that is something a restart repairs.
The original /v1/health stayed, unchanged. It's the Dockerfile's
HEALTHCHECK target for local Docker and there was no reason to break it.
Churn the endpoints during a probe migration and you end up with a revision
whose probe points at a path the image doesn't serve yet.
Startup is not continuous readiness
One limitation took me a while to internalise. Cloud Run's startup probe runs at startup. It gates whether a new revision or a scale-up instance may begin serving. It does not run continuously, so an instance that was healthy at boot and has since lost its database won't be pulled out of rotation. It keeps recieving traffic and keeps failing requests.
Kubernetes has a separate continuous readiness probe for this. Cloud Run does not. What it has is liveness, and for the reasons above that is the wrong tool. So the gap is real and the mitigation has to live somewhere else: in the application returning accurate errors, and in monitoring that watches the error rate instead of a probe.
Draining is a third answer
All three endpoints share one early return:
if (isDraining()) {
return res.status(503).json({ status: 'draining', ... });
}
On SIGTERM the process marks itself draining before it does anything else, and
from that moment every health endpoint reports 503. Then it stops the
schedulers, closes WebSocket connections, gives in-flight HTTP up to four
seconds to finish, releases the job leases it isn't actively using, flushes
telemetry and closes its stores. A watchdog forces exit at nine seconds
regardless.
The ordering matters. With draining marked first, the platform's own health checks stop claiming this instance is available before the instance starts dismantling itself, and not halfway through. Leases held by work that's still running are not released. They expire on their TTL instead, so another instance can't pick up a job that is still executing here.
Concurrency is a probe question wearing a different hat
The last change this week was request concurrency: 800 per instance in production, with a maximum of four instances.
Those numbers look like scaling knobs, but really they're a connection budget.
The server computes its database pool size from the real max_connections of
the instance divided by the peak number of application instances (both are
passed in as deployed configuration, not guessed) and refuses to start if
either is missing. So max_instances doesn't mean "how much traffic can we
take". It means "how many processes are allowed to exist at once, each holding a
slice of a fixed number of database connections."
Raising concurrency instead of instance count is the cheap direction. One process serving eight hundred concurrent requests holds one pool, and eight processes serving one hundred each hold eight. This workload is mostly waiting on I/O, so the CPU cost of the extra concurrency is small and the connection cost of the extra instances is not.
WebSockets complicate this. Each long-lived connection counts as one concurrent request, and Cloud Run will not reclaim an instance holding an open request. So when a new revision takes over, the old one keeps every instance that still has a socket attached, and both revisions bill simultaneously until those sockets are severed. Readiness says nothing about this because the instance is perfectly healthy. It just doesn't go away.
What a passing probe still does not tell you
/v1/health/ready proves the dependencies are reachable from this container. It
does not prove the container did everything it was supposed to do at startup.
Some of those forty scheduled jobs catch and log their own initialisation failures and don't crash the process. For most of them that's the right call, one broken poller should not take the API down. But it means a revision can pass a deep readiness check, serve traffic correctly, and still have failed to schedule something.
A passing probe says the HTTP listener works. It says nothing about the scheduled work running beside it, and that's where I now expect most of our production surprises to come from.


