Delay Queue Pattern¶
The Delay Queue Pattern is a messaging design pattern used to defer the processing of a message until a specified time has elapsed^[001-TODO__code_copy.md]. In the provided implementation, this is achieved by utilizing a queue with a configured Time-To-Live (TTL)^[001-TODO__code_copy.md].
Mechanism¶
Messages are routed to a dedicated delay queue which is configured with a ttl parameter^[001-TODO__code_copy.md]. In the provided source code, the delay time is calculated as REPORT_CACHE_MINUTE * 60 * 1000^[001-TODO__code_copy.md].
Once the TTL expires, the message is not discarded; instead, it is automatically routed to a Dead Letter Exchange^[001-TODO__code_copy.md]. This exchange routes the message to a final processing queue (referred to as a "Dead Queue" in this context, though it functions as the active processing target)^[001-TODO__code_copy.md].
Implementation Details¶
The configuration defines a specific delay queue (plt.basic.report.delay.q) with the following characteristics^[001-TODO__code_copy.md]:
- TTL: Messages are held for the configured delay duration^[001-TODO__code_copy.md].
- Dead Letter Exchange: Upon TTL expiration, messages are moved to
plt.basic.report.dead.ex^[001-TODO__code_copy.md]. - Dead Letter Routing Key: The routing key is set to
plt.basic.report.dead.q^[001-TODO__code_copy.md].
Workflow¶
- Failure Handling: When a report task fails or requires a retry, it is sent to the delay queue (
DELAY_QUEUE_CHANNEL_Q)^[001-TODO__code_copy.md]. - Waiting: The message resides in the delay queue for the duration defined by the TTL^[001-TODO__code_copy.md].
- Processing: After the TTL expires, the message is transferred to the dead letter handling queue (
DEAD_QUEUE_Q), where a listener updates the report status to "FAIL"^[001-TODO__code_copy.md].
Sources¶
^[001-TODO__code_copy.md]
Related Concepts¶
- Message Queue
- [[Dead Letter Queue]]
- [[Retry Pattern]]
- RabbitMQ