Engineering · 2026-12-04 · 3 min read · 2 views
A practical guide to implementing robust background processing in Node.js using BullMQ and Redis, covering retries, concurrency, and failure handling.

Background processing is a fundamental requirement for modern web applications. Offloading heavy computations, external API calls, or email delivery to asynchronous workers keeps HTTP response times fast and improves overall system resilience. In the Node.js ecosystem, BullMQ built on top of Redis has become a standard choice for managing background jobs.
In this article, we will examine how to set up a robust background job architecture using TypeScript, BullMQ, and Redis. We will cover queue creation, worker configuration, handling retries with exponential backoff, managing concurrency, and monitoring job lifecycles.
Before writing code, let us look at the high-level data flow between the producer application, Redis, and the background worker processes.
To begin, we need a reliable connection to Redis and a defined queue. We will use TypeScript to ensure type safety across job data structures.
First, install the required packages:
npm install bullmq ioredis
npm install -D typescript @types/nodeNext, let us define our queue and worker initialization logic.
import { Queue, Worker, Job } from 'bullmq';
const connection = {
host: 'localhost',
port: 6379
};
// Define the queue
export const emailQueue = new Queue('email-processing', { connection });
// Define the worker
const worker = new Worker(
'email-processing',
async (job: Job) => {
console.log(`Processing job ${job.id} for ${job.data.email}`);
// Simulate work
await new Promise((resolve) => setTimeout(resolve, 1000));
return { sent: true };
},
{ connection, concurrency: 5 }
);
worker.on('completed', (job) => {
console.log(`Job ${job.id} completed successfully.`);
});
worker.on('failed', (job, err) => {
console.log(`Job ${job?.id} failed with error: ${err.message}`);
});Failures are inevitable in distributed systems. Network partitions, rate limits, and database timeouts will occur. BullMQ allows us to define retry strategies declaratively when adding a job to the queue.
await emailQueue.add(
'send-welcome-email',
{ email: 'user@example.com', name: 'Jane Doe' },
{
attempts: 3,
backoff: {
type: 'exponential',
delay: 2000,
},
}
);With this configuration, if the worker throws an error, BullMQ will wait 2 seconds before the first retry, 4 seconds before the second, and so on.
Controlling how many jobs execute concurrently prevents your workers from overwhelming downstream services like third-party APIs or databases.
const restrictedWorker = new Worker(
'api-sync',
async (job) => {
// Sync data with external service
},
{
connection,
concurrency: 2, // Process max 2 jobs simultaneously per worker instance
limiter: {
max: 10,
duration: 1000, // Max 10 jobs per second
},
}
);When a job exhausts all its retry attempts, BullMQ moves it to the 'failed' state. In a production environment, you should monitor these failures and implement alerting or a mechanism to inspect and manually retry dead jobs.
worker.on('failed', async (job, err) => {
if (job) {
console.error(`Job ${job.id} failed permanently after ${job.attemptsMade} attempts.`);
// [add alert integration, e.g., Sentry or PagerDuty]
}
});Building resilient background job pipelines requires careful consideration of retries, concurrency, and error handling. By leveraging BullMQ and Redis, you get a robust foundation that scales horizontally as your application grows. Always monitor your queues in production to catch bottlenecks early.
Documentation: https://docs.bullmq.io/
No comments yet. Start the conversation.
Keep exploring