Open PortfolioOpen Portfolio.
โ† Back to Blog

How to Implement Background Jobs with Bull

May 9, 2026at 2:01 PM UTCBy Pocket Portfolio TeamEngineering
How to Implement Background Jobs with Bull
#background jobs#Bull#Node.js#queues

Problem

In modern applications, handling time-intensive tasks synchronously can degrade performance and user experience. Tasks like email notifications, video processing, and data aggregation should run in the background to ensure a responsive application. This guide demonstrates how to implement background jobs using Bull, a robust queueing library for Node.js backed by Redis.

Solution with Code

Setup

First, ensure Redis is installed and running on your system. Then, install Bull and Redis client libraries in your Node.js application:

npm install bull redis

Creating a Queue

Create a Bull queue and define a job processor:

const Queue = require('bull');

// Create a new Bull queue
const myQueue = new Queue('myQueue');

// Define a job processor
myQueue.process(async (job) => {
  // This is where you handle the job data
  console.log(`Processing job with data: ${job.data}`);
  // Simulate a time-consuming task
  await doHeavyLifting(job.data);
});

// Function to simulate a heavy task
async function doHeavyLifting(data) {
  return new Promise((resolve) => setTimeout(resolve, 5000)); // 5-second delay
}

Adding Jobs to the Queue

Add jobs to the queue with the add method:

// Add a job to the queue
myQueue.add({ userId: 123, message: 'Welcome to our service!' });

Handling Job Completion

You can handle when a job is completed by listening to the completed event:

myQueue.on('completed', (job, result) => {
  console.log(`Job completed with result: ${result}`);
});

Running the Queue

Ensure your queue-processing script is running separately from your main application to handle jobs correctly. This separation allows scaling by running multiple workers.

Key Concepts

  • Queue: A queue is a data structure that follows the FIFO (First In, First Out) principle. In Bull, each queue is backed by Redis.

  • Job: A job is a unit of work that is processed by the queue. It includes the data to be processed and optional job options like priority or delay.

  • Processor: A processor is a function that handles the logic of processing a job. It contains the business logic for what should happen when a job is executed.

  • Concurrency: Bull allows you to specify the number of concurrent jobs a queue can process, enabling you to scale your job processing horizontally.

By using Bull, you can efficiently manage background jobs in your application, improving performance and user satisfaction.

How to Implement Background Jobs with Bull | Open Portfolio Blog | Open Portfolio