{
    "slug": "health_check_patterns",
    "term": "Health Check Patterns",
    "category": "observability",
    "difficulty": "beginner",
    "short": "Health checks report service status to load balancers and orchestrators — /health/live (is the process running?), /health/ready (can it serve traffic?), and deep health checks for dependencies.",
    "long": "Kubernetes probes: liveness (restart if failing), readiness (remove from load balancer if failing), startup (wait for slow start before liveness kicks in). Patterns: shallow (is process alive? just return 200), deep (check DB, cache, queue), readiness (are all dependencies ready?). Shallow for liveness — avoid cascading failures (if DB is down, all instances fail liveness, all restart, making DB situation worse). Deep for readiness — remove from rotation without restarting. Deep health check components: DB query (SELECT 1), cache ping, queue connectivity, disk space check, memory check. Timeout all checks (1-2s max).",
    "aliases": [],
    "tags": [
        "observability",
        "health-checks",
        "kubernetes",
        "reliability"
    ],
    "misconception": "Health check should verify all dependencies are working — use deep checks for readiness; use shallow checks for liveness. Deep liveness checks cause cascading failures when a dependency is down.",
    "why_it_matters": "Correctly implemented health checks are the foundation of zero-downtime deployments and automatic failure recovery — wrong probes cause unnecessary restarts or leave broken instances in rotation.",
    "common_mistakes": [
        "Deep health check for liveness — DB down → all instances restart → thundering herd.",
        "No readiness probe — instances receive traffic before warming up.",
        "Health check without timeout — slow dependency causes health check to time out and restart healthy instance."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "golden_signals",
        "alerting_best_practices",
        "canary_deployment_obs"
    ],
    "prerequisites": [
        "golden_signals"
    ],
    "refs": [
        "https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/"
    ],
    "bad_code": "// Liveness checks DB — cascading failure on DB outage:\nroute('/health/live', function() {\n    DB::select('SELECT 1'); // DB down → liveness fails → instance restarts → more DB pressure\n    return ['status' => 'ok'];\n});",
    "good_code": "// Shallow liveness — is process running?\nroute('/health/live', fn() => ['status' => 'alive']);\n\n// Deep readiness — can serve traffic?\nroute('/health/ready', function() {\n    $checks = [\n        'db' => fn() => DB::select('SELECT 1'),\n        'cache' => fn() => Cache::has('health-check') || Cache::set('health-check', 1, 10),\n    ];\n    foreach ($checks as $name => $check) {\n        try { $check(); } catch (Exception $e) {\n            return response(['status' => 'not ready', 'failed' => $name], 503);\n        }\n    }\n    return ['status' => 'ready'];\n});",
    "quick_fix": "Separate /live (shallow, always fast) from /ready (deep, checks dependencies). Add startup probe for slow-starting services. Timeout all dependency checks at 1-2s.",
    "severity": "high",
    "effort": "low",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/health_check_patterns",
        "html_url": "https://codeclaritylab.com/glossary/health_check_patterns",
        "json_url": "https://codeclaritylab.com/glossary/health_check_patterns.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Health Check Patterns](https://codeclaritylab.com/glossary/health_check_patterns) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/health_check_patterns"
            }
        }
    }
}