これらのライブラリは、Node.js アプリケーションでメール送信、画像処理、データ集計などの重いタスクをバックグラウンドで非同期に実行するためのツールです。メインのリクエスト処理をブロックせずに済むため、ユーザー体験の向上やシステム全体の安定性に貢献します。Redis や MongoDB などの外部ストレージを利用してジョブを管理し、失敗時のリトライやスケジュール実行といった機能を提供します。
Node.js でバックグラウンドタスクを処理する際、どのライブラリを選ぶかはシステムの信頼性に直結します。agenda、bee-queue、bree、bull、kue、node-resque はすべてジョブキューを管理しますが、内部の仕組みや適した場面が異なります。実務で直面する課題に基づき、主要な違いを解説します。
ジョブの状態をどこに保存するかは、インフラ構成に大きな影響を与えます。
agenda は MongoDB を使用します。
// agenda: MongoDB 接続設定
const agenda = new Agenda({ db: { address: 'mongodb://localhost/agenda' } });
await agenda.start();
bee-queue、bull、kue、node-resque は Redis を使用します。
// bull: Redis 接続設定
const Queue = require('bull');
const queue = new Queue('my-queue', 'redis://127.0.0.1:6379');
bree はファイルシステムとプロセス管理を重視します。
// bree: 設定ファイルベース
const Bree = require('bree');
const bree = new Bree({
root: './jobs',
jobs: [{ name: 'daily-report', interval: 'daily' }]
});
ジョブをどのように登録し、実行するかは開発体験に直結します。
agenda は名前付きジョブを定義します。
// agenda: ジョブ定義と実行
agenda.define('send email', async (job) => {
console.log(`Sending to ${job.data.to}`);
});
await agenda.schedule('in 5 minutes', 'send email', { to: 'user@example.com' });
bee-queue はキューインスタンスに対して処理を登録します。
// bee-queue: 処理登録
queue.process(async (job) => {
console.log(`Processing job ${job.id}`);
return job.data;
});
const job = queue.createJob({ id: 123 });
await job.save();
bull もキューベースの処理登録を行います。
queue.process で処理を定義し、queue.add でジョブを投入します。// bull: 処理登録と追加
queue.process(async (job) => {
return doSomething(job.data);
});
await queue.add({ foo: 'bar' });
bree は独立したファイルとしてジョブを記述します。
./jobs/daily-report.js のようなファイルを作成し、そこでロジックを書きます。// bree: ワーカースクリプト (jobs/daily-report.js)
module.exports = async () => {
console.log('Running daily report');
};
kue はチェーンメソッドでジョブを作成します。
// kue: ジョブ作成
const job = queue.create('email', { to: 'user' });
job.save((err) => {
if (!err) console.log('Job saved');
});
node-resque はクラスベースでキューとワーカーを管理します。
Queue クラスで enqueue し、Worker クラスで処理します。// node-resque: キューとワーカー
const { Worker, Queue } = require('node-resque');
const queue = new Queue({ connection: { host: 'localhost' } });
await queue.connect();
await queue.enqueue('queueName', 'jobName', ['arg1']);
特定の時刻にジョブを実行したい場合、ライブラリの対応状況が重要です。
agenda はスケジュール機能が強力です。
schedule や every メソッドで、人間が読める形式の時間を指定できます。// agenda: スケジュール実行
await agenda.every('10 minutes', 'calculate stats');
await agenda.schedule('tomorrow at noon', 'send report');
bree は cron 構文や間隔指定をサポートします。
interval や cron を書くだけで済みます。// bree: 間隔指定
const bree = new Bree({
jobs: [
{ name: 'cleanup', interval: '5m' },
{ name: 'backup', cron: '0 0 * * *' }
]
});
bull はジョブ追加時にオプションで指定します。
repeat オプションを使って、ジョブの繰り返しを設定します。// bull: 繰り返しジョブ
await queue.add('send notification', {}, {
repeat: { cron: '*/5 * * * *' }
});
bee-queue、kue、node-resque は基本的なキュー機能に留まります。
// node-resque: 基本的な enqueue のみ
// スケジュール機能は本体にないため、外部で管理が必要
await queue.enqueue('high', 'sendEmail', ['user@example.com']);
ライブラリの保守状況は、長期的なプロジェクトの安定性を決めます。
kue は公式に非推奨です。
// kue: 使用を避けるべき
// const kue = require('kue'); // 非推奨
bull はメンテナンスモードに入っています。
bullmq で開発されています。bullmq の検討を推奨しますが、bull もまだ広く使われています。// bull: 後継に注意
// const Queue = require('bull'); // _legacy_
// const { Queue } = require('bullmq'); // _modern_
agenda、bee-queue、bree、node-resque は継続してメンテナンスされています。
// bree: 活発なメンテナンス
const Bree = require('bree'); // 最新版が利用可能
ジョブが失敗したときにどう振る舞うかは、システムの信頼性に影響します。
agenda は再試行ロジックを内蔵しています。
// agenda: 再試行設定
agenda.define('task', { priority: 'high', concurrency: 10 }, async (job) => {
// 失敗時は自動的にリトライ設定に従う
});
bull は詳細なアトンプト設定を持っています。
attempts オプションで再試行回数を指定できます。// bull: アトンプト設定
queue.add('video process', { id: 1 }, {
attempts: 3,
backoff: { type: 'fixed', delay: 2000 }
});
bee-queue はシンプルにエラーを返します。
// bee-queue: エラー処理
queue.process(async (job) => {
if (!job.data.valid) throw new Error('Invalid data');
});
bree はワーカープロセスの再起動で対応します。
// bree: ワーカー管理
bree.on('worker error', (name, err) => {
console.error(`Worker ${name} failed`, err);
// 必要に応じて手動で再起動トリガー
});
agenda は MongoDB を使っているチームに最適です — データベース統合がスムーズで、スケジュール機能が豊富です。
bull は Redis 環境で高機能なキューが必要な場合に適していますが、bullmq への移行を視野に入れるべきです — 機能は豊富ですが、レガシーになりつつあります。
bree は cron ジョブや定期タスクに特化しています — 別プロセス実行による安定性が魅力で、設定も簡単です。
bee-queue はシンプルさを求める場合に選びます — Redis を使いつつ、設定を最小限に抑えたいプロジェクトに向いています。
kue は使用してはいけません — 非推奨であり、セキュリティリスクがあります。
node-resque は Resque エコシステムとの互換性が必要な場合に意味があります — 特定の環境以外では、他のモダンな選択肢を検討すべきです。
最終的には、既存のインフラ(Redis か MongoDB か)と、求められる機能(単純なキューか、複雑なスケジュールか)で決定します。
MongoDB を既に使用しているプロジェクトで、ジョブの永続性と柔軟なスケジュール管理を重視する場合に選びます。データベースにジョブ状態を保存するため、Redis の導入が不要な点がメリットです。複雑な再試行ロジックや頻繁なスケジューリングが必要なシステムに適しています。
Redis を使ったシンプルで軽量なキュー構成が必要な場合に適しています。設定項目が少なく、コードが直感的であるため、小規模から中規模のタスク処理に最適です。高度な機能よりも、導入の手軽さと動作の確実性を優先するプロジェクトで選ばれます。
cron ジョブや定期的なタスク実行をメインの目的とする場合に選びます。他のキューライブラリとは異なり、ワーカーを別プロセスとして実行するため、メモリリークのリスクを隔離できます。Node.js スクリプトを直接ジョブとして登録できるため、開発者の負担が少ないのが特徴です。
Redis ベースで堅牢な機能セットが必要な場合に適していますが、後継の bullmq への移行も検討すべきです。ジョブの優先順位やイベント処理など、エンタープライズ向けの機能が充実しています。大規模な分散処理システムで実績が多いライブラリです。
新規プロジェクトでは使用しないでください。このライブラリは公式に非推奨(Deprecated)となっており、メンテナンスが停止しています。セキュリティリスクやバグ修正が受けられないため、既存システムからの移行を強く推奨します。
Ruby の Resque システムと互換性が必要な場合や、Resque プロトコルに基づく既存インフラと連携する場合に選びます。Redis をバックエンドに使用し、シンプルな FIFO キュー構成を提供します。特定のレガシーシステムとの統合が必要なシーンで価値を発揮します。
A light-weight job scheduling library for Node.js
Migrating from v5? See the Migration Guide for all breaking changes.
Agenda 6.x is a complete TypeScript rewrite with a focus on modularity and flexibility:
Pluggable storage backends - Choose from MongoDB, PostgreSQL, Redis, or implement your own. Each backend is a separate package - install only what you need.
Pluggable notification channels - Move beyond polling with real-time job notifications via Redis, PostgreSQL LISTEN/NOTIFY, or other pub/sub systems. Jobs get processed immediately when saved, not on the next poll cycle.
Modern stack - ESM-only, Node.js 18+, full TypeScript with strict typing.
See the 6.x Roadmap for details and progress.
Install the core package and your preferred backend:
# For MongoDB
npm install agenda @agendajs/mongo-backend
# For PostgreSQL
npm install agenda @agendajs/postgres-backend
# For Redis
npm install agenda @agendajs/redis-backend
Requirements:
import { Agenda } from 'agenda';
import { MongoBackend } from '@agendajs/mongo-backend';
const agenda = new Agenda({
backend: new MongoBackend({ address: 'mongodb://localhost/agenda' })
});
// Define a job
agenda.define('send email', async (job) => {
const { to, subject } = job.attrs.data;
await sendEmail(to, subject);
});
// Start processing
await agenda.start();
// Schedule jobs
await agenda.every('1 hour', 'send email', { to: 'user@example.com', subject: 'Hello' });
await agenda.schedule('in 5 minutes', 'send email', { to: 'admin@example.com', subject: 'Report' });
await agenda.now('send email', { to: 'support@example.com', subject: 'Urgent' });
| Package | Backend | Notifications | Install |
|---|---|---|---|
@agendajs/mongo-backend | MongoDB | Polling only | npm install @agendajs/mongo-backend |
@agendajs/postgres-backend | PostgreSQL | LISTEN/NOTIFY | npm install @agendajs/postgres-backend |
@agendajs/redis-backend | Redis | Pub/Sub | npm install @agendajs/redis-backend |
| Backend | Storage | Notifications | Notes |
|---|---|---|---|
MongoDB (MongoBackend) | ✅ | ❌ | Storage only. Combine with external notification channel for real-time. |
PostgreSQL (PostgresBackend) | ✅ | ✅ | Full backend. Uses LISTEN/NOTIFY for notifications. |
Redis (RedisBackend) | ✅ | ✅ | Full backend. Uses Pub/Sub for notifications. |
| InMemoryNotificationChannel | ❌ | ✅ | Notifications only. For single-process/testing. |
import { Agenda } from 'agenda';
import { MongoBackend } from '@agendajs/mongo-backend';
// Via connection string
const agenda = new Agenda({
backend: new MongoBackend({ address: 'mongodb://localhost/agenda' })
});
// Via existing MongoDB connection
const agenda = new Agenda({
backend: new MongoBackend({ mongo: existingDb })
});
// With options
const agenda = new Agenda({
backend: new MongoBackend({
mongo: db,
collection: 'jobs' // Collection name (default: 'agendaJobs')
}),
processEvery: '30 seconds', // Job polling interval
maxConcurrency: 20, // Max concurrent jobs
defaultConcurrency: 5 // Default per job type
});
import { Agenda } from 'agenda';
import { PostgresBackend } from '@agendajs/postgres-backend';
const agenda = new Agenda({
backend: new PostgresBackend({
connectionString: 'postgresql://user:pass@localhost:5432/mydb'
})
});
import { Agenda } from 'agenda';
import { RedisBackend } from '@agendajs/redis-backend';
const agenda = new Agenda({
backend: new RedisBackend({
connectionString: 'redis://localhost:6379'
})
});
For faster job processing across distributed systems:
import { Agenda, InMemoryNotificationChannel } from 'agenda';
import { MongoBackend } from '@agendajs/mongo-backend';
const agenda = new Agenda({
backend: new MongoBackend({ mongo: db }),
notificationChannel: new InMemoryNotificationChannel()
});
You can use MongoDB for storage while using a different system for real-time notifications:
import { Agenda } from 'agenda';
import { MongoBackend } from '@agendajs/mongo-backend';
import { RedisBackend } from '@agendajs/redis-backend';
// MongoDB for storage + Redis for real-time notifications
const redisBackend = new RedisBackend({ connectionString: 'redis://localhost:6379' });
const agenda = new Agenda({
backend: new MongoBackend({ mongo: db }),
notificationChannel: redisBackend.notificationChannel
});
This is useful when you want MongoDB's proven durability and flexible queries for job storage, but need faster real-time notifications across multiple processes.
// Simple async handler
agenda.define('my-job', async (job) => {
console.log('Processing:', job.attrs.data);
});
// With options
agenda.define('my-job', async (job) => { /* ... */ }, {
concurrency: 10,
lockLimit: 5,
lockLifetime: 10 * 60 * 1000, // 10 minutes
priority: 'high'
});
For a class-based approach, use TypeScript decorators:
import { JobsController, Define, Every, registerJobs, Job } from 'agenda';
@JobsController({ namespace: 'email' })
class EmailJobs {
@Define({ concurrency: 5 })
async sendWelcome(job: Job<{ userId: string }>) {
console.log('Sending welcome to:', job.attrs.data.userId);
}
@Every('1 hour')
async cleanupBounced(job: Job) {
console.log('Cleaning up bounced emails');
}
}
registerJobs(agenda, [new EmailJobs()]);
await agenda.start();
// Schedule using namespaced name
await agenda.now('email.sendWelcome', { userId: '123' });
See Decorators Documentation for full details.
// Run immediately
await agenda.now('my-job', { userId: '123' });
// Run at specific time
await agenda.schedule('tomorrow at noon', 'my-job', data);
await agenda.schedule(new Date('2024-12-25'), 'my-job', data);
// Run repeatedly
await agenda.every('5 minutes', 'my-job');
await agenda.every('0 * * * *', 'my-job'); // Cron syntax
// Cancel jobs matching a filter (removes from database)
await agenda.cancel({ name: 'my-job' });
await agenda.cancel({ name: 'my-job', data: { userId: 123 } });
// Cancel ALL jobs unconditionally
await agenda.cancelAll();
// Disable/enable jobs globally (by query)
await agenda.disable({ name: 'my-job' }); // Disable all jobs matching query
await agenda.enable({ name: 'my-job' }); // Enable all jobs matching query
// Disable/enable individual jobs
const job = await agenda.create('my-job', data);
job.disable();
await job.save();
// Progress tracking
agenda.define('long-job', async (job) => {
for (let i = 0; i <= 100; i += 10) {
await doWork();
await job.touch(i); // Report progress 0-100
}
});
// Stop immediately - unlocks running jobs so other workers can pick them up
await agenda.stop();
// Drain - waits for running jobs to complete before stopping
await agenda.drain();
// Drain with timeout (30 seconds) - for cloud platforms with shutdown deadlines
const result = await agenda.drain(30000);
if (result.timedOut) {
console.log(`${result.running} jobs still running after timeout`);
}
// Drain with AbortSignal - for external control
const controller = new AbortController();
setTimeout(() => controller.abort(), 30000);
await agenda.drain({ signal: controller.signal });
Use drain() for graceful shutdowns where you want in-progress jobs to finish their work.
agenda.on('start', (job) => console.log('Job started:', job.attrs.name));
agenda.on('complete', (job) => console.log('Job completed:', job.attrs.name));
agenda.on('success', (job) => console.log('Job succeeded:', job.attrs.name));
agenda.on('fail', (err, job) => console.log('Job failed:', job.attrs.name, err));
// Job-specific events
agenda.on('start:send email', (job) => { /* ... */ });
agenda.on('fail:send email', (err, job) => { /* ... */ });
Use fail listeners to capture richer error context, such as stack traces,
without storing large payloads in job.attrs.failReason:
agenda.on('fail', async (err, job) => {
await saveJobError({
jobId: job.attrs._id,
jobName: job.attrs.name,
message: err.message,
stack: err.stack
});
});
For databases other than MongoDB, PostgreSQL, or Redis, implement AgendaBackend:
import { AgendaBackend, JobRepository } from 'agenda';
class SQLiteBackend implements AgendaBackend {
readonly repository: JobRepository;
readonly notificationChannel = undefined; // Or implement NotificationChannel
async connect() { /* ... */ }
async disconnect() { /* ... */ }
}
const agenda = new Agenda({
backend: new SQLiteBackend({ path: './jobs.db' })
});
See Custom Backend Driver for details.
Official Backend Packages:
Tools:
MIT