Course contents

07. Messaging and Event-Driven Systems Lesson 9 of 10

Dead Letter Queues

See a poison message loop forever, failed deliveries eat worker capacity, a delivery limit park messages in a DLQ, an outage flood it, and a redrive.

15 minIntermediate7 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • A poison message fails on every delivery; with unlimited retries it loops forever.
  • A failed, unacknowledged delivery holds consumer capacity until its visibility timeout expires.
  • A delivery limit bounds the cost of any single message; the DLQ keeps those messages instead of dropping them.
  • A DLQ does not fix a broken consumer, and transient outages must not be dead-lettered.
  • A DLQ needs an alert, failure context and a rate-limited redrive into idempotent consumers.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

A dead letter queue (DLQ) is a separate queue where a broker or consumer puts messages that could not be processed after a set number of attempts. The main queue stays moving; the failed messages are kept, with their content intact, for someone to inspect, fix and send back (redrive).

Why it exists

With at-least-once delivery, a failed message is simply not acknowledged and comes back to be tried again. That is the right behaviour for a transient error and the wrong one for a poison message: a malformed payload, a reference to a record that does not exist, a bug triggered by one specific input. It fails on every delivery, so unlimited retries never end. Each attempt costs consumer time and, in many brokers, holds an in-flight slot until it times out. In ordered consumption (a FIFO message group, a stream partition) it is worse: the consumer cannot move past the message, so everything behind it waits. A delivery limit plus a DLQ ends the loop without throwing the message away.

How it works

Counting deliveries. The broker counts how often each message has been delivered: SQS calls the limit maxReceiveCount in a redrive policy, Azure Service Bus MaxDeliveryCount (10 by default), RabbitMQ quorum queues a delivery limit with a dead letter exchange as the target. When a message's delivery fails and the count has reached the limit, it is moved to the DLQ instead of being made visible again.

Consumer-side dead-lettering. Log-based systems like Kafka have no per-message delivery count. The consumer catches the error, publishes the record to a dead letter topic (often after a few retry topics with increasing delays) and commits its offset to move on. Frameworks and Kafka Connect provide this as configuration.

Classifying errors. Good consumers do not leave every failure to the delivery counter. Permanent errors (validation failures, unparseable payloads, missing references) can go to the DLQ on the first attempt. Transient errors (timeouts, 503s, throttling) deserve retries with backoff, and during a known outage it is better to pause consumption (a circuit breaker on the consumer) than to burn through delivery attempts.

Redrive. After fixing the cause, messages are moved back to the source queue (SQS and Service Bus have built-in redrive or move operations) or replayed by a job. Redriven messages are late and may already have been partly processed, so consumers must be idempotent, and the redrive should be rate-limited.

When to use it

  • Any queue consumer whose processing can fail for reasons a retry will not fix. In practice: almost every production queue.
  • Ordered streams, where one stuck message would otherwise stall a whole partition.
  • Integrations with external systems that occasionally reject specific payloads.

When not to

  • Do not use the DLQ as the retry mechanism for outages; that is what backoff, retry topics and circuit breakers are for.
  • Do not add a DLQ that nobody monitors: it only delays and hides data loss.
  • For at-most-once telemetry where failed messages are worthless anyway, dropping may be the honest choice.

Trade-offs

Choice You gain You pay
Low delivery limit (e.g. 3) Poison messages leave fast, little wasted capacity Short outages push valid messages into the DLQ
High delivery limit Rides out longer transient failures Poison messages loop longer and cost more capacity
Dead-letter permanent errors on first failure No wasted retries Needs reliable error classification in the consumer
Retry topics with delays before the DLQ Backoff without blocking the main topic More topics, more moving parts, reordering
Automatic redrive No manual work Messages cycle during an unfixed outage and real poison loops anyway

Alternatives

  • Drop after N attempts: simpler, acceptable only when the messages have no value after failing.
  • Parking-lot table: write failed messages to a database table with the error, which is easier to query and edit than a queue.
  • Skip-and-log in stream processing: commit past the bad record and log it, with an alert; cheap but relies on logs as storage.
  • Pause and alert: stop consuming when failures pass a threshold, for systems where order matters more than progress.

Failure modes

  • Infinite loop: no delivery limit; poison messages consume capacity forever and, in ordered queues, block everything behind them.
  • Capacity collapse: failed deliveries hold in-flight slots for the visibility timeout; a high failure rate turns plenty of capacity into a growing backlog while workers look idle.
  • Outage flood: a dependency blip shorter than one alert cycle dead-letters thousands of valid messages.
  • Silent DLQ: no alert on depth or age; messages expire there unseen.
  • Retention expiry: in some brokers (SQS standard queues, for example) a message's expiry is based on its original enqueue time, so a DLQ with the same or shorter retention can delete messages soon after they arrive. Give the DLQ longer retention than the source.
  • Redrive storm: replaying a large DLQ at full speed overloads the consumer or the dependency that just recovered.
  • Lost context: dead letters without the error, stack trace or attempt count make diagnosis guesswork.

In production

Attach a DLQ to every important queue and alert when it is non-empty or when its oldest message passes an age threshold. Record the failure reason with each message (headers or attributes). Classify errors in the consumer, retry transient ones with exponential backoff and jitter, and size the limit so delivery attempts × timeout outlast a normal dependency blip. Keep the DLQ's retention longer than the source's. Redrive only after fixing the cause, at a limited rate, into idempotent consumers, and track how many redriven messages fail again.

Common misconceptions

  • "The DLQ is where failures are handled." It is where they wait. Handling means an alert, a fix and a redrive.
  • "A DLQ protects against a bad deploy." It parks individual messages; a consumer that fails most messages still collapses throughput. Roll back.
  • "More retries are always safer." For poison messages they are pure waste, and in ordered queues they stall everything.
  • "Everything in the DLQ is broken." After an outage, most of it may be perfectly valid.

Interview angle

Whenever you add a queue, add "with a delivery limit and a DLQ, alerted on depth and age" in the same sentence. Interviewers then ask how you tell transient from permanent failures, how you would handle a poison message in an ordered stream (retry topics, dead letter topic, skip with alert), what happens to the DLQ during an outage, and how you redrive safely. Mentioning that unacknowledged failures hold in-flight capacity, and that DLQ retention must outlast the source queue's, shows operational experience.

Model assumptions

The simulation is a simplified model, not a benchmark. The Order Service publishes 20 messages per second (Poisson). Two workers with 8 slots each take about 100 ms per message (lognormal, cv 0.3). A failed delivery is not acknowledged: the message stays in flight, and keeps its worker slot, until its 1 s visibility timeout expires; then it goes back to the front of the queue. "Retry forever" is a delivery limit of 1,000, which no message reaches in a step. The engine's failure rate is a per-delivery probability, so a retried message may succeed; a true poison message (failing every time) is shown as a scripted packet loop. The outage is modelled as every delivery failing for 3 s. The redrive job is a worker consuming the dead letter queue directly, standing in for moving messages back to the source queue. 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. The Redelivered, Dead-lettered and Dropped tiles show these counts. Numbers show behaviour and orders of magnitude only.

The lesson as textEvery step of the simulation, written out.
  1. 1. A healthy queue, lots of headroom

    The Order Service publishes 20 events per second. Two workers with 8 slots each process one in about 100 ms, so together they could handle about 160 per second. The queue stays nearly empty.

    When processing fails, the worker does not ack: the message comes back after the 1 s visibility timeout and is tried again. For now, nothing fails, and there is no limit on how often a message may be retried.

    Scene: The Order Service publishes 20 events per second to the Order Events queue; two workers process them with plenty of spare capacity. The queue is almost empty and nothing is redelivered.

    Key idea: Redelivery is the queue’s retry mechanism: a failed message simply isn’t acknowledged and comes back after the visibility timeout.

  2. 2. A message that can never succeed

    One event has a malformed payload: a field the consumer cannot parse. Every delivery throws, so it is never acked; every second it comes back and fails again: delivery 1, 2, 3, 4… forever. That is a poison message. Retrying cannot help, because nothing about it changes.

    The highlighted loop is a scripted illustration of that one message; the other traffic is the live simulation. In an ordered queue or a stream partition it is worse: the consumer cannot move past it, so every message behind it waits too (head-of-line blocking).

    Scene: Scripted: one malformed message is delivered to a worker, fails and is not acknowledged; every second it is delivered again and fails again, alternating between the workers. The rest of the traffic flows normally.

    Key idea: A poison message fails on every delivery. Unlimited retries turn it into an infinite loop that costs capacity forever.

  3. 3. Failures eat capacity even when workers look idle

    A bad deploy makes 60% of all deliveries fail. Retries are still unlimited. Each failed message stays in flight until its 1 s timeout expires and, like a prefetch window, it keeps one of the worker’s slots for that whole second, although the 100 ms of work is long over.

    The effect is drastic: about 160 messages per second of capacity drops to roughly 10, half of the 20 that arrive. The backlog grows while the workers’ utilisation stays low: slots are reserved, CPUs are idle. A true poison message is worse still: it never succeeds, so it holds its slot forever.

    Scene: Sixty percent of all deliveries fail and are retried without limit. Failed messages hold worker slots until their timeout, so throughput collapses: the queue backlog grows steadily while worker utilisation stays low.

    Key idea: Failed deliveries cost a slot for the whole visibility timeout. Under unlimited retries, failures quietly turn into lost capacity and a growing backlog.

  4. 4. Cap deliveries, park the rest

    The queue now allows 3 deliveries. A message whose third delivery also fails is moved to a dead letter queue (DLQ) instead of being retried again. No message can cost more than three attempts, and a poison message leaves the main queue after its third failure.

    Watch what it does *not* fix: 60% of deliveries still fail, so the backlog still grows. Only messages that fail three times in a row (about 1 in 5 here) are parked. A DLQ is a safety valve for individual bad messages, not a repair for a broken consumer: that needs a rollback.

    Scene: The queue allows three deliveries. Messages that fail three times move to the Dead Letters queue, which slowly fills. Most deliveries still fail, so the main backlog keeps growing.

    Key idea: maxDeliveries bounds what any single message can cost; the DLQ keeps those messages for inspection instead of dropping or looping them.

  5. 5. A short outage floods the DLQ with good messages

    The deploy is rolled back: nothing fails. Then, half a second in, the downstream inventory service goes down for 3 s and every delivery fails. With a 1 s timeout and 3 deliveries, a message can only survive about 3 s of failures.

    The messages tried during the outage burn through their three deliveries and land in the DLQ, although nothing is wrong with them: they all would have succeeded a moment later. After the outage nothing new is dead-lettered and the backlog drains. Transient errors should be retried with backoff (or the consumers paused); only permanent errors belong in the DLQ.

    Scene: For three seconds every delivery fails. Messages tried during that window fail three times and move to the Dead Letters queue even though they are valid. When the dependency recovers, nothing more is dead-lettered and the main queue drains.

    Key idea: Separate transient from permanent failures. Size maxDeliveries × timeout (plus backoff) to outlast normal outages, or a dependency blip fills the DLQ.

  6. 6. Alert on the DLQ, fix, then redrive

    The same 3 s outage happens again, this time from the start. The DLQ fills and its alert fires (DLQ depth above zero, oldest message age). The on-call engineer checks the dead letters, sees they all failed with "inventory unavailable", and waits until the inventory service is healthy.

    At 5 s they start a redrive job that feeds the dead letters back through the fixed consumer code, here a worker reading the DLQ. Every parked message is processed and the DLQ empties. Redriven messages arrive late and may be duplicates, so the consumer must be idempotent, and a redrive should be rate-limited so it does not overload the system it just recovered.

    Scene: A three-second outage fills the Dead Letters queue again. At five seconds the redrive job starts, processes every parked message with the fixed code and the Dead Letters queue empties.

    Key idea: A DLQ is only useful if someone is paged by it, finds the cause, fixes it and redrives the messages.

  7. 7. Bounded retries, a watched DLQ

    Everything is healthy. Redelivery handles transient failures; a delivery limit keeps one bad message from costing capacity forever; the DLQ keeps those messages instead of dropping them; an alert and a redrive path turn them back into processed work.

    Classify errors in the consumer: retry transient ones with backoff, send permanent ones (validation, deserialisation) straight to the DLQ, and record why each message failed. A DLQ nobody watches is just a slower way to lose data.

    Scene: The Order Service publishes to the Order Events queue; two workers consume it, failed messages would move to Dead Letters after three deliveries, and the redrive job is idle. Nothing fails.

    Key idea: Retry what can succeed, park what cannot, and treat a non-empty DLQ as an incident.