{
    "slug": "worker_pool_php",
    "term": "Worker Pool Patterns",
    "category": "performance",
    "difficulty": "advanced",
    "short": "Fixed pool of pre-spawned workers for parallel tasks — avoiding per-task process spawn overhead.",
    "long": "A worker pool maintains N pre-spawned workers ready to accept tasks. Benefits: no per-task spawn overhead (50ms each), bounded resource usage, backpressure. PHP implementations: amphp/parallel for process pools, Laravel Horizon for queue workers.",
    "aliases": [
        "worker pool",
        "process pool",
        "parallel workers"
    ],
    "tags": [
        "performance",
        "php",
        "concurrency"
    ],
    "misconception": "Spawning a new process per task is equivalent to a pool — process spawning costs 10-100ms; pre-spawned workers handle tasks in <1ms overhead.",
    "why_it_matters": "1000 images with per-process spawn wastes 50+ seconds in overhead — a pre-spawned pool of 8 workers processes all in parallel efficiently.",
    "common_mistakes": [
        "Pool larger than CPU cores for CPU-bound work",
        "No backpressure",
        "Not restarting crashed workers",
        "Shared mutable state between workers"
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_concurrency_options",
        "php_roadrunner"
    ],
    "prerequisites": [
        "php_fpm",
        "php_queue_workers",
        "php_concurrency_options"
    ],
    "refs": [
        "https://amphp.org/parallel"
    ],
    "bad_code": "foreach ($images as $img) { $p = new Process(['php','resize.php',$img]); $p->start(); }",
    "good_code": "$pool = new DefaultPool(8);\n$promises = array_map(fn($img) => $pool->enqueue(new CallableTask('resize',[$img])), $images);\nawait Promise\\all($promises);",
    "quick_fix": "Size your PHP-FPM worker pool: pm.max_children = available_RAM / average_worker_memory; use pm = dynamic for variable traffic and pm = static for steady high-throughput loads",
    "severity": "high",
    "effort": "medium",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/worker_pool_php",
        "html_url": "https://codeclaritylab.com/glossary/worker_pool_php",
        "json_url": "https://codeclaritylab.com/glossary/worker_pool_php.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": "[Worker Pool Patterns](https://codeclaritylab.com/glossary/worker_pool_php) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/worker_pool_php"
            }
        }
    }
}