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
- Expose a
/healthzendpoint. - Check critical dependencies.
- Return appropriate HTTP status codes.
- 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.