Redis queues are a powerful way to handle background jobs, scheduled tasks, or heavy processes without blocking your main Node.js thread. In this guide, we’ll use the Bull package — a popular Node.js queue library built on top of Redis.
⚙️ Bull makes it easy to create robust job queues, retry failed jobs, and monitor progress — all backed by Redis.
1. Install Bull and Redis
First, install Bull and Redis client in your Node.js project:
Make sure Redis is running locally on port 6379. You can start it using:
2. Define Queue (emailQueue.js)
Create a queue file that connects to Redis and defines a new Bull queue for email jobs:
import Queue from "bull";
// Redis connection
const emailQueue = new Queue("emailQueue", {
redis: { host: "127.0.0.1", port: 6379 },
});
export default emailQueue;
3. Add Jobs to Queue (addJob.js)
You can add jobs to the queue from anywhere — API routes, services, or scripts. Here’s how to push an email task to the Redis queue:
import emailQueue from "./emailQueue.js";
// Queue mein job add karna
emailQueue.add({
to: "user23@example.com",
subject: "Welcome!",
body: "Thanks for joining our app!",
});
Each job is stored in Redis until a worker picks it up for processing.
4. Queue Worker (worker.js)
The worker is responsible for processing jobs from the Redis queue. It listens to new jobs, performs the task, and marks them as complete or failed.
import emailQueue from "./emailQueue.js";
// Job process karna
emailQueue.process(async (job) => {
const { to, subject, body } = job.data;
console.log(`Sending email to ${to}...`);
// Yahan actual email send karne ka code likho (e.g. nodemailer)
await new Promise((resolve) => setTimeout(resolve, 2000)); // fake delay
console.log(`✅ Email sent to ${to}`);
});
🧠 Tip: You can create multiple workers or scale them horizontally — Bull handles concurrency automatically.
5. Running the Queue
You can now run your queue system using two separate Node processes:
node addJob.js
node worker.js
The first script adds jobs to the queue, and the second script processes them.
6. Real-World Use Cases
- 📧 Sending emails asynchronously
- 📊 Generating reports or invoices
- 📦 Processing file uploads
- 📱 Sending push notifications
- ⚡ Background data imports
7. Monitor Jobs (Optional)
You can install Bull Board — a dashboard to monitor your queue in real time:
npm install @bull-board/express
Then create a simple dashboard endpoint:
import express from "express";
import { router, setQueues } from "@bull-board/express";
import emailQueue from "./emailQueue.js";
const app = express();
setQueues([emailQueue]);
app.use("/admin/queues", router);
app.listen(3000, () => console.log("Dashboard: http://localhost:3000/admin/queues"));
With Bull and Redis, you can easily build scalable background job systems for any Node.js app. It’s fast, reliable, and perfect for microservices or heavy async workloads.