The dead-letter redrive that reopened a canceled order
Failure story 07, in events and replay.
The challenge
Section titled “The challenge”An operator fixed a bug and redrove one failed message from the dead-letter queue. Why is an order the customer canceled yesterday open again, and on its way to them?
The setup
Section titled “The setup”| Assumption | Value |
|---|---|
| Producer | The order service publishes OrderCreated and OrderCanceled |
| Consumer | The fulfillment service keeps its own copy of each order’s status and ships orders that are open |
| Failure handling | A message that fails 5 times goes to a dead-letter queue (DLQ) |
| Consumer’s update | Each event sets the status: OrderCreated sets open, OrderCanceled sets canceled, creating the record if it doesn’t exist |
What happened
Section titled “What happened”| Step | What happens | Fulfillment’s status for O-77 |
|---|---|---|
| 1 | OrderCreated(O-77) fails five times, because of a parsing bug with one address format, and moves to the DLQ. |
none |
| 2 | The customer cancels. OrderCanceled(O-77) is processed fine. |
canceled |
| 3 | A day later, the bug is fixed. An operator selects the DLQ and clicks Redrive. | canceled |
| 4 | OrderCreated(O-77) is processed and sets the status to open. |
open |
| 5 | The nightly pick list includes O-77. The parcel ships. | open |
Why it went wrong
Section titled “Why it went wrong”A dead-letter queue takes a message out of order and holds it while newer messages for the same order are processed. Redrive puts it back as if it were new. A consumer that blindly applies whatever it receives treats a day-old fact as the latest one, and the order goes backwards in its lifecycle.
The DLQ didn’t cause the bug; it made the old ordering assumption visible. Retries with backoff, multiple partitions and replays from backup all do the same thing.
Two designs
Section titled “Two designs”| Aspect | Design A: blind redrive | Design B: version-aware transitions |
|---|---|---|
| Events carry | The new status | The new status and the order’s version, a number the producer increments with every change |
| Consumer stores | Status | Status and the last version applied |
| Old event arrives | Applied | Skipped if its version isn’t newer, and recorded as skipped with the reason |
| Lifecycle rule | Any status can follow any status | Only allowed transitions: canceled is final |
| What the operator sees | Redrive succeeded | 1 message redriven; O-77 skipped: version 1 is older than applied version 2 (canceled) |
on event(orderId, version, newStatus): current = load(orderId) if current and version <= current.version: log_skip(orderId, version, current) # visible to the operator return if current and not allowed(current.status, newStatus): log_rejected_transition(orderId, current.status, newStatus) return save(orderId, newStatus, version)Either guard alone catches this case; together they cover more. The version catches stale events in general; the lifecycle rule catches a producer bug that sends a valid-looking but impossible transition.
The check
Section titled “The check”- Redrive an old event. With O-1 canceled at version 2, redeliver
OrderCreated(O-1, version 1). Expected: O-1 stays canceled, it isn’t on the pick list, and the redrive report explains why it was skipped. - Redrive a genuinely missing event. With no record of O-2, redeliver
OrderCreated(O-2, version 1). Expected: O-2 becomes open. - Any order. Deliver versions 1, 2 and 3 of an order in all six orders. Expected: the same final state every time.
This check is in the checks catalog as test templates in Java and Python.
When the simpler design is enough
Section titled “When the simpler design is enough”- Each event is independent of every other: a page view, a sensor reading, a log line.
- Messages for one entity are strictly ordered and failures block the entity’s later messages instead of skipping ahead. That avoids the reordering, at the cost of one bad message stopping everything behind it.
For educators
Section titled “For educators”Discussion question. Who should be allowed to redrive a DLQ, and what should they see before they press the button?
Exercise. OrderCreated carries the delivery address, and OrderCanceled doesn’t. Under Design B, O-77’s record was created from the cancellation, so it has no address. When the old OrderCreated is skipped, the address is never stored. Support needs it to handle the customer’s refund. What do you change?
Worked answer
Skipping the whole event is too blunt: the status in it is stale, but the address is information the consumer never had. Two options. Version each part of the record separately, so the consumer can take the address from version 1 while keeping the status from version 2. Or, when an event is skipped or out of order, fetch the order’s current state from the order service and store that. The second is simpler and is the usual answer when the producer can serve its current state. Either way, decide it per field and write it down; it’s the kind of rule that otherwise gets decided in an incident.
Further reading
Section titled “Further reading”- Amazon’s documentation on dead-letter queues and redrive (opens in a new tab) in SQS.
Related pages
Section titled “Related pages”- The cancellation that arrived before the booking, the same mechanism with commands
- Messaging
- Event-driven architecture
Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.