CodeBlanks Logo Icon CodeBlanks

Summary - Message queue

Combine producer, consumer and retry to write a background job system.

Summary - Message queue

In this group you learned to set up a message queue with Bull: creating a queue with new Bull("name"), writing a producer with queue.add, defining a consumer with queue.process, and setting retries with attempts/backoff.

Now you will combine these into a small email queue skeleton. The skeleton will create a queue, add a message, and define a consumer. This mini task shows how the queue tools combine.

Email queue design

The skeleton of an email queue is this: a queue with new Bull("email"), a message with queue.add, a consumer with queue.process. Retry and backoff are also added.

Flow: const kuyruk = new Bull("email"); kuyruk.add({ to: "alex@x.com" }, { attempts: 3, backoff: 1000 }); kuyruk.process(async (job) => { console.log("Sent: " + job.data.to); });. The message is added to the queue; the consumer takes it and sends the email. On failure it is retried 3 times. On top of this skeleton you can add priority, scheduling and a dead-letter queue (DLQ). Bull manages all of this with a simple API.

Practice Build the email queue skeleton

Import bull and create kuyruk = new Bull("email"). Define a consumer with kuyruk.process(async (job) => { console.log("Sent: " + job.data.to); }). Print "Queue system ready" with console.log.

Starter code

index.js
// Import bull, create queue and define consumer
// write here

// Print "Queue system ready"
// write here

Sample solution

index.js
const Bull = require("bull");
const kuyruk = new Bull("email");
kuyruk.process(async (job) => {
  console.log("Sent: " + job.data.to);
});
console.log("Queue system ready");

Expected output

Queue system ready

Questions and answers

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

1 What pieces does the queue skeleton combine?

index.js
new Bull + add + process
  • Only require
  • Only console.log
  • Only attempts
  • Bull, add, process and backoff Correct answer

Explanation: Bull + add + process + backoff combine.

2 Match each tool to its role

Term Meaning
new Bull Creates a queue
queue.add Adds a message
queue.process Defines a consumer
backoff Waits between retries

All lessons in Advanced