What it is
A message queue is a buffer between programs. A producer puts a message on the queue ("send the confirmation for order 123") and moves on. One or more consumers (workers) take messages off the queue, do the work, and acknowledge each message when they are done, which removes it for good. Amazon SQS, RabbitMQ, Azure Service Bus and many job frameworks work this way.
Why it exists
When one service calls another synchronously, the caller waits for all the callee's work and fails when the callee fails. That is fine for work the user needs right now, and wasteful for work that can happen a moment later: sending emails, resizing images, generating invoices, calling slow third parties. A queue takes that work out of the request path. The producer stays fast and available even when consumers are slow, busy or down, and consumers can be scaled independently.
How it works
Publish. The producer sends a message and gets a quick confirmation that the queue stored it. From then on the queue is responsible for it.
Consume and acknowledge. A consumer receives a message. The queue does not delete it yet; it hides it for a visibility timeout. If the consumer finishes and acknowledges in time, the message is deleted. If the consumer crashes, or takes longer than the timeout, the message becomes visible again and is delivered to another consumer.
At-least-once delivery. Acknowledging after processing means a message is never lost because a worker died, but it can be processed more than once: a worker might finish the work and crash before its acknowledgement arrives, or a slow worker might still be running when the message is redelivered. Duplicates are a normal outcome, not a bug in the queue. Consumers must be idempotent: processing the same message twice has the same effect as once (record processed message or business ids, use upserts, pass idempotency keys to payment providers).
Retries and dead letters. A message that fails is delivered again. To stop a "poison" message that can never succeed from being retried forever, the queue counts deliveries; after a maximum it moves the message to a dead letter queue (DLQ) for inspection and repair.
Backlog. If messages arrive faster than consumers process them, the backlog grows. During a short burst that is the point: the queue holds the peak and consumers catch up afterwards. If the gap persists, messages get older and older until the queue's limit is reached.
When to use it
- Work the caller does not need to finish before it answers: notifications, media processing, reports, syncing to other systems.
- Smoothing spiky traffic so consumers can be sized for the average rather than the peak.
- Isolating a request path from a slow or unreliable dependency.
- Distributing work across a pool of workers that can scale with the backlog.
When not to
- When the user needs the result in the response (a login check, the page they asked for).
- When strict ordering across all messages is required and you cannot partition by key.
- For simple, fast, reliable calls where a queue only adds latency, infrastructure and eventual consistency.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Queue instead of a synchronous call | Fast, available producer; independent scaling | Eventual completion, duplicates, harder debugging |
| Long visibility timeout | Fewer duplicates from slow work | Slower recovery after a consumer crash |
| Short visibility timeout | Fast redelivery after crashes | Duplicates whenever work runs long |
| High max deliveries | Survives longer transient outages | Poison messages occupy workers longer |
| Many parallel consumers | Throughput | No global ordering |
Alternatives
- Synchronous calls with timeouts, retries and a circuit breaker: simpler when the caller needs the answer.
- Publish/subscribe: one event delivered to many independent subscribers, instead of one task to one worker.
- Event streaming (Kafka, Kinesis): an ordered, replayable log consumed by offset; consumers track their position rather than acknowledging individual messages.
- A jobs table polled by workers: fine at small scale and transactional with your data, but polling and locking get costly.
Failure modes
- Growing backlog: consumers cannot keep up; message age rises until users notice (emails hours late) or the queue fills. Alert on the age of the oldest message, not only the count.
- Duplicates: crashes, network retries and short visibility timeouts all cause redelivery. Non-idempotent consumers send two emails or charge twice.
- Poison messages: a malformed message fails every time; without a delivery limit it is retried forever.
- Ignored DLQ: messages pile up in the dead letter queue and nobody looks. A growing DLQ is an incident.
- Lost messages on the producer side: the database commit succeeds but the publish fails (or the reverse). The transactional outbox pattern fixes this.
- Out-of-order processing: parallel consumers and redeliveries reorder messages.
In production
Keep messages small (ids rather than whole objects) and versioned. Make every consumer idempotent. Set the visibility timeout above the slowest normal processing time and extend it for long jobs. Cap deliveries, attach a DLQ, and alert on DLQ size and on the oldest message's age. Scale consumers on backlog. Use the transactional outbox when a message must be published if and only if a database change commits.
Common misconceptions
- "A queue adds capacity." It buffers. Throughput is still set by the consumers.
- "The queue guarantees exactly-once processing." Delivery is at-least-once; exactly-once effects come from idempotent consumers.
- "Messages are processed in order." Not with several consumers or redeliveries, unless you partition by key and process each partition sequentially.
- "Once it's on the queue, it's handled." It is stored. Whether it ever succeeds depends on retries, the DLQ and someone watching it.
Interview angle
Use a queue whenever a design has slow or non-essential side effects, and say why: latency, isolation, burst absorption. Then expect questions on delivery guarantees (at-least-once and idempotency), visibility timeouts and acknowledgement, retries and dead letters, ordering, backpressure when consumers fall behind, and how producers publish reliably (outbox). Quantifying the backlog ("40 msgs/s in, 26 out, so it grows by 14/s; we autoscale workers on queue age") stands out.
Model assumptions
The simulation is a simplified model, not a benchmark. A confirmation takes about 300 ms to send (lognormal, cv 0.3; cv 1.5 in the duplicates step). The synchronous Email Service has 8 workers and room for 10 waiting calls. With the queue, two workers with 4 slots each consume messages round robin; a worker holds a slot for each unacknowledged message until it is acknowledged or its visibility timeout expires. The visibility timeout is 2 s (1 s in the duplicates step), and after 3 deliveries a message moves to the dead letter queue. A failed send is simply not acknowledged. Orders succeed as soon as the message is stored. Message accounting: each message ends in one outcome, counted once: delivered (first successful acknowledgement), dead-lettered (moved to a dead letter queue after its last delivery) or dropped (it ran out of deliveries with no dead letter queue connected, or a subscription was full or down when its copy arrived). Redelivered (a delivery that came back after its timeout) and duplicates (a message processed again after it had already been processed) are not outcomes: they count extra deliveries of messages that are counted once above. Numbers show behaviour and orders of magnitude only.