Message queues: design asynchronous processing, retries, and failures safely
A message queue stores work between producers and consumers. Producers enqueue work and consumers process it at their own pace, buffering bursts and decoupling execution. This adds trade-offs: response delay, duplicate delivery, changed ordering, and backlog. This guide covers cloud backend work queues, not broker-specific configuration.
How does a queue change the request flow?
With a synchronous call, the caller waits for the service response. With a queue, the producer sends the work and a tracking ID, then can acknowledge receipt. The consumer processes it later. If a result is needed, design a separate status lookup, callback, or reply queue. A queue is usually one-way communication. (Microsoft Queue-Based Load Leveling)
A queue buffers temporary production spikes so consumers can work at a manageable rate. But if average production stays above consumption, backlog and wait time keep growing. Scaling consumers without limits can move overload to a database or external API, so bound concurrency and downstream call rates.
How should loss and duplicate processing be handled?
Many queues use at-least-once delivery, so a message can return after a failure. Make consumers idempotent: repeated processing of the same message ID or business key must not apply the outcome twice. For example, a repeated payment task can return an existing result for the same business key instead of creating another charge. This is an illustrative design, not a tested payment implementation. (Microsoft Queue-Based Load Leveling)
Consumers acknowledge or delete messages after processing. If a consumer stops before acknowledgement, a broker may redeliver. Acknowledging too early can lose unfinished work. Details differ by broker: Amazon SQS hides a received message during its visibility timeout, then may make it available again if not deleted. SQS does not guarantee zero duplicates during that interval. (Amazon SQS: Visibility timeout)
Route persistently failing messages to a dead-letter queue (DLQ) after a bounded retry policy. Monitor DLQ depth and message age, investigate causes, then decide how to resubmit safely. Choose persistence, retention, and size limits for the workload’s loss tolerance.
How should throughput and ordering be designed?
Monitor queue depth, age of the oldest message, success/failure rates, redeliveries, consumer concurrency, and downstream latency. If backlog keeps growing, scale within safe limits or limit producer intake.
If work can exceed the broker’s visibility or acknowledgement window, consider its supported extension or heartbeat. An overly long window delays retries. SQS defaults and limits apply only to SQS; do not generalize them to other brokers. ([Amazon SQS: Visibility timeout](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-visibility-timeout.html))
Multiple producers/consumers, redelivery, and priority can change submission order. If order matters, verify the selected broker’s partition, message-group, or single-consumer guarantees. RabbitMQ also notes that multiple consumers and redelivery can affect effective order. ([RabbitMQ: Queues](https://www.rabbitmq.com/docs/queues), [Microsoft Queue-Based Load Leveling](https://learn.microsoft.com/en-us/azure/architecture/patterns/queue-based-load-leveling))
A queue may add complexity without benefit when callers require an immediate synchronous response or workload is low and predictable. Also design message schema, access control, sensitive-data handling, and observability.
What should be verified after introducing a queue?
In a non-production environment, reproduce successful processing, redelivery after consumer restart, duplicate messages, persistent failure, and DLQ isolation. Link message and business correlation IDs so logs and metrics trace enqueue, delivery, processing, and acknowledgement. Check duplicates do not duplicate business outcomes, and that consumer concurrency stays safe when the downstream store slows.
Success is not simply reducing queue depth by adding consumers. Work must not be lost or applied twice; backlog and wait time must stay acceptable; failures must be diagnosable; and downstream systems must avoid overload. Verify delivery, ordering, durability, and limits in the chosen broker’s documentation.