BullMQ Job Queues: Background Processing in Node.js Done Right
Your API endpoint sends an email, generates a PDF, and resizes an image. Response time: 8 seconds. Users rage-quit. Move heavy work to a background queue. Basic Queue Setup import { Queue, Worker }...

Source: DEV Community
Your API endpoint sends an email, generates a PDF, and resizes an image. Response time: 8 seconds. Users rage-quit. Move heavy work to a background queue. Basic Queue Setup import { Queue, Worker } from "bullmq"; import IORedis from "ioredis"; const connection = new IORedis({ maxRetriesPerRequest: null }); // Producer: add jobs to the queue const emailQueue = new Queue("email", { connection }); await emailQueue.add("welcome", { to: "[email protected]", subject: "Welcome\!", template: "welcome" }); // Consumer: process jobs const worker = new Worker("email", async (job) => { await sendEmail(job.data); }, { connection, concurrency: 5 }); worker.on("completed", (job) => console.log(job.id, "done")); worker.on("failed", (job, err) => console.error(job.id, err.message)); Job Options That Matter await emailQueue.add("password-reset", { to, token }, { priority: 1, // Lower number = higher priority attempts: 3, // Retry up to 3 times backoff: { type: "exponential", delay: 2000 }, remov