Introducing Dragonfly Cloud! Learn More

Question: What are some best practices for using BullMQ?


BullMQ is a powerful queueing system used in Node.js with a focus on performance, reliability, and extensibility. Here are several best practices to follow while working with BullMQ:

  1. Use separate processes for job processing: This ensures that the event loop is not blocked by your job processing logic, thereby ensuring better performance.
const Queue = require('bullmq').Queue; const myQueue = new Queue('my-queue'); myQueue.process(__dirname + '/jobProcessor.js');
  1. Handle job completion and failure: Always make sure to handle the completed and failed events for jobs. Not handling these can lead to unhandled promise rejections or silent failures.
job.on('completed', (result) => { console.log(`Job completed with result ${result}`); }); job.on('failed', (err) => { console.error(`Job failed: ${err.message}`); });
  1. Consider job priorities: If you have certain jobs that need to be processed urgently, you can use priorities in BullMQ. Higher priority jobs will be processed before lower priority ones.
await myQueue.add('urgent job', data, { priority: 1 }); await myQueue.add('normal job', data, { priority: 2 });
  1. Re-use connections when possible: Each instance of a queue or job uses a separate Redis connection. To minimize connection overhead, consider re-using connections when possible.

  2. Use rate-limited queues for throttling: If you want to limit how many jobs are processed per time unit, you can use rate-limited queues in BullMQ.

const limiter = { max: 100, duration: 5000, }; const rateLimitedQueue = new Queue('my-queue', { limiter });
  1. Handle stalled jobs: Jobs can become stalled if the worker processing them crashes. Make sure to handle this case - for example, by setting the stalledInterval option when creating your queues.

  2. Be cautious with repeatable jobs: Repeatable jobs in BullMQ are not idempotent, meaning multiple instances of the same job can be added to the queue. Be aware of this and design your job handling code accordingly.

Remember to always test thoroughly and monitor your queues' performance and error rates closely.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.