What are message queues?
- Node.js course
- Advanced
- Lesson 55/83
- 5 min
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.
Practice Define a queue skeleton
Import the Bull module with require. Create a queue with new Bull("email-queue"). Print "Queue ready" with console.log.
Starter code
// Import the Bull module
// write here
// Create a new queue named "email-queue"
// write here
// Print "Queue ready"
// write hereSample solution
const Bull = require("Bull");
const queue = new Bull("email-queue");
console.log("Queue ready");Expected output
Queue ready
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is a message queue for?
producer → queue → consumer- Responds to HTTP requests
- Backs up the database
- Delivers messages in order to a consumer Correct answer
- Draws the user interface
Explanation: A queue delivers messages in order to a consumer.
2 Complete the queue.add call
queue.____({ task: "email" });
queue.add({ task: "email" });
Explanation: queue.add adds a message.
3 Match each concept to its role
| Term | Meaning |
|---|---|
producer |
Adds a message to the queue |
consumer |
Takes and processes a message |
queue |
Holds messages in order |
Bull |
Queue library on Redis |