Task Queues for TypeScript: Bull, Agenda, and Bee-Queue
In modern TypeScript web applications, there is often a need to perform resource-intensive or long-running operations asynchronously without blocking the main thread. Job Queues are a proven architectural pattern that allows you to defer task execution, distribute load among workers, and ensure processing reliability. In this article, we will look at three popular tools for TypeScript: Bull, Agenda, and Bee-Queue. You will learn how to integrate them into a project, what their strengths are, and which tool to choose for specific scenarios.
1. Bull — a powerful Redis-based queue
Bull is arguably the most popular library for managing task queues in Node.js/TypeScript. It uses Redis as a backend, ensuring high performance and fault tolerance. Bull supports task priorities, retries, delays, as well as complex scenarios with queue pausing and resuming.
Installation:
npm install bullnpm install @types/bull // for TypeScriptExample of a simple producer-consumer:
import Queue from 'bull';
// Create a queue named 'email'const emailQueue = new Queue('email', { redis: { host: 'localhost', port: 6379 }});
// Producer: add a jobasync function sendWelcomeEmail(userId: string) { await emailQueue.add( { userId, type: 'welcome' }, { attempts: 3, backoff: 2000 } // 3 attempts with 2s delay );}
// Consumer: process the jobemailQueue.process(async (job) => { const { userId, type } = job.data; console.log(`Sending ${type} email to user ${userId}`); // Email sending logic here});Bull also provides a convenient UI monitoring via bull-board or bull-repl, which simplifies debugging in production.
2. Agenda — a flexible queue with scheduling support
Agenda is a library that also uses MongoDB as its storage. Its main feature is built-in support for periodic tasks (cron-like schedules) and a convenient API for defining recurring jobs. Agenda is well-suited for tasks that need to run on a schedule: database cleanup, report generation, sending digests.
Installation:
npm install agendanpm install @types/agendaExample usage with scheduling:
import Agenda from 'agenda';
const agenda = new Agenda({ db: { address: 'mongodb://localhost:27017/agenda-example' }});
// Define a jobagenda.define('send weekly report', async (job) => { const { department } = job.attrs.data; console.log(`Generating weekly report for ${department} department`);});
// Start the agent and schedule the job(async () => { await agenda.start(); // Run every Friday at 18:00 await agenda.every('0 18 * * 5', 'send weekly report', { department: 'IT' });})();Agenda supports priorities, retries, and graceful shutdown. However, it is less performant than Bull due to using MongoDB instead of Redis.
3. Bee-Queue — a lightweight and fast queue
Bee-Queue is a minimalistic Redis-based queue focused on maximum performance and a small code footprint. It does not support complex features like priorities or schedules, but it is ideal for simple scenarios where speed and low memory consumption are important.
Installation:
npm install bee-queuenpm install @types/bee-queueExample usage:
import Bee from 'bee-queue';
const imageQueue = new Bee('image-processing', { redis: { host: 'localhost', port: 6379 }});
// ProducerimageQueue.createJob({