Background Jobs in Node.js with Queues

Emails, exports, syncs and automation: moving slow work off the request path with a proper job queue.
If a request does something slow, like sending email, calling a third-party API or generating a report, the user should not wait for it. That is what job queues are for.
What goes in a queue
- Emails and notifications.
- Calls to slow or unreliable external APIs.
- File processing, exports and imports.
- Scheduled work such as nightly syncs and cleanups.
A simple setup
const emailQueue = new Queue("email", { connection });
// in the request handler
await emailQueue.add("welcome", { userId }, {
attempts: 5,
backoff: { type: "exponential", delay: 2000 },
});
// in a worker process
new Worker("email", async (job) => sendWelcomeEmail(job.data.userId), { connection });Make jobs idempotent
Jobs can run more than once after a crash or retry. Design them so running twice is safe, for example by checking whether the email was already sent.
Run workers separately
Workers should run as their own processes so heavy jobs never slow down API responses, and you can scale them independently.
Watch the queue
Track failed jobs and queue length. A growing queue is an early warning that something downstream is broken.









