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: "user@example.com", 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 },
removeOnComplete: { count: 1000 }, // Keep last 1000 completed
removeOnFail: { age: 7 * 24 * 3600 }, // Keep failures for 7 days
});
Delayed and Recurring Jobs
// Delayed: run in 30 minutes
await emailQueue.add("reminder", data, { delay: 30 * 60 * 1000 });
// Recurring: every day at 9am
await emailQueue.upsertJobScheduler("daily-digest", {
pattern: "0 9 * * *",
}, { name: "digest", data: {} });
When to Use a Job Queue
Use queues for: email sending, image/video processing, PDF generation, webhooks, data imports, scheduled reports, notifications.
Do NOT queue: simple DB reads, cache lookups, anything the user needs immediately in the response.
Part of my Production Backend Patterns series. Follow for more practical backend engineering.
You Might Also Like
- Background Job Processing in Node.js: BullMQ, Queues, and Worker Patterns (2026)
- API Rate Limiting with Redis: Token Bucket, Sliding Window, and Per-Client Limits
- Graceful Shutdown in Node.js: Stop Dropping Requests (2026)
Follow me for more production-ready backend content!
If this helped you, buy me a coffee on Ko-fi!
Top comments (0)