Back-end

Queues using Bull in Nest.js

A Small Guide Handling Queues using Bull in Nest.js

Share:

Credit: M. Salman Agustian

Queue is an important technique in Node.js used for effectively handling asynchronous operations. These operations exist in different forms, including HTTP requests, read or write file operations, streams, and more.

Bull is a Node library that implements a fast and robust queue system based on redis. If you are new to queues you may wonder why they are needed after all.

"Queues can solve many different problems in an elegant way, from smoothing out processing peaks to creating robust communication channels between microservices or offloading heavy work from one server to many smaller workers, etc."

In its implementation, Bull has three (3) main cores concept including ProducersConsumers and Listeners.

  • Simple Producers is a code node js program where its function is to add a job to the Queue. One Producers can have one or more Producers.
  • Consumers is a process function where the main function is to run jobs that have been previously added to the Queue. The relationship between Consumers and Queues is almost the same as that of Producers in that the Queue can have one or more Consumers.
  • While Listeners is a process function that functions to provide actions/events on changes in Queue status (Progress, Completed, Failed, etc).

"The three main cores concept above make a job lifecycle that we can use to find out all the potential uses of Bull."

A Job Lifecycle

For full explanation, you can see directly here.

In the next session to understand more about using Queue, we will implement creating a Queue Send User Purchase Invoice using Bull in Nest Js.

.    .    .

Installation Bull in Nest Js

  • NestJS provides @nestjs/bull package as a wrapper on top of the Bull.
  • You need to install the Redis server before you get into the Bull. Because Bull is based on Redis.
  • Then run the command below to install Bull dependency in Nest Js.

npm install — save @nestjs/bull bull

npm install — save-dev @types/bull

Next… Is How To Config Bull in Nest Js ⚙️

Assume we already have a fresh Nest JS installation. Then create a config/queue folder inside the src folder. Next we create 3 files, namely: config.module.tsconfig.provider.ts, and config.ts. The folder structure we have now looks like this:

Case Study