BullMQ is a Node.js library that allows you to handle jobs and messages in various queues. It uses Redis as a communication channel between different services and provides mechanisms for working with jobs, such as prioritization, scheduling, retries, and more.
Here's a high-level overview of how BullMQ works:
add
method on a queue. Each job is an atomic unit of work, and it can contain any JSON-serializable data. When a job is created, it's added to a Redis list representing the waiting queue.const queue = new Queue('my-queue'); queue.add('my-job', { foo: 'bar' });
queue.process(async (job) => { // Process the job. });
Job Lifecycle: Jobs go through several states during their lifecycle, including waiting
, active
, completed
, failed
, etc. These states are represented as separate lists in Redis.
Retries and Delays: BullMQ has built-in support for delayed jobs and job retries. If a job fails, it can be automatically retried after a specified delay.
Events: BullMQ emits events related to job lifecycle changes, such as completed
, failed
, stalled
, etc. You can listen to these events to get real-time notifications about job status changes.
Priority and Scheduling: BullMQ supports priority-based scheduling, where jobs with a higher priority will be processed before jobs with a lower priority. It also supports cron-based job scheduling.
BullMQ is built on top of Redis, which provides fast and reliable message passing. The queue operations are atomic thanks to Redis's multi/exec feature.
Please note that while BullMQ is powerful, it does require careful management of resources due to its reliance on Redis, and jobs can be lost if not properly managed.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.