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?
IntermediateAnswer
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.