BullMQ is the modern successor of the popular Bull library. It’s built by the same team but offers better TypeScript support, performance, and compatibility with modern Redis versions.
⚙️ BullMQ is based on Redis and helps you manage background tasks, queues, and job processing efficiently in Node.js.
1. Install BullMQ and Redis
To get started, install the required packages:
npm install bullmq ioredis
Ensure Redis is running on your local system:
2. Setup Redis Connection
BullMQ uses ioredis for connecting with Redis. Let’s define a shared Redis connection file:
// redisClient.js
import { Redis } from "ioredis";
const connection = new Redis({
host: "127.0.0.1",
port: 6379,
});
export default connection;
3. Create Queue (emailQueue.js)
Create a BullMQ queue to add jobs. Each queue is connected to Redis through the shared connection.
// emailQueue.js
import { Queue } from "bullmq";
import connection from "./redisClient.js";
const emailQueue = new Queue("emailQueue", { connection });
export default emailQueue;
4. Add Jobs to Queue (addJob.js)
Jobs can be added to the queue from any part of your app — routes, controllers, or scripts.
// addJob.js
import emailQueue from "./emailQueue.js";
// Queue mein job add karna
await emailQueue.add("sendEmail", {
to: "user45@example.com",
subject: "Welcome to BullMQ!",
body: "This email is processed in the background.",
});
Each job is stored in Redis until a worker picks it up for processing.
5. Process Jobs (worker.js)
A Worker listens for new jobs in Redis and processes them asynchronously.
// worker.js
import { Worker } from "bullmq";
import connection from "./redisClient.js";
const worker = new Worker("emailQueue", async (job) => {
const { to, subject, body } = job.data;
console.log(`📨 Sending email to ${to}...`);
// Simulate delay
await new Promise((resolve) => setTimeout(resolve, 2000));
console.log(`✅ Email sent successfully to ${to}`);
}, { connection });
worker.on("completed", (job) => {
console.log(`🎉 Job ${job.id} completed`);
});
worker.on("failed", (job, err) => {
console.error(`❌ Job ${job.id} failed:`, err.message);
});
🧠 Tip: You can run multiple workers for the same queue — BullMQ automatically balances jobs among them.
6. Running the Queue
Run the following scripts in separate terminals:
node addJob.js
node worker.js
The first script adds jobs, while the second processes them from Redis in real-time.
7. Add a Queue Scheduler (Optional)
BullMQ also provides a QueueScheduler to handle delayed or retried jobs:
// scheduler.js
import { QueueScheduler } from "bullmq";
import connection from "./redisClient.js";
new QueueScheduler("emailQueue", { connection });
console.log("🕒 Scheduler is running...");
The scheduler ensures delayed jobs and retries are managed correctly.
8. Monitor Jobs (BullMQ Dashboard)
For job visualization, you can use Arena or Bull Board:
npm install @bull-board/express
Add the dashboard route in your app:
import express from "express";
import { router, setQueues } from "@bull-board/express";
import { BullMQAdapter } from "@bull-board/api/bullMQAdapter.js";
import emailQueue from "./emailQueue.js";
const app = express();
setQueues([new BullMQAdapter(emailQueue)]);
app.use("/admin/queues", router);
app.listen(3000, () =>
console.log("Dashboard running on http://localhost:3000/admin/queues")
);
9. Common Use Cases
- 📧 Sending transactional emails
- 📱 Push notifications
- 📊 Report or PDF generation
- 📦 Data processing in background
- 🕒 Scheduled or delayed jobs
🚀 BullMQ is the most reliable and modern way to manage background jobs in Node.js. It’s fast, scalable, and integrates perfectly with Redis for high-performance task processing.