Job Queues for JavaScript (Node.js): Bull, Bee-Queue, and Kue
In modern web applications built on Node.js, there is often a need to perform long-running or resource-intensive operations asynchronously without blocking the main request processing thread. Job queues are used for this purpose. They allow you to defer work, distribute load across multiple processes, and ensure retry processing in case of errors.
In this article, we will look at three popular libraries for working with job queues in Node.js: Bull, Bee-Queue, and Kue. All of them use Redis as a message broker, ensuring high performance and reliability.
1. Bull — The Most Popular and Feature-Rich Library
Bull is a modern, fast, and reliable job queue for Node.js built on Redis. It supports all key features: delayed jobs, retries, priorities, pause/resume, and monitoring via a built-in UI.
Installation
npm install bullBasic Example
const Queue = require('bull');
// Create a queueconst emailQueue = new Queue('email', 'redis://127.0.0.1:6379');
// Job processoremailQueue.process(async (job) => { const { to, subject } = job.data; console.log(`Sending email to ${to}: ${subject}`); // Simulate sending await new Promise(resolve => setTimeout(resolve, 1000)); return { success: true };});
// Adding a jobemailQueue.add({ to: 'user@example.com', subject: 'Hello from Bull!'}, { attempts: 3, // retry 3 times on error delay: 5000, // delay by 5 seconds priority: 1 // high priority});Key Bull Features
- Retries — automatic retry on failure with configurable delay.
- Priorities — high-priority jobs are processed first.
- Delayed Jobs — you can set a delay before execution.
- Pause and Resume — stop the queue for maintenance.
- Monitoring — built-in web interface
bull-board. - Events — subscribe to events (completed, failed, progress).
Advanced Example with Events
const Queue = require('bull');const myQueue = new Queue('myQueue');
myQueue.on('completed', (job, result) => { console.log(`Job ${job.id} completed:`, result);});
myQueue.on('failed', (job, err) => { console.error(`Job ${job.id} failed: ${err.message}`);});
myQueue.process(async (job) => { // Progress await job.progress(50); // Execution return { status: 'ok' };});Bull is recommended for projects that require maximum flexibility and ready-made monitoring tools.
2. Bee-Queue — A Lightweight and Fast Alternative
Bee-Queue is a minimalist job queue focused on performance. It is simpler than Bull but still provides all the basic features: delayed jobs, retries, priorities, and scaling.
Installation
npm install bee-queueUsage Example
const Queue = require('bee-queue');
const imageQueue = new Queue('images', { redis: { host: '127.0.0.1', port: 6379 }});
// ProcessorimageQueue.process(async (job) => { const { filename } = job.data; console.log(`Processing image: ${filename}`); // Simulate processing await new Promise(resolve => setTimeout(resolve, 2000)); return { processed: true };});
// Adding a jobimageQueue.createJob({ filename: 'photo.jpg'}) .retries(2) .del