Message Queue
A line jobs wait in so producers can hand off work instantly and workers chew through it at their own pace.
See it
What it is
A message queue is a line that work waits in. One side (the producer) drops a message and moves on immediately; the other side (a worker, or twenty workers) pulls messages off and does the slow part later. The queue absorbs bursts, survives worker restarts, and lets you scale the consuming side without touching the producing side. Common ones: SQS, RabbitMQ, Redis-backed BullMQ, and Kafka (technically a log, used the same way).
Reach for it whenever the response should not wait on the work: sending email, generating PDFs, resizing uploads, syncing to a CRM, calling a flaky third-party API. A queue also hands you the primitives for retries and backpressure: acknowledgements, visibility timeouts, dead letter routing, bounded consumer concurrency. You still have to configure them (how many attempts, how long a message stays invisible while a worker holds it, where the give-ups land, how many workers run at once), but once you have, a downstream service being down becomes a slow backlog instead of a pile of 500s in the user's face.
Gotcha: almost every queue is at-least-once, meaning a message can be delivered twice when a worker dies mid-job or an ack gets lost. Write consumers that can safely run the same message twice, and give permanently failing messages somewhere to go (a dead letter queue) or one poison message will spin forever and starve the rest of the line.
Ask AI for it
Move this slow operation out of the request path and behind a message queue. On the request side, validate the input, enqueue a job with a stable job ID plus everything the worker needs, and return immediately. Add a separate worker process that consumes the queue with bounded concurrency, makes the handler idempotent by checking whether that job ID was already processed, retries transient failures with exponential backoff, and routes messages that fail after the max attempts to a dead letter queue with the original payload and the last error attached.