Skip to content

Dead-letter queues — the pattern, the wiring, and the runbook

Dead-letter queue design across RabbitMQ, Kafka, SQS, JetStream, and friends — what to route, how to alert, and the triage runbook an MSP runs against the DLQ.

A dead-letter queue (DLQ) is the place a message goes when a consumer cannot process it after a bounded number of attempts. The DLQ keeps the main pipeline flowing while preserving the bad message for triage instead of dropping it or trapping a worker in an infinite retry loop. This article is the DLQ pattern we wire for clients across RabbitMQ, Kafka, JetStream, SQS, and Sidekiq/Celery — what to route, what alerts to set, and the human-readable runbook that turns a DLQ from “place where messages go to die” into a real operational tool.

How to verify

Whichever broker you are running, the DLQ wiring is something you check in the runtime config:

# RabbitMQ — dead-letter exchange on a queue policy
sudo rabbitmqctl list_policies | grep -E 'dead-letter|dlx'

# Kafka — dead-letter topic on a Connect connector
curl -s http://connect:8083/connectors/orders-source/config | grep -E 'deadletter'

# JetStream — max delivery before drop, configured per consumer
nats consumer info ORDERS process-orders | grep -E 'MaxDeliver'

# SQS — redrive policy on the source queue
aws sqs get-queue-attributes --queue-url $SOURCE_URL --attribute-names RedrivePolicy

If the DLQ wiring is absent, the broker either silently drops poison messages or loops on them forever. Both are operational bugs.

What’s happening

A poison message is one that the consumer cannot process and that the consumer will keep failing on. Possible causes:

  • The payload is malformed (schema mismatch, missing required field, encoding bug).
  • The payload refers to data that no longer exists (a user_id deleted before the worker got to it).
  • The payload is well-formed but triggers a deterministic bug in the consumer code.
  • An upstream service the consumer depends on is permanently broken for that record.

Without a DLQ, a poison message:

  • Blocks the consumer (reserve it, fail, return to ready, reserve it again — infinite loop) and keeps the partition / queue stuck.
  • Or worse, gets silently dropped after a retry cap that nobody set deliberately, hiding a real bug.

The DLQ pattern fixes both. The broker tracks per-message delivery attempts; on exceeding max_delivery_count (or equivalent), the broker routes the message to a dead-letter destination — a separate queue, topic, or exchange. The main queue keeps flowing. The DLQ holds the bad messages for human triage.

The DLQ is not a “garbage can” — it is an inbox for engineering. Messages in the DLQ need to be looked at, classified, and either replayed (after a code fix), discarded (if the data is genuinely garbage), or escalated (if the cause is upstream).

Wiring per broker

RabbitMQ (quorum or classic queues). Set a dead-letter-exchange policy on the source queue:

rabbitmqctl set_policy DLX-orders "^orders\." \
  '{"dead-letter-exchange":"dlx-main","dead-letter-routing-key":"orders.dead","delivery-limit":5}' \
  --apply-to queues

Then create the exchange and a queue bound to it:

rabbitmqadmin declare exchange name=dlx-main type=topic durable=true
rabbitmqadmin declare queue name=q.orders.dead durable=true \
  arguments='{"x-queue-type":"quorum"}'
rabbitmqadmin declare binding source=dlx-main destination=q.orders.dead routing_key=orders.dead

After 5 failed deliveries (or any basic.nack with requeue=false), the message lands in q.orders.dead with the original payload plus headers describing where it came from.

Kafka Connect. Set on the connector config:

{
  "errors.tolerance": "all",
  "errors.deadletterqueue.topic.name": "orders-dlq",
  "errors.deadletterqueue.topic.replication.factor": "3",
  "errors.deadletterqueue.context.headers.enable": "true"
}

Bad records are produced to orders-dlq with headers carrying the original topic, partition, offset, and the exception.

JetStream. Per consumer, set --max-deliver=5 and configure a separate stream for the DLQ; in your consumer code, on max-redeliver, publish the original payload (with metadata headers) to the DLQ stream and ack the original.

SQS. Set a redrive policy with a deadLetterTargetArn pointing to a separate queue and maxReceiveCount of 5; the broker handles the routing automatically.

Sidekiq. The dead set is the built-in DLQ. Tune Sidekiq.options[:max_retries] to a value that gives transient failures a chance but does not loop forever.

Celery. No built-in DLQ; the canonical pattern is to catch the exception at the task level after max_retries, publish the payload to a DLQ queue, and let the task succeed. Or use a Celery-aware library like celery-dlq that wraps this.

The runbook

Once a DLQ has messages in it, an operator needs to act. The runbook:

  1. Triage the message. Pull one message from the DLQ; look at the payload and the original error (it is usually in a header or a sidecar field).
  2. Classify the cause.
    • Bug in consumer — file a ticket, fix, redeploy, then replay the message.
    • Bad data — verify the upstream system has the same view. If the data is genuinely invalid, the message can be discarded.
    • Temporary upstream failure that is now resolved — replay the message.
    • Permanent upstream failure — escalate to the team that owns the upstream system.
  3. Replay by publishing the message back to the source queue / topic. With the right tooling (shovel plugin for RabbitMQ, a Kafka mirror script, a custom CLI), this is one command.
  4. Drop by acking and deleting the DLQ message after classification.
  5. Track classified messages somewhere persistent so the bug counts and patterns are visible — a Notion doc, a Jira board, a Grafana annotation. The DLQ depth itself is a metric; the classification of why messages landed there is operational gold.

Operational notes

  • The DLQ needs the same durability as the source queue. A DLQ on a single broker for messages that came from a 3-replica source is a regression.
  • Alert on DLQ depth > 0, but also on DLQ depth growing over time. A single message is normal; a growing DLQ is a leak.
  • Don’t auto-replay from the DLQ blindly — the message is in the DLQ because something failed, and replaying without diagnosing the cause replays the failure.
  • Add headers to the DLQ message: original queue, original delivery count, exception message, exception stack trace. Without these, triage is a guessing game.
  • Set a retention on the DLQ itself (e.g., 30 days) so stale, unclassified messages eventually drop. Whoever set up the queue is responsible for the operating cost of letting it grow unbounded.

Stack Harbor wires DLQs as part of any messaging stack we onboard, with the runbook above as an operator-facing document for the on-call rotation — alerts on DLQ depth, classified-cause tracking, and a replay tool that is one command per broker. This is the work covered by our Managed Operations practice.