← All bricks

Go · Advanced

RabbitMQ Retry + Dead Letter Queue

Build a safe async pipeline in Go: a retry queue that delays redelivery, a cap on attempts, and a dead-letter queue so one poison message can't block the rest.

Async work is where messages go to get stuck. Here we build a RabbitMQ pipeline in Go that handles failure on purpose: a main queue, a retry queue that delays redelivery with a TTL, a cap on how many times a message is retried, and a dead-letter queue for the ones that never succeed. We declare the topology a piece at a time so the flow of a failing message stays clear — and one poison message can't wedge the whole worker.

What you'll build

  • Publish and consume messages with RabbitMQ in Go
  • Delay redelivery with a TTL-based retry queue
  • Cap retry attempts using the dead-letter count
  • Park unprocessable messages in a dead-letter queue

Contents

  1. An empty Go module
  2. A local RabbitMQ
  3. Connect and open a channel
  4. The main exchange
  5. The main queue, with an escape hatch
  6. The retry exchange
  7. The retry queue that waits, then returns
  8. The dead-letter queue
  9. Publish a message
  10. Consume and ack
  11. Reject a failure into the retry loop
  12. Cap the retries, then dead-letter