CodeBlanks Logo Icon CodeBlanks

Bull

Learn Redis-based queues with Bull.

The Bull library

Bull is a popular Node.js queue library built on Redis. It is fast, durable and offers advanced features like retry/backoff. Install it with npm install bull.

Example: const Bull = require("bull"); const queue = new Bull("email-queue");. Bull uses Redis under the hood and stores messages there. This means messages do not get lost even if Bull crashes. Bull also offers priority, delayed jobs, repeatable jobs and concurrency. In production Redis must run as a separate process. Bull's simple API makes it easy to manage complex queue scenarios.

Setting up Bull

To use Bull, Redis must be running first. Then you install Bull with npm install bull. In code you import it with require("bull") and create a queue with new Bull("name").

Example: const Bull = require("bull"); const emailQueue = new Bull("email");. The queue name determines the Redis key for that queue. Two queues with the same name actually share the same queue, so names should be unique. Bull connects to Redis at localhost:6379 by default; in production you provide a redis://... connection string. Once the queue is created you add messages with queue.add and define a consumer with queue.process.

Practice Create a Bull queue

Import the bull module. Create a queue with new Bull("job-queue") and assign it to kuyruk. Print "Bull ready" with console.log.

Starter code

index.js
// Import the bull module
// write here

// Create a new queue named "job-queue"
// write here

// Print "Bull ready"
// write here

Sample solution

index.js
const Bull = require("bull");
const kuyruk = new Bull("job-queue");
console.log("Bull ready");

Expected output

Bull ready

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match each piece to its role

Term Meaning
Bull Queue library
Redis Stores messages in the back
new Bull(name) Creates a new queue
npm install bull Installs the package

2 Which data store does Bull use?

index.js
const queue = new Bull("email")
  • MongoDB
  • Redis Correct answer
  • File system
  • PostgreSQL

Explanation: The right answer is: Redis. Bull stores messages in Redis.

3 Complete the Bull import line

Question
const Bull = require(____);
Answer
const Bull = require("bull");

Explanation: The Bull module is loaded as "bull".

All lessons in Advanced