{
    "slug": "bulkhead_pattern",
    "term": "Bulkhead Pattern",
    "category": "architecture",
    "difficulty": "advanced",
    "short": "Isolating system components into separate resource pools so a failure in one doesn't cascade and exhaust resources for others.",
    "long": "Named after the watertight compartments in a ship's hull, the Bulkhead pattern prevents a failure in one service from cascading by isolating resource pools (thread pools, connection pools, semaphores) per downstream dependency. If calls to Service A consume all available threads, Service B's thread pool remains unaffected. In PHP architectures, bulkheads are implemented via: separate database connection pools per service, queue workers dedicated to specific job types, rate limits per API client, and circuit breakers per downstream endpoint. Bulkheads complement circuit breakers — circuit breakers detect failure, bulkheads contain its blast radius.",
    "aliases": [
        "bulkhead",
        "isolation pattern",
        "failure isolation"
    ],
    "tags": [
        "architecture",
        "resilience",
        "microservices",
        "design-pattern"
    ],
    "misconception": "Bulkhead patterns only apply to microservices. Bulkheads isolate failure — separate thread pools for different operations in a monolith, separate database connection pools per feature, or separate worker queues per customer tier all apply the bulkhead principle.",
    "why_it_matters": "Bulkheads isolate failures to a partition — if one pool of threads or connections exhausts, other pools continue serving unaffected workloads, preventing a single slow dependency from degrading the entire service.",
    "common_mistakes": [
        "One shared thread pool or connection pool for all operations — slow calls to one service starve calls to all others.",
        "Not sizing bulkheads based on actual workload — too small causes unnecessary rejection; too large defeats the isolation.",
        "Bulkheads without fallbacks — isolation prevents cascading failure but callers still need a degraded-mode response.",
        "Not monitoring per-bulkhead utilisation — you cannot tune what you cannot observe."
    ],
    "when_to_use": [
        "Isolate thread/connection pools per downstream dependency — so a slow third-party API cannot starve requests to your database.",
        "Apply bulkheads when a single shared resource pool services multiple unrelated workloads with different SLAs.",
        "Use process-level bulkheads (separate workers per queue) to stop a backlogged job type from blocking time-sensitive ones."
    ],
    "avoid_when": [
        "Avoid over-partitioning small systems — splitting a 20-connection pool into five pools of four creates artificial scarcity.",
        "Do not apply bulkheads as a substitute for fixing a genuinely slow dependency — isolation contains the damage but does not cure the cause."
    ],
    "related": [
        "circuit_breaker",
        "retry_pattern",
        "rate_limiting",
        "defence_in_depth"
    ],
    "prerequisites": [
        "circuit_breaker",
        "microservices",
        "connection_pooling"
    ],
    "refs": [
        "https://microservices.io/patterns/reliability/bulkhead.html"
    ],
    "bad_code": "// Single shared connection pool — one slow service starves everything:\n$pool = new ConnectionPool(maxConnections: 20); // Shared by all services\n$userResult   = $pool->query('SELECT ...'); // 18 connections consumed by slow report\n$orderResult  = $pool->query('SELECT ...'); // Starved — waits for pool\n$reportResult = $pool->query('SELECT ...'); // Holds all connections for 30s",
    "good_code": "// Bulkhead — isolate failures to prevent cascading\n// Like watertight compartments in a ship\n\n// Thread/process pool isolation in PHP via queue workers:\n// payments-worker: max 5 workers — payment failures don't starve emails\n// emails-worker:   max 3 workers\n// reports-worker:  max 2 workers\n\n// config/horizon.php\n'payment-queue' => [\n    'connection' => 'redis',\n    'queue'      => ['payments'],\n    'maxProcesses' => 5,\n],\n'email-queue' => [\n    'connection' => 'redis',\n    'queue'      => ['emails'],\n    'maxProcesses' => 3,\n],\n\n// Database connection pool bulkhead:\n// Separate connection pools for read vs write vs reporting\n// If reporting queries spike, they can't starve transactional connections",
    "example_note": "The bad example shares one 20-connection pool across all services; a slow payment provider exhausts the pool and takes down user lookups. The fix gives each service its own capped pool.",
    "quick_fix": "Assign separate thread pools or PHP-FPM pools to different feature areas — a slow payment API won't exhaust all workers and prevent search from responding",
    "severity": "high",
    "effort": "high",
    "created": "2026-03-15",
    "updated": "2026-03-31",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/bulkhead_pattern",
        "html_url": "https://codeclaritylab.com/glossary/bulkhead_pattern",
        "json_url": "https://codeclaritylab.com/glossary/bulkhead_pattern.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": "[Bulkhead Pattern](https://codeclaritylab.com/glossary/bulkhead_pattern) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/bulkhead_pattern"
            }
        }
    }
}