Video in progress

Why is it dangerous to check the database connection inside the liveness probe?

If the DB slows down or drops for a moment, all pods fail liveness at the same time and Kubernetes restarts them. A restart does not fix the DB, it only adds cold-start load and turns a small problem into a full outage.

📖 Answer

Liveness has one question: "is this process alive or stuck, and would a restart help?" When the DB is down, the application process is alive, and a restart changes nothing.

A chain you see in prod: a Postgres failover takes 20 seconds, so health is DOWN on all pods, the kubelet restarts them one after another, the new pods have no JIT warm-up and open the connection pool all at once, and when the DB comes back hundreds of connections hit it at the same moment, so it slows down again. A cascading failure.

Spring Boot separates this correctly:

management:
  endpoint:
    health:
      probes:
        enabled: true      # turned on automatically in Kubernetes
      group:
        readiness:
          include: readinessState, db
  • /actuator/health/liveness: only LivenessState, no external systems;
  • /actuator/health/readiness: you can add the DB if you want. The pod leaves the traffic but is not killed.

Be careful adding it to readiness too: if the DB is shared, all pods go not-ready at once and the service returns 503. Sometimes that is the right behavior, and sometimes it is better to keep running in a degraded state.

Also make liveness's failureThreshold and periodSeconds forgiving: a restart because of a long GC pause is not wanted either.