{
    "slug": "async_processing",
    "term": "Async Processing / Job Queues",
    "category": "performance",
    "difficulty": "intermediate",
    "short": "Deferring time-consuming work (emails, image processing, third-party API calls) to background workers via a queue, improving response times.",
    "long": "Synchronous processing of slow operations (sending emails, resizing images, calling external APIs) directly in a web request increases response times and risks timeouts. Job queues (Laravel Queues, Symfony Messenger, RabbitMQ, Redis queues) accept a job description, store it durably, and process it asynchronously in a separate worker process. This reduces web response time to near-instant, improves resilience (failed jobs can be retried), and allows work to be distributed across multiple workers. The trade-off is eventual consistency and added infrastructure complexity.",
    "aliases": [
        "background jobs",
        "async tasks",
        "job queue processing"
    ],
    "tags": [
        "performance",
        "queues",
        "scalability",
        "devops"
    ],
    "misconception": "Async processing always means using a message queue like RabbitMQ. Simple background processing can be achieved with database-backed queues (Laravel Queues, Symfony Messenger) or even cron-driven workers — a full message broker is only warranted when throughput or reliability requirements exceed what a database queue can provide.",
    "why_it_matters": "Moving slow operations — email sending, PDF generation, third-party API calls — off the HTTP request/response cycle keeps response times fast regardless of the work being done. Without async processing, one slow job can block a request thread for seconds.",
    "common_mistakes": [
        "Dispatching jobs to a queue but not running any queue workers — jobs sit queued and never process.",
        "Putting too much serialised data in the job payload — pass IDs and re-fetch fresh data in the worker instead.",
        "Not handling job failures with retries and dead-letter queues — failed jobs silently disappear.",
        "Running queue workers without a process supervisor (Supervisor, Horizon) — workers die and no one notices."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "fibers",
        "memory_leak",
        "connection_pooling"
    ],
    "prerequisites": [
        "message_queue_patterns",
        "event_driven",
        "php_queue_workers"
    ],
    "refs": [
        "https://laravel.com/docs/queues",
        "https://symfony.com/doc/current/messenger.html"
    ],
    "bad_code": "// Synchronous — user waits for email + PDF + analytics:\nfunction placeOrder(Order $o): void {\n    $this->db->save($o);\n    $this->mailer->sendConfirmation($o);  // 800ms\n    $this->pdf->generateInvoice($o);      // 1200ms\n    $this->analytics->track($o);          // 300ms\n    // User blocked for 2.3s — all should be queued\n}",
    "good_code": "// Dispatch heavy work to a queue — return immediately to the user\nclass OrderController {\n    public function store(Request $req): JsonResponse {\n        $order = Order::create($req->validated());\n\n        // Don't process payment synchronously — queue it\n        ProcessPayment::dispatch($order)->onQueue('payments');\n        GenerateInvoice::dispatch($order)->onQueue('documents');\n        SendConfirmationEmail::dispatch($order)->delay(now()->addSeconds(5));\n\n        return response()->json(['id' => $order->id], 202); // Accepted\n    }\n}\n\n// Worker processes jobs in the background\n$ php artisan queue:work --queue=payments,documents,default",
    "quick_fix": "Move any operation taking >200ms out of the HTTP request into a queue job — email, PDF generation, image resizing, API calls to slow third parties",
    "severity": "high",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/async_processing",
        "html_url": "https://codeclaritylab.com/glossary/async_processing",
        "json_url": "https://codeclaritylab.com/glossary/async_processing.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": "[Async Processing / Job Queues](https://codeclaritylab.com/glossary/async_processing) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/async_processing"
            }
        }
    }
}