Introduction to BullMQ and Quick Start
🎯 Introduction
By the end of this article, you will be able to start Redis, install BullMQ in a Node.js project, and submit a job.
🧱 What Is BullMQ?
BullMQ is a library for managing background jobs in Node.js. An application submits jobs for BullMQ to store, and a Worker picks them up and processes them in the background. The producer and the processor can run separately.
BullMQ handles queuing, delays, job states, and retries. You still write the actual processing logic, such as handling files or sending email.
💡 Common Use Cases
| Requirement | Role of a background job |
|---|---|
| Generate large files | Return a job ID first so the user can check the result when generation finishes |
| Process uploaded images | Compress images or generate thumbnails after upload |
| Send notification emails | Send asynchronously after the main operation and retry temporary failures |
| Organize data periodically | Run cleanup or synchronization on a schedule |
If the user needs an immediate result, such as password validation, perform that work in the current operation. Do not report success while a background job has yet to make the decision.
🧱 How BullMQ Uses Redis
Redis is a separate service that stores job information and handles reads and writes. BullMQ is a Node.js library that uses Redis to store and schedule jobs. Installing BullMQ does not start Redis. Workers must also be able to connect to the Redis instance holding the jobs.
BullMQ stores job states and scheduling information in Redis, so this instance must not use a cache policy that evicts keys. Redis's noeviction policy rejects new writes when memory runs out, preventing cache eviction from silently deleting queue data.
🛠 Start Redis and Install BullMQ
The examples in this course use Node.js 22.9.0, BullMQ 5.61.2, and Redis 7.4.2. For Docker installation and container operations, see the Docker course. Start a dedicated practice instance:
docker run -d --name bullmq-redis \
-p 127.0.0.1:6380:6379 \
-v bullmq-redis-data:/data \
redis:7.4.2 redis-server --appendonly yes --maxmemory-policy noeviction
6380 is the port on your machine; Redis still uses 6379 inside the container. The volume stores Redis files, and appendonly yes enables AOF persistence. Job data serves a different purpose from a rebuildable cache. Use a dedicated instance or have your operations team explicitly configure isolation.
Install the specified version in your practice directory:
npm install --save-exact bullmq@5.61.2
🧰 Configure the BullMQ Connection
Create queue-config.mjs so subsequent Queue and Worker instances share the same connection address and queue name:
export const connection = {
host: process.env.REDIS_HOST ?? '127.0.0.1',
port: Number(process.env.REDIS_PORT ?? 6380),
};
export const queueName = 'course-tasks';
host and port identify the Redis service. queueName identifies a set of jobs, and the producer and Worker must use the same name. In a real deployment, supply the service address through environment variables instead of hardcoding environment-specific addresses.
Queue and Worker instances have different Redis retry requirements in BullMQ 5. The producer will explicitly use a finite retry limit, while the Worker needs to keep reconnecting. Each connection option is explained when first used.
🛠 Submit Your First Job
Create add-job.mjs to connect to the queue and add a job:
import { Queue } from 'bullmq';
import { connection, queueName } from './queue-config.mjs';
const queue = new Queue(queueName, {
connection: { ...connection, maxRetriesPerRequest: 1 },
});
queue.on('error', error => console.error('BullMQ connection error:', error.message));
try {
const job = await queue.add('write-text', { text: 'Hello BullMQ' });
console.log('Job submitted:', job.id);
} finally {
await queue.close();
}
The first argument to queue.add() is the job name, and the second is the data passed to the processor function. BullMQ generates a job ID that you can use for later queries. Closing the Queue releases this script's connection without deleting submitted jobs.
Run the script:
node add-job.mjs
The terminal prints the job ID. Because no Worker exists yet, the job waits to be processed. In the next article, you will write a separate Worker and observe it processing the job.
❓ Knowledge Check
Q1: Is BullMQ another name for Redis?
No. BullMQ is a Node.js library. Redis is a separate service that BullMQ uses to store queue data.
Q2: Does a job start running as soon as it is submitted?
Not necessarily. A Worker must connect to the same Redis instance and listen to the same queue before the job can be processed.
Q3: Can a development cache and BullMQ jobs share a Redis instance that evicts keys?
Avoid this configuration. Cache eviction can remove BullMQ job data. Use a dedicated instance or ensure that the relevant keys cannot be evicted.
🧪 Exercise
Change the job name and text value, submit another job, and check whether the job ID changes.
// Write your code here
🎉 You now have a working BullMQ and Redis environment!
