{
    "slug": "queue_worker_tuning",
    "term": "Queue Worker Tuning",
    "category": "performance",
    "difficulty": "intermediate",
    "short": "Configuring PHP queue workers (Laravel Horizon, Supervisor) for throughput, memory limits, graceful restarts, and concurrency — preventing job failures and memory leaks.",
    "long": "PHP queue workers are long-running processes — unlike PHP-FPM requests they do not restart after each job. Memory leaks accumulate over time. Key configuration: --max-jobs (restart after N jobs, prevents memory leak), --max-time (restart after N seconds), --memory (restart if memory exceeds limit), --timeout (kill hung jobs), --tries (retry failed jobs), --backoff (delay between retries). Supervisor keeps workers alive and respawns crashed workers. Horizon provides per-queue concurrency, job metrics, and real-time dashboard for Laravel.",
    "aliases": [
        "Horizon",
        "Supervisor",
        "queue workers",
        "job processing"
    ],
    "tags": [
        "performance",
        "php",
        "queue",
        "devops"
    ],
    "misconception": "Queue workers run forever without issues — PHP workers accumulate memory from object graphs, event listeners, and ORM identity maps; memory limits and max-jobs restarts are not optional.",
    "why_it_matters": "A queue worker with a memory leak that is never restarted will exhaust server memory overnight, causing all jobs to fail — max-jobs and memory limits are essential production configuration.",
    "common_mistakes": [
        "No --max-jobs limit — worker runs until it exhausts memory, then crashes and all queued jobs time out.",
        "Timeout shorter than the longest expected job — jobs are killed mid-execution, causing partial processing.",
        "Not using --sleep for empty queues — workers spin at 100% CPU when the queue is empty without a sleep delay.",
        "One worker process for all queues — high-priority queues blocked behind slow bulk jobs."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "async_processing",
        "message_queue_patterns",
        "php_fpm",
        "memory_leak"
    ],
    "prerequisites": [
        "php_queue_workers",
        "message_queue_patterns",
        "php_fpm"
    ],
    "refs": [
        "https://laravel.com/docs/queues#supervisor-configuration"
    ],
    "bad_code": "# Supervisor config — no memory limit, no max-jobs:\n[program:worker]\ncommand=php artisan queue:work\nautostart=true\nautorestart=true\n# Worker runs indefinitely\n# Memory grows from 50MB to 2GB over 3 days\n# Server OOM kills worker\n# All jobs fail until Supervisor restarts it",
    "good_code": "# Supervisor — with proper limits:\n[program:worker-default]\ncommand=php artisan queue:work --queue=default,low\n  --max-jobs=1000 --max-time=3600\n  --memory=256 --timeout=90\n  --sleep=3 --tries=3 --backoff=10\nprocess_num=4        ; 4 concurrent workers\nautostart=true\nautorestart=true\nstopwaitsecs=120     ; Allow jobs to finish gracefully\n\n[program:worker-critical]\ncommand=php artisan queue:work --queue=critical\n  --max-jobs=500 --memory=128 --timeout=30\nprocess_num=2",
    "quick_fix": "Set --timeout slightly less than visibility timeout, --max-jobs=1000 to prevent memory leaks, and --sleep=3 to avoid CPU spin on empty queue — monitor queue depth and auto-scale workers when it grows",
    "severity": "high",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/queue_worker_tuning",
        "html_url": "https://codeclaritylab.com/glossary/queue_worker_tuning",
        "json_url": "https://codeclaritylab.com/glossary/queue_worker_tuning.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": "[Queue Worker Tuning](https://codeclaritylab.com/glossary/queue_worker_tuning) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/queue_worker_tuning"
            }
        }
    }
}