Message Queues: Let Services Hand Off Work and Keep Running
A message queue holds work between a sender and a receiver, so each can run at its own speed and survive the other's failures.
In short
A message queue holds work between a sender and a receiver, so each can run at its own speed and survive the other's failures.
11 min readFree
What You Will Learn
Many software systems are made of parts that send work to each other. Sometimes the receiving part is busy, slow, or down for a few minutes. A message queue fixes this by standing between the two parts as a holding area. In this lesson you will learn how a queue stores and hands out messages, why a message is removed only after it is handled, what a queue gives a system, and what it costs. You will also follow one online order from start to finish.
Read the key words first. They come back later in the lesson.
Key words
- Producer
- The service that creates a message and sends it to the queue.
- Consumer
- The service that takes messages from the queue and does the work in them.
- Queue
- A holding area that keeps each message until a consumer takes it.
- Broker
- The server program that runs the queue and keeps the messages safe.
- Message
- One piece of data that describes work to do, such as 'order 1001 placed'.
- Acknowledgment (ack)
- A signal a consumer sends to the queue to say it finished handling a message.
- Decoupling
- Keeping two services separate, so one does not need the other to be running or fast.
- Delivery guarantee
- A promise about how many times a message will be handed out and handled.
- Dead-letter queue (DLQ)
- A second queue where messages go after failing again and again.
What Are the Three Parts of a Queue?
Every message queue has three roles. The sender is the producer. The holding area is the queue, which runs inside a broker. The receiver is the consumer.
Producer
The service that builds a message and sends it to the queue. It usually does not wait for the work to finish. It drops the message in and moves on.
Queue
The holding area that keeps each message until someone takes it.
Consumer
The service that takes messages out and does the work. It runs at its own speed and tells the queue when each message is done.
How Does a Message Travel?
A message moves through a fixed set of stages. The queue does not delete it at the first stage, and that is the key idea.
- 1
Send
The producer writes the message, with the details the work needs, and sends it to the queue. The queue stores it and tells the producer it has the message.
- 2
Wait
The message sits in line. If every consumer is busy, it waits.
- 3
Take
A consumer takes the message. The queue keeps its copy while the consumer works, so the message is not gone yet.
- 4
Acknowledge
When the work is complete, the consumer sends an ack back to the queue. The ack means: this one is handled.
- 5
Delete
The queue removes the message only after the ack.
Why Does Removal Wait for the Acknowledgment?
Consider a queue that deletes a message the moment a consumer takes it. If that consumer crashes halfway through an order, the order is gone and nothing can retry it. Waiting for the ack prevents this loss. RabbitMQ's guidance says a consumer should not confirm a message until it has finished what it needs to do with it, such as saving the data or passing it on. Once the ack arrives, the broker is free to remove the message.
Some clients offer an auto-ack mode, which counts a message as done the moment it is sent. That is faster, but a crash during the work then loses the message. Auto-ack can be unsafe for some workloads.
from collections import deque
queue = deque()
in_flight = {}
queue.append('order 1001 placed')
queue.append('order 1002 placed')
msg_id = 1
body = queue.popleft()
in_flight[msg_id] = body
print('received:', body)
del in_flight[msg_id]
print('acked, left in queue:', len(queue), 'in flight:', len(in_flight))
msg_id = 2
body = queue.popleft()
in_flight[msg_id] = body
print('received:', body)
# the consumer crashes here, so no ack is sent
queue.appendleft(in_flight.pop(msg_id))
print('redelivered:', queue[0])
Output
received: order 1001 placed acked, left in queue: 1 in flight: 0 received: order 1002 placed redelivered: order 1002 placed
This is a toy queue, not a real broker, but it shows the rule. A taken message waits in a separate in-flight area. The ack removes it. Without an ack, the message goes back to the front of the queue.
What Does a Queue Give You?
A queue is useful for three reasons. All three come from the same holding area.
Decoupling
The producer does not need to know which services read its messages, how many readers there are, or how they work. A new consumer can be added later without changing the sender.
Smoothing traffic spikes
Traffic often arrives in bursts, such as a sale that opens at noon. The queue holds the extra messages, and consumers take them at a steady pace. This is often called load leveling.
Surviving consumer outages
If a consumer is down, its messages wait in the queue. They are not lost, because the queue keeps each one until an ack arrives. When the consumer returns, it picks up the backlog.
How Do Direct Calls Compare with Queued Messages?
A direct call means one service asks another and waits for the answer. If the other service is slow, the first one is slow too, and a user may see a timeout. With a queue, the first service writes the message, gets a fast reply that the queue has it, and carries on. The slow part then happens in the background.
Tip
A queue is a poor fit when the user needs the answer right now, such as a card's approval code. Use a direct call for that step, and send the follow-up work through a queue.
Worked Example: Placing an Online Order
Take an online shop such as Amazon. When you place an order, several things must happen: the card is charged, stock is reduced, and a confirmation email is sent. The steps below show a simplified version of one way to use a queue. They are an illustration, not a description of any company's real systems.
- 1
Checkout publishes
The checkout service writes an 'order placed' message with the order number and items, and puts it on a queue.
- 2
Checkout replies at once
The queue confirms it has the message. Checkout shows you the confirmation screen right away, without waiting for payment or email.
- 3
Payment consumer
The payment consumer reads its copy, asks the payment provider to take the money, and then sends its ack.
- 4
Inventory consumer
The inventory consumer reads its copy, lowers the stock count, and then sends its ack.
- 5
Email consumer
The email consumer reads its copy, sends the confirmation email, and then sends its ack. If this service is slow, only its own copies wait.
Tip
Three services need the same order, but a single shared queue gives each message to only one reader. Each consumer then acks its own copy, so one slow or failing service does not hold up the others.
Now picture the email service down for ten minutes. Its copies pile up in its own queue, while payment and inventory keep working. When the email service returns, it reads the backlog and sends the emails. They arrive a little late, but they are not lost.
Delivery Guarantees and Their Trade-offs
A delivery guarantee is a promise about how many times a message will be handed to a consumer and handled. The promise depends on when the ack happens compared with the work. Three promises come up often.
At most once
The message is handled zero or one times. The ack comes before the work, or there is no ack at all. A crash can lose the message, but it is never repeated. This is fast, but work can be missed.
At least once
The message is handled one or more times. The ack comes after the work. A crash before the ack causes a new delivery. Nothing is lost, but the same message may be handled twice.
Exactly once
Handling each message once and only once is hard to guarantee across a whole system.
RabbitMQ acknowledgements guarantee at least once delivery. Read the quiz below to see how the ack timing decides the outcome.
Quick check
A consumer takes a 'card charged' message, sends its ack to the queue right away, and then starts the charge. The server crashes before the charge finishes. What happened, and which guarantee does this design follow?
Duplicates and Message Order
A duplicate happens when a consumer finishes the work, crashes before its ack reaches the queue, and then receives the same message again. The fix is not in the queue. The consumer must be built so that running the same job twice does no harm. This lesson does not teach how to build one; that belongs to its own lesson.
Order is the second trade-off. This matters when one step must finish before the next, such as a refund that must follow a charge.
Dead-Letter Queues
Some messages never succeed. The data may be malformed, or the code may have a bug that every attempt triggers. If the queue keeps handing that message out, it wastes effort and may block other work. A dead-letter queue is a second queue where such messages are moved after repeated failures.
A dead-letter queue is an ordinary queue. A main queue is linked to it and sends over the messages it cannot process. Engineers can then read those messages, see why they failed, and decide what to do.
- 1
Set a limit
Choose how many delivery attempts a message gets before it is moved. AWS calls this setting maxReceiveCount. If the limit is 1, one glitch moves a healthy message, so the limit should be high enough to allow real retries.
- 2
Count the attempts
Each time a consumer takes the message and does not ack it, the attempt count goes up by one.
- 3
Move it
When the count reaches the limit, the queue moves the message to the dead-letter queue instead of offering it again.
- 4
Inspect and fix
An engineer reads the messages, finds the cause, and fixes the code or the data.
- 5
Redrive
Once the problem is fixed, the messages can be moved back to the main queue to be processed again.
MAX_ATTEMPTS = 3
main_queue = ['good order', 'bad order']
dead_letter = []
attempts = {}
def handle(body):
return body != 'bad order'
while main_queue:
body = main_queue.pop(0)
attempts[body] = attempts.get(body, 0) + 1
if handle(body):
print('done:', body)
elif attempts[body] >= MAX_ATTEMPTS:
dead_letter.append(body)
print('dead letter:', body)
else:
main_queue.append(body)
print('retry:', body, 'attempt', attempts[body])
Output
done: good order retry: bad order attempt 1 retry: bad order attempt 2 dead letter: bad order
The sample runs the good order once and gives the bad order three tries. Then the bad order moves to the dead-letter list, and the main queue is empty. The good work is never blocked.
Tip
Set an alarm on the dead-letter queue so a person hears when messages arrive. A dead-letter queue that nobody watches becomes a quiet pile of failures. Keep its retention period longer than the main queue's, so messages are not deleted before someone reads them.
Check Your Understanding
Try this scenario. It uses the ideas from the whole lesson.
Quick check
A ticket site's checkout calls the email service directly and waits for it. During a big sale, the email service slows down and checkouts time out. What change helps most, and what happens to the emails if the email service is down for ten minutes?
Summary
A message queue is a holding area between a producer and a consumer. The producer hands off its work and moves on. The consumer takes messages at its own pace and acks each one when the work is done. The queue deletes a message only after that ack. This gives you decoupling, smoother traffic, and safety during outages. The costs are delay, possible duplicates, and the need to plan for message order and failed messages. Next, you can study how queues support background jobs, and how a system asks senders to slow down when it is overloaded.
Sources
- RabbitMQ Reliability Guide
- RabbitMQ Consumer Acknowledgements and Publisher Confirms
- RabbitMQ Consumers guide
- RabbitMQ tutorial: Work Queues
- Amazon SQS Developer Guide: Using dead-letter queues
- Amazon SQS FAQs
- Amazon SQS announces Dead Letter Queues (2014)
- Amazon SQS dead-letter queue redrive via SDK and CLI (2023)
- DesignGurus: Message Queues in System Design Interviews
- LevelUp Coding: How Message Queues Actually Work
- Redisson glossary: What Is a Message Queue?
- Stream: How Does a Message Queue Power Reliable Chat Delivery?
- Stream glossary: Message Queuing
- OneUptime: How to Handle Message Acknowledgment in RabbitMQ
- Wikipedia: Dead letter queue
Last updated on 17 November 2026
Related lessons
Learn with me
Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.