Background jobs & queues

Moving email/exports/webhooks out of the request path with BullMQ or pg-based queues; retries, dead-letter handling, at-least-once semantics.

About 27 min read3-question knowledge check

The mental model

A restaurant does not make the waiter cook. The waiter takes your order, clips a ticket to the kitchen rail, and returns to the floor in seconds; the kitchen works the rail at its own pace, in its own order, retrying a burnt dish without the customer ever standing at the pass. A job queue is the ticket rail: the producer (your request handler) writes down what should happen and immediately returns; a worker (a separate process) picks tickets up and does the slow, failure-prone work — sending email, generating a PDF export, calling a partner's flaky webhook, resizing images.

What breaks without it: everything slow or unreliable happens inside the HTTP request. Checkout waits 4 seconds on the email provider's API; a webhook receiver times out because it synchronously re-delivers to three downstream systems; a report export ties up a serverless function until the platform kills it at the timeout. Worse, failure handling becomes the user's problem — if the email API is down, does checkout fail? A queue decouples accepting work from doing work, which buys you: fast responses, retries with backoff, rate control toward fragile dependencies, and a buffer that absorbs traffic spikes.

The one idea that explains the whole topic: queues give you at-least-once delivery, and everything else follows from that. A worker can crash after doing the work but before acknowledging it, so the job runs again. You cannot get exactly-once *delivery* from any real queue; you get exactly-once *effect* by making handlers idempotent. Every design decision — idempotency keys, dedupe tables, retry policies, dead-letter queues — is downstream of this single fact.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision