A 40-second database blip restarted every pod because our liveness probe checked the database
A routine database failover took 40 seconds. Our API's liveness probe queried the database, so Kubernetes killed every API pod at once, and they came back into a thundering herd of reconnects that lasted 12 minutes. What liveness, readiness and startup probes are for, and what each should check.
Our managed PostgreSQL provider performs a failover during maintenance windows. Normally it takes 20 to 40 seconds, our services see a burst of connection errors, retry, and carry on. One Tuesday, the same 40 second failover turned into a 12 minute outage of the public API.
The Kubernetes events told the story:
LAST SEEN TYPE REASON OBJECT MESSAGE
02:01:14 Warning Unhealthy pod/api-7f9c-2xk4q Liveness probe failed: HTTP probe failed with statuscode: 503
02:01:14 Warning Unhealthy pod/api-7f9c-8mhtz Liveness probe failed: HTTP probe failed with statuscode: 503
...
02:01:34 Normal Killing pod/api-7f9c-2xk4q Container api failed liveness probe, will be restarted
02:01:34 Normal Killing pod/api-7f9c-8mhtz Container api failed liveness probe, will be restarted
... (all 24 pods)
All 24 API pods were killed within the same few seconds. The probe:
livenessProbe:
httpGet:
path: /health
port: 8080
periodSeconds: 10
failureThreshold: 3
timeoutSeconds: 2
And the handler behind /health:
func health(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), time.Second)
defer cancel()
if err := db.PingContext(ctx); err != nil {
http.Error(w, "db unavailable", http.StatusServiceUnavailable)
return
}
if err := redis.Ping(ctx).Err(); err != nil {
http.Error(w, "redis unavailable", http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
}
The liveness probe checked the database. When the database was unavailable during failover, every pod's liveness probe failed three times in a row (30 seconds), and Kubernetes restarted every container.
Why 40 seconds became 12 minutes
The restarts made things far worse than the failover itself:
- All 24 pods restarted at the same moment. Each took about 25 seconds to start (loading configuration, warming caches), so for that time there was no capacity at all.
- When they came up, they all opened their connection pools at once, 24 pods times 20 connections, against a database that had just failed over and was warming its own cache. Connection attempts timed out.
- Health checks failed again because the database was slow to accept connections, and some pods were restarted a second time.
- Kubernetes applies an exponential back-off to repeatedly crashing containers (CrashLoopBackOff), so the later restarts waited longer and longer.
The database was healthy at 02:02. The API was not fully back until 02:13.
What each probe is for
Kubernetes has three probes, and they answer different questions:
| Probe | Question | Action on failure |
|---|---|---|
| liveness | is this process broken in a way only a restart can fix? | restart the container |
| readiness | should this pod receive traffic right now? | remove from Service endpoints, no restart |
| startup | has the application finished starting? | other probes are disabled until it succeeds |
A database outage is not something a restart can fix. Restarting the API does not bring the database back. So it must never fail the liveness probe. The Kubernetes documentation makes this point: liveness probes should not depend on external dependencies, because a dependency failure would cause cascading restarts.
What a liveness probe should detect: a deadlocked process, an event loop that has stopped, a server that no longer accepts connections. Things inside the container, for which restarting is the cure.
Readiness and dependencies
It is tempting to move the database check to readiness instead. That is better, since readiness failures do not restart anything, but it still needs care. If every pod's readiness depends on the same database, a database blip marks all pods not ready, the Service has no endpoints, and every request fails with a connection error at the load balancer instead of a clean error from the application. For a short blip, it is usually better for pods to stay ready and return fast errors (or serve from cache) for the requests that need the database.
Readiness is the right place for things specific to this pod: still starting, draining before shutdown, a local cache not yet warm, or the pod being overloaded.
Environment
| Item | Value |
|---|---|
| Platform | Kubernetes 1.30, 24 API pods |
| App | Go HTTP service, pool of 20 DB connections per pod |
| Database | managed PostgreSQL, failover 20 to 40 s |
| Liveness | /health checks DB and Redis, 3 failures at 10 s period |
1. Separate endpoints for separate questions
// Liveness: is the process able to serve HTTP at all?
func livez(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
// Readiness: should this pod receive traffic?
func readyz(w http.ResponseWriter, r *http.Request) {
if shuttingDown.Load() || !warm.Load() {
http.Error(w, "not ready", http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
}
livez succeeding only proves the HTTP server goroutines are responsive, which is exactly what liveness should
prove. If the process deadlocks, the handler does not run and the probe times out.
For a stronger liveness signal, we added a watchdog inside the process: a background goroutine that updates a
timestamp every second, and livez fails if the timestamp is older than 10 seconds. That catches a stalled
scheduler or a deadlock that blocks the ticker, still without touching any external dependency.
2. Probe configuration
startupProbe:
httpGet: { path: /livez, port: 8080 }
periodSeconds: 2
failureThreshold: 30 # up to 60 s to start
livenessProbe:
httpGet: { path: /livez, port: 8080 }
periodSeconds: 10
timeoutSeconds: 2
failureThreshold: 6 # 60 s of failures before restart
readinessProbe:
httpGet: { path: /readyz, port: 8080 }
periodSeconds: 5
failureThreshold: 2
- The startup probe gives the pod time to start without the liveness probe killing it during a slow boot.
- Liveness is lenient: a minute of failures before restart. A restart is a heavy action and should be reserved for a process that is genuinely stuck.
- Readiness is responsive: a pod that starts draining is out of rotation within ten seconds.
3. Handle dependency failures in the request path
With the database check out of the probes, the application has to deal with database errors itself, which it should anyway:
- Requests that need the database return
503withRetry-After: 5quickly when the pool cannot get a connection within 500 ms, instead of hanging. - Read endpoints that can serve from Redis or an in-memory cache do so.
- The connection pool retries with jittered backoff, so 24 pods do not reconnect in lockstep when the database returns.
4. Report dependency health somewhere useful
The database and Redis checks did not disappear. They moved to /healthz/deps, which is not used by any
Kubernetes probe. Our monitoring scrapes it and alerts, and the status page reads it. Dependency health is a
monitoring signal, not a reason to restart.
5. A PodDisruptionBudget does not help here
We checked: PodDisruptionBudgets limit voluntary disruptions such as drains and evictions. Liveness probe restarts are not covered. The only protection against mass liveness restarts is a liveness probe that does not fail for shared reasons.
1. Database failover drill. In staging, we triggered a failover of the test database under load:
| Version | Pods restarted | API error window | Errors returned |
|---|---|---|---|
| Old probes | 24 of 24 | 11 min | connection refused, 502 at LB |
| New probes | 0 | 41 s | fast 503 with Retry-After |
During the 41 seconds, read endpoints backed by cache kept serving normally.
2. Liveness still catches real hangs. We added a debug endpoint (staging only) that deadlocks the watchdog goroutine. The liveness probe failed after the threshold and Kubernetes restarted the pod, as intended.
3. Policy check. A CI check on our Helm charts fails if a liveness probe path is the same as the readiness path, or if a liveness handler's code references the database or cache clients (checked with a simple static analysis rule on the handler package).
4. Production. The next provider maintenance failover caused 38 seconds of 503s on write endpoints and no restarts.
5. Security implications. The old /health endpoint was publicly reachable through the ingress and revealed
which dependency was down. The new probe endpoints are served on a separate internal port not exposed by the
ingress, and /healthz/deps requires an internal token.
Takeaways
- Liveness answers "is a restart the cure?" Never fail it because a shared dependency is down.
- Readiness controls traffic, not restarts; use it for pod-local conditions like startup and draining.
- Use a startup probe for slow boots, and make liveness lenient, restarts are expensive.
- Handle dependency outages in the request path with fast errors and cached fallbacks, and monitor dependency health separately.
References: Kubernetes liveness, readiness and startup probes, Configure liveness, readiness and startup probes, Kubernetes pod lifecycle, container restart policy, PodDisruptionBudget.
Add a label
Embed this post
<iframe src="https://www.hibug.me/liveness-probe-checks-database-cascading-restarts" width="700" height="420" frameborder="0" loading="lazy"></iframe>
I am a site reliability engineer on a platform team that runs the edge for dozens of internal services: API gateways, reverse proxies, load balancers and the resilience settings everyone copies from each other. The incidents I learn most from happen when a safety mechanism does the wrong thing. A rate limit that locked out an entire office behind one IP, a circuit breaker that never opened, a liveness probe that restarted every pod during a database blip, two nodes that both decided they were primary. Each post explains what the mechanism was meant to do, why it misfired in our setup, how we changed the configuration and how we drilled the failure afterwards to prove it. I keep the examples close to real configs so you can compare them with your own.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.