Message Queues
Definition
Message queue
- In plain English
- A message queue holds messages from producers until consumers are ready to process them.
- Think of it like
- It is like the ticket rail in a kitchen where orders wait for the next free cook.
Producer and consumer
- In plain English
- A producer puts messages onto the queue, and a consumer takes them off and does the work.
- Think of it like
- It is like waiters pinning tickets up and cooks taking them down.
Competing consumers
- In plain English
- Competing consumers are several consumers taking from one queue, each message going to just one of them.
- Think of it like
- Like several cooks working one ticket rail.
Why it exists
Nine hundred orders arrive in ten minutes. Kitchens cannot all finish at the same speed as incoming requests.
A consumer crashes while work is waiting. A durable queue keeps accepted messages until processing can resume.
How it works
Store messages durably and retain them long enough for recovery. A consumer acknowledgement confirms handling, but does not prevent duplicate effects.
Acknowledgement
- In plain English
- An acknowledgement is the consumer telling the queue a message is fully handled, so it can be deleted.
- Think of it like
- Like a cook spiking a finished ticket.
Dead-letter queue
- In plain English
- A dead-letter queue is a side queue for messages that keep failing, so they don't block the rest.
- Think of it like
- Like a "problem orders" tray the manager checks.
Producers send to a durable queue, which delivers work to two competing consumers and moves repeated failures to a dead-letter queue.
- Publish safely
The producer waits for confirmation that the queue stored the message durably.
- Receive and process
A consumer takes a message and completes its work.
- Acknowledge completion
Acknowledge only after the required effect is saved.
- Retry unfinished work
In this queue design, a message without acknowledgement becomes available after a visibility timeout.
- Isolate repeated failure
After five failed attempts, move the message to a dead-letter queue for investigation.
- A queue buffers bursts but cannot fix a permanently slower processing rate.
- Add consumers only when the work can run independently.
- A lost acknowledgement can cause completed work to be delivered again; prevent repeated effects.
Competing consumers share deliveries rather than all receiving copies. A retry can reach a different consumer after a crash.
Examples
Kitchen router
Friday 7 pm: 900 orders in 10 minutes. The queue holds them; kitchens pull at their own pace instead of the order service waiting on each kitchen.
Nine hundred orders in ten minutes average 90 orders per minute. Bursts within that window can still exceed kitchen capacity.
arrivals: 90 orders/minute on average
capacity: 100 orders/minute across kitchens
short bursts: wait in the queue
sustained arrivals above capacity: backlog growsThe insight
Queues turn spikes into steady work.
Airport baggage
A sorting consumer crashes for four minutes. Durably accepted bag-scan messages remain within the queue’s retention period.
When the consumer restarts, it resumes waiting work. Unacknowledged deliveries can appear again, so repeated scans must not repeat physical actions.
Durable bag scans → Consumer unavailable → Restart and retry.
The insight
Queues let consumers fail and recover without losing work.
Podcast hosting
Workers share jobs that convert podcast audio into playback formats. A corrupt file fails five times and enters the dead-letter queue.
Workers convert audio files into playback formats. A corrupt file fails five attempts and moves aside for investigation.
- Healthy jobs continue through other workers.
- Inspect and repair failed jobs before replaying them.
The insight
Dead-letter queues isolate poison messages.
Trade-offs
Pros
- Buffer short bursts.
- Retry interrupted work.
- Scale independent workers.
Cons
- Duplicates remain possible.
- Queue storage and operation cost money.
- Following a job across machines takes effort.
Use when
- Background work arrives in bursts.
- Producers and consumers fail independently.
Avoid when
- The caller needs an immediate result.
- Unbounded waiting would violate a deadline.
Quick check
Takeaways
- Queues hold work between producers and consumers.
- Acknowledge after completing work, and handle redelivery safely.
- Dead-letter queues isolate jobs that need investigation.