Retries, poison messages and DLQs
We decide what a consumer does when processing fails. Some failures pass if you wait, some never will, and telling them apart decides whether retries help or hurt. We will cover backoff with jitter, poison messages and dead-letter queues, and the reordering that retries cause.
#Section 10Retries, backoff and jitter
Most job failures are temporary. The email provider returns 503 for a minute, the kitchen API times out during a deploy, the database fails over. Retrying later usually succeeds. Retrying immediately usually fails again, and if every worker retries immediately, the struggling service receives a burst of retries exactly when it is least able to handle them.
Exponential backoff waits longer after each failure. Wren’s workers wait 1 s before the second attempt, 2 s before the third, then 4, 8, 16, 32, 64 and 128 s, eight retries spanning up to 255 s, a little over four minutes. Waits usually also have a cap, such as 5 minutes, so a job that fails twenty times does not wait hours between attempts.
Jitter randomizes each wait. Without it, a thousand jobs that failed at the same moment, because the provider blipped, all retry at exactly 1 s, then 3 s, then 7 s, arriving in synchronized waves. AWS’s architecture blog compared strategies in 2015 and recommended full jitter, where each wait is a random value between zero and the current cap. The retries smear into a falling trickle that a recovering service can absorb.
Not every failure deserves a retry. A 400 Bad Request, a 404 for a deleted restaurant, a JSON parse error, or a validation failure will fail the same way every time. Retrying them wastes capacity and delays the dead-letter queue that would bring them to a human’s attention. Wren’s workers classify errors, the same way Unit V classified API errors. Network errors, timeouts, 429 and 5xx responses are retried. 4xx responses other than 429 and any deserialization or validation error go straight to the dead-letter queue.
Retries have a budget at the system level too. If 10% of requests to a service fail and every caller retries three times, the service receives up to 1.3 times its normal traffic when it is already in trouble. Unit X covers retry budgets and circuit breakers, which stop retries entirely when a dependency is clearly down. In job systems the queue itself absorbs much of this, since a failed job simply waits, but the same logic applies to how fast workers come back.
Brokers implement delayed retries differently. SQS uses the visibility timeout, since a message that is not deleted reappears after it, and ChangeMessageVisibility sets the delay per message. RabbitMQ uses a delayed-message exchange or a chain of retry queues with message TTLs that dead-letter back into the main queue. Kafka has no per-message delay, so teams use separate retry topics, such as order-events-retry-1m and order-events-retry-10m, each consumed with a delay.
#Section 11Poison messages and dead-letter queues
A poison message is one that fails every time it is processed. It might be malformed JSON from a buggy producer, an event for a restaurant that was deleted, a payload that triggers a bug in the consumer, or a message larger than the consumer can handle. Retrying it forever has two costs. It wastes worker time on every attempt, and in brokers that deliver in order, it blocks everything behind it, the classic head-of-line blocking.
A dead-letter queue, DLQ, is where messages go after too many failures. In SQS, a redrive policy sends a message to the DLQ once its receive count passes maxReceiveCount, 5 for Wren’s kitchen jobs. RabbitMQ dead-letters a message when it is rejected without requeue, expires, or exceeds a delivery limit. Kafka has no built-in DLQ, so consumers publish failed records to a dead-letter topic themselves and commit the offset to move on.
A DLQ is not a bin. Every message in it represents work that did not happen, a customer who did not get a receipt, a kitchen that never saw an order. Wren alerts when any message lands in a DLQ, records the error and stack trace with the message, and gives the on-call engineer a tool to inspect messages and redrive them, sending them back to the main queue after the cause is fixed. SQS has a built-in redrive. For Kafka, a small replay tool reads the dead-letter topic and republishes.
Redrive is safe only if consumers are idempotent, since some messages in the DLQ may have partially succeeded before failing. It also needs care with ordering and staleness. A “courier is 5 minutes away” notification redriven an hour later is worse than none, so some messages are better discarded with a log entry.
Consumers also need a guard against the poison that crashes the whole process, such as a payload that triggers an out-of-memory error. The process dies before it can count the failure, the message is redelivered, and it kills the next worker too. Receive counts kept by the broker, as SQS and RabbitMQ quorum queues do, catch this, while counters kept in the worker’s memory do not.
#Section 12Ordering under retries
Retries break ordering, and the break is easy to miss. Order 124 produces three events in sequence, placed, paid and cancelled, the last because the customer changed their mind. The consumer handles “placed”. “Paid” fails because the database blipped and goes to a retry delay. “Cancelled” succeeds. Then “paid” is retried and succeeds. The consumer’s final state for order 124 is “paid”, after the order was cancelled.
Three approaches handle this, and they trade throughput for strictness. The first is strict per-key ordering. Process each key’s messages one at a time, and when one fails, block the rest of that key’s messages until it succeeds or is dead-lettered. Kafka gives this per partition, and SQS FIFO per message group. The cost is that a stuck message blocks its key, and with Kafka, blocks every key in the partition unless the consumer tracks keys itself.
The second is to make consumers order-tolerant. Each event carries a version or sequence number per entity, here 1 for placed, 2 for paid, 3 for cancelled. The consumer stores the last version it applied and ignores anything older. When “paid”, version 2, arrives after “cancelled”, version 3, it is discarded. This is the same conditional-update idea from Part III, and it lets retries and parallelism happen freely. It works when each event carries enough state that skipping an older one loses nothing.
The third is to model the domain as a state machine whose transitions refuse impossible moves. A cancelled order cannot become paid, so the consumer rejects the transition and logs it. This is less general than versions and often closer to the business rules, and it catches bugs in producers as well as reordering.
Wren uses versions for its read models, such as the kitchen board and the order history, and state machine checks in the order service itself. It avoids strict blocking except where the business demands it, such as ledger entries, where Kafka’s per-partition order and a single consumer per partition are worth the throughput cost.