Lesson 5 of 9 · 26m
Dead Letters and Safe Replay
Use dead-letter queues as investigation surfaces, not trash cans — capture context, quarantine poison messages, and replay with controls.
A dead-letter queue is not where bad messages go to disappear. It is evidence preservation for work your system could not perform. Teams get into trouble when the DLQ becomes a silent landfill: messages accumulate, nobody owns them, and months later someone presses "replay all" into a production schema that no longer understands them.
Dead letters are a symptom with context
A useful dead-letter record includes more than the raw payload:
{
"message_id": "01J9ZK5V0B4T4P7S5H7M0HCEEW", "type": "invoice.finalized", "attempts": 5,
"first_failed_at": "2026-11-30T18:04:12Z", "dead_lettered_at": "2026-11-30T18:19:44Z",
"error_class": "KeyError", "error_message": "'customer_email'", "worker_version": "2026.11.30-4",
"body": {"invoice_id": "inv_10492", "currency": "usd"}
}Capture attempt count, exception class, worker version, timestamps, and trace ID. Do not blindly copy secrets or personal data into a secondary queue with broader access.
Configure redrive deliberately
In SQS, the source queue redrives messages after a fixed number of receives:
{"deadLetterTargetArn": "arn:aws:sqs:us-east-1:123456789012:billing-dlq", "maxReceiveCount": 5}maxReceiveCount is not retry count exactly; it counts receives, including crashed or abandoned processing. Kafka consumers usually write poison records to an error topic and commit the original offset; RabbitMQ uses dead-letter exchanges; database queues use a terminal state. Same idea, different mechanics.
Quarantine beats deletion
The correct default is quarantine and inspect. Some messages are permanently malformed. Some are valid but arrived before a migration. Some expose a worker bug fixed in a later deploy. Treat them differently:
malformed schema discard after validation and evidence capture
transient dependency replay after dependency recovery
worker bug fixed replay using fixed worker version
conflict or unknown route to operations or investigate; never bulk replayReplay is production mutation
Replaying is not harmless. It may send email, create invoices, trigger webhooks, or mutate customer-visible state. Build a replay tool with explicit filters and rate limits:
python manage.py replay-dead-letters --queue billing-dlq --type invoice.finalized --dead-before 2026-11-30T19:00:00Z --limit 100 --rate 5/second --dry-runDry-run first. Record who replayed what and why. Ideally replay through the same idempotent worker path rather than a bespoke "force processor."
Alert on the DLQ as a queue
A DLQ depth of zero is healthy. A growing DLQ is backlog with worse semantics. Alert on age of oldest message, sudden growth, and repeated error classes. Dashboards should show source queue, worker version, and error distribution.
Field notes
- Never put secrets in error payloads. Redact authorization headers, API keys, and tokens before writing failure context.
- Replay order can violate per-entity ordering. Sort or partition by aggregate key when ordering matters.
- A message dead-lettered under schema v1 may be invalid under v3. Migration tooling beats blind retry.
- Keep a small sample payload in the incident ticket. "It errored" is not enough context to prevent recurrence.
Was this lesson helpful?