Skip to the page

Messaging patterns · senior

Retries, ordering and dead-letter replay

Retries can break ordering, and replaying a dead-letter queue can double-pay or overload a provider. Plan both before an incident.

The notes

What you must know

  • Non-blocking retries (a retry topic or queue) take a failed event out of line. Later events for the same key are applied first, then the retried event overwrites newer state. Order status can go backwards.
  • Guarded update: UPDATE orders SET status = :s, seq = :seq WHERE id = :id AND seq < :seq. It is idempotent and ignores old events. Add a state machine that rejects impossible moves.
  • Other options: blocking retries for that consumer (the partition waits), or parking later events of a key while one is in retry.
  • Order must come from one owner. Timestamps from phones or different services cannot be trusted, but a per-order sequence set by the order service can.
  • Before replaying a dead-letter queue, group messages by error. A 503 from the provider is safe to replay. A timeout has an unknown result, so ask the provider for the status first. A data error (bad bank code) fails again, so route it to people.
  • Replay at a limited rate below the provider limit minus live traffic, live traffic first. Tools: SQS StartMessageMoveTask, a replay job for a Kafka dead-letter topic, a shovel or script for RabbitMQ.
  • Send the payout id to the bank as a reference and check your own payout state first, so a replay cannot pay twice.
  • Track the replay with a report that adds up to the full count. Next time: a circuit breaker so consumers stop taking messages while the provider is down, a longer back-off for server errors, an alarm on the dead-letter queue and a replay runbook.

The red pen

Common traps: mistake → fix

  • Mistake: Use one partition for order

    Fix: it kills throughput; use a version check.

  • Mistake: Kafka guarantees order, so it is a UI bug

    Fix: retries and multiple writers can break order.

  • Mistake: Redrive everything at once

    Fix: you overload the provider and may double-pay.

  • Mistake: Replay timeouts without checking

    Fix: they may already have succeeded.

Easy to hard

Questions interviewers ask, easy to hard

  1. Order status goes from Delivered back to Out for delivery after we added retries. Why?
  2. How do you replay 2 lakh messages from a dead-letter queue?
  3. How do you stop a bad message from blocking a partition?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview