Skip to content

RabbitMQ delayed queue pattern

The RabbitMQ delayed queue pattern is a mechanism used to defer the processing of a message until a specific time has elapsed. In the provided system, this pattern is implemented using RabbitMQ's Time-To-Live (TTL) and Dead Letter Exchange (DLX) features to manage report generation timeouts.^[001-todo-code.md]

Implementation Details

The core configuration for this pattern involves defining a primary "delay queue" with specific TTL arguments and a secondary "dead letter queue" to receive the expired message.^[001-todo-code.md]

  • Delay Queue (plt.basic.report.delay.q): This queue is configured with a x-message-ttl (set to 5 minutes) and a x-dead-letter-exchange pointing to the dead letter exchange.^[001-todo-code.md]
  • Dead Letter Queue (plt.basic.report.dead.q): This queue receives messages from the delay queue after the TTL expires or if the processing fails.^[001-todo-code.md]

Workflow in Context

In the report generation workflow, this pattern functions as a watchdog timer:

  1. Initiation: When a report record is created, a message is sent to the main processing queue (MONITOR_RUN_FAILURE_Q).^[001-todo-code.md]
  2. Transfer to Delay: If a consumer processes this message (specifically the registerReportFailListener), it immediately forwards the payload to the delay queue (DELAY_QUEUE_Q) to start the countdown.^[001-todo-code.md]
  3. Expiration: After the configured TTL (5 minutes) passes, the message expires from the delay queue and is routed to the Dead Letter Exchange (DEAD_QUEUE_EX).^[001-todo-code.md]
  4. Final Handling: A listener on the dead letter queue (handleFailedRecord) consumes the message and executes the final failure logic, such as updating the report status to "FAIL" with a timeout error message.^[001-todo-code.md]

Sources

^[001-todo-code.md]