Back to results for “Caching with Redis”
Node.js › Background Jobs & Queues

How does BullMQ (built on Redis) implement a job queue in Node.js, and what are its core building blocks?

Intermediate

Answer

BullMQ uses Redis as a durable, shared broker between producers (application code adding jobs) and one or more worker processes (consuming and processing jobs) -- its core building blocks are the Queue (used to add jobs), the Worker (which processes jobs, potentially running in a completely separate process), and optionally a QueueEvents listener for observing job lifecycle events like completion or failure from elsewhere in the system.
const { Queue, Worker } = require('bullmq');
const connection = { host: 'localhost', port: 6379 };

const emailQueue = new Queue('emails', { connection });
await emailQueue.add('welcome-email', { userId: 123 });

// In a separate worker process
new Worker('emails', async (job) => {
  await sendWelcomeEmail(job.data.userId);
}, { connection });
Real-world example An e-commerce platform uses BullMQ to queue order-confirmation emails, with the web servers only responsible for calling queue.add() and separate dedicated worker processes actually sending the emails, so a slow email provider never affects the responsiveness of the main checkout API.

Common follow-ups: How do BullMQ's built-in retry and backoff options compare to implementing retry logic manually?;What happens to jobs already in the queue if the Redis instance backing BullMQ restarts?