Node.js
What are message queues?
Learn the message queue concept.
What is a message queue?
A message queue is a broker that holds messages in order and delivers them to a consumer. The producer drops a message into the queue; the consumer takes it when ready and processes it. This decouples the producer from the consumer.
Example: during user registration, you drop the email-sending job into a queue. The user gets an immediate response; the email is sent in the background. Common tools: RabbitMQ, Redis, AWS SQS. In Node.js, Bull is a popular queue library built on Redis. Queues make long-running jobs (email, reports, file processing) asynchronous and keep the system's response time short. In this group you will learn to set up a queue with Bull, write producers and consumers, and apply retry/backoff patterns.
Producer and consumer
The producer adds a message to the queue. The consumer takes the message from the queue and processes it. These two roles work independently; one slowing down does not stop the other.
Example: an API endpoint (producer) adds an email job with queue.add({ task: "email" }). A separate worker process (consumer) takes the job with queue.process((job) => sendMail(job.data)) and sends the email. The producer does not wait for the consumer to finish; the queue holds the message. If the consumer crashes, the message stays in the queue; when the consumer restarts, it processes it. This durability is a key advantage of queues. Multiple consumers can process the queue in parallel.
Exercise Define a queue skeleton
Import the Bull module with require. Create a queue with new Bull("email-queue"). Print "Queue ready" with console.log.
Show example solution
const Bull = require("Bull");
const queue = new Bull("email-queue");
console.log("Queue ready");
Test yourself
-
What is a message queue for?
producer → queue → consumer
- Responds to HTTP requests
- Backs up the database
- Delivers messages in order to a consumer
- Draws the user interface
Show answer
Delivers messages in order to a consumer — A queue delivers messages in order to a consumer.
-
Complete the queue.add call
queue.____({ task: "email" });
Show answer
queue.add({ task: "email" });
-
Match each concept to its role
- producer
- Adds a message to the queue
- consumer
- Takes and processes a message
- queue
- Holds messages in order
- Bull
- Queue library on Redis
Next lesson: Bull