A message queue solves a real problem: it takes slow work out of the path between a user clicking something and the page responding. It also replaces one failure mode you understand with several you probably have not thought about yet. Both halves of that sentence matter, and most introductions only cover the first.
The shape of it
Three parts. A producer puts a message on the queue. The queue holds it. A consumer (or worker) takes it off and does the work.
The gain is decoupling. Your web request finishes as soon as the message is accepted — it does not wait for the email to send, the video to encode or the payment provider to answer.
When a queue genuinely helps
- Slow work in a request. Anything the user does not need the result of immediately: email, image processing, report generation, webhooks.
- Traffic spikes. A queue absorbs a burst and lets workers drain it at a sustainable rate, instead of the burst taking down the database.
- Unreliable dependencies. If a third-party API is down, messages wait rather than requests failing.
- Fan-out. One event that several unrelated systems need to react to.
Delivery guarantees, read properly
This is where most of the trouble lives.
- At-most-once. Fire and forget. Messages can be lost. Acceptable for metrics; not for payments.
- At-least-once. The common default. Messages are not lost, and duplicates are possible — because a worker can finish the job and die before acknowledging it, so the message is redelivered.
- Exactly-once. Advertised more than it exists. Where offered, it holds within boundaries that rarely cover an end-to-end pipeline involving your database and a third-party API.
So build for duplicates. Make consumers idempotent: processing the same message twice should produce the same result as processing it once. In practice that means a unique key per job and a check before acting, not a hope that redelivery will not happen.
This single decision prevents the most expensive class of queue bug — charging a customer twice because a worker was restarted at the wrong moment.
The other things to set up on day one
A dead letter queue. Somewhere messages go after failing repeatedly. Without one, a message that always fails is retried forever, consuming workers and filling logs. A "poison message" blocking a queue is a classic 3am incident.
Visibility. You need to know queue depth, failure count and how long messages wait. A queue is a place work can quietly pile up, and the first sign is usually a user asking why their email never arrived.
A retry policy with backoff. Immediate infinite retries against a struggling downstream service turn a brief outage into a sustained attack on it.
Ordering, if you need it. Most queues do not guarantee order once you have several workers. If sequence matters, you need partitioning or a single consumer, and both cost throughput.
Picking one
| Option | Suits |
|---|---|
| Database table | Starting out. Easy to inspect, transactional with your data, fine at modest volume. |
| Redis | A step up, low latency. Lists for simple work, streams for consumer groups. |
| RabbitMQ | A real broker: routing, priorities, acknowledgements. The default when a database is no longer enough. |
| Kafka | A distributed log rather than a queue. Replay, multiple independent consumers, very high throughput. |
| Managed (SQS and similar) | You do not want to operate a broker. |
Our guide to Redis covers that option in more depth, including why using one instance for both caching and queueing causes pain.
When not to add one
A queue is a second system with its own failure modes, its own monitoring and its own operational burden. Before adding one:
- Is the work actually slow? Sending one email takes well under a second. Queue it because you do not want the request to fail if the mail server is down, not because of speed.
- Can the user wait? If they need the result on the next screen, a queue means building status polling as well.
- Is the real problem a slow query? Moving a slow query into a background job hides it rather than fixing it.
Debugging gets harder too. A synchronous failure shows up in a stack trace with a request attached. An async failure shows up in a worker log, possibly minutes later, possibly on a different machine.
Written by the WritePures newsroom. Spotted something that needs updating? Tell our editors.
0 Comments
Join the conversation
Replying toNo comments yet. Be the first to share your thoughts.