How to implement health checks for services?

· Category: DevOps & CI/CD

Short answer

Health checks are endpoints that report service status. Liveness checks detect deadlocks, readiness checks determine traffic eligibility, and deep checks verify dependencies.

Steps

  1. Expose a /healthz endpoint.
  2. Check critical dependencies.
  3. Return appropriate HTTP status codes.
  4. Integrate with load balancers and orchestrators.

Example

@app.get("/health")
def health():
    if db.is_connected():
        return {"status": "ok"}
    raise HTTPException(status_code=503)

Tips

  • Keep checks lightweight.
  • Separate liveness and readiness.
  • Avoid cascading failures in deep checks.

Common issues

  • Heavy health checks impact performance.
  • False negatives from transient issues.
  • Missing checks cause delayed failure detection.