Operate a dead-letter queue: investigate and safely replay failed messages
A dead-letter queue (DLQ) is a queue or topic that holds messages that cannot be processed on the normal consumer path or that policy says to isolate. Moving a message there does not fix the problem. Classify the cause, set ownership and retention, repair the data or consumer, then replay with duplicate effects controlled.
A dead-letter queue (DLQ) is a queue or topic that holds messages that cannot be processed on the normal consumer path or that policy says to isolate. Moving a message there does not fix the problem. Classify the cause, set ownership and retention, repair the data or consumer, then replay with duplicate effects controlled.
When to send a message to a DLQ
Transient network errors, throttling responses, and brief dependency outages may merit delayed retries. Invalid schemas, missing required fields, unsupported event versions, or permanently rejected business rules are candidates for isolation rather than endless retries. A delivery-attempt limit can also trigger isolation, but allow enough attempts for recovery without overloading a dependency.
A DLQ preserves messages for investigation, but does not automatically solve source-queue backlog, stale data, or ordering blockage. Repeatedly leaving failures on the source queue can raise costs or reduce throughput, so choose a policy that fits delivery semantics and ordering requirements.
Preserve useful information
Keep the message body and enough context to diagnose and safely route it back: source queue/subscription, original message ID, first publish time, delivery count, failure time, classified error code, and a concise description. Do not copy passwords, access tokens, payment data, or other sensitive content into error descriptions or logs. Check message-size limits and sensitive-data exposure before storing full exceptions or stack traces.
Systems do not all add the same metadata. Azure Service Bus provides a dead-letter reason and description; Pub/Sub can expose delivery-attempt information in different fields depending on configuration and client; Amazon SQS uses maxReceiveCount in its redrive policy. Verify product behavior and defaults in current documentation rather than generalizing one provider's rules.
Operational workflow
**Set alerts and ownership.** Monitor DLQ count, arrival rate, age of the oldest message, and recurring error codes. Watch for continuous growth as well as whether the queue is empty. Assign an investigator and response time.
**Investigate against the source.** Check the original ID and source, failure reason, attempt count, and schema/consumer versions. Group similar failures and classify them as transient or persistent input errors. A manual read may count as a delivery attempt in some systems; check the product behavior so diagnosis does not alter retry policy.
**Repair the recovery condition first.** Fix consumer code or configuration; route bad messages through an approved data-correction process owned by the business. Reinjecting without a fix can repeat the failure. Preserve the original and transformation history if the message format must change.
**Replay in small batches.** Validate safety through an isolated test path, then redrive a sample if possible and watch success and side effects. Set rate limits, stop conditions, deduplication keys, and ordering requirements. Do not assume automatic redrive restores original order or guarantees exactly-once effects.
**Verify and close out.** Check successful processing, messages returned to the DLQ, duplicate or ordering issues, and business-data outcomes. Retain or delete unresolved messages according to retention and approval rules; record findings and actions.
Decision table before replay
| Observed cause | First action | Replay when |
|---|---|---|
| Temporary dependency outage | Confirm recovery and retry load | Service is healthy and retry capacity is available |
| Invalid message format or field | Check producer contract and correction path | Approved transformation or producer fix is verified |
| Consumer bug or bad configuration | Check corrected version and isolated test path | Representative messages succeed with the fix |
| Invalid business data | Obtain business-owner review and auditable correction | Owner approves and outcome can be traced |
| Unknown cause or uncertain external effect | Stop automatic reinjection; reconcile prior execution | State is checked and recovery owner approves |
Replay executes the message again. If a prior attempt may already have charged a payment or sent an email, blind redelivery can duplicate the side effect. Keep the consumer's idempotency key and inspect the external operation ID or result first. When source order matters, decide whether later messages may proceed while one message is isolated.
Retention and access control
Business data may remain in a DLQ for a long time. Restrict access as carefully as for the normal queue, and apply encryption, audit logging, and retention/deletion rules. Choose retention using replay SLA, message expiry, legal and business requirements, and storage cost. Expiry semantics differ by platform. For example, Azure Service Bus documents that TTL does not apply to its DLQ, so messages can remain until an operator removes them.
What to verify
In a test environment, confirm that transient and permanent failures follow intended paths, messages are isolated after the configured attempt limit, and diagnostic metadata is preserved. After a consumer fix, test a limited redrive and safe handling of duplicates, ordering, and external effects. No real broker or DLQ was run or tested for this article.
Sources
[Azure Service Bus: Dead-letter queues](https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dead-letter-queues)
[Amazon SQS: Using dead-letter queues](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html)
[Google Cloud Pub/Sub: Dead-letter topics](https://cloud.google.com/pubsub/docs/dead-letter-topics)