Delayed messages

Delayed messages allow you to schedule messages for future delivery, enabling time-based workflows like scheduled tasks, reminders, or retries.

CloudAMQP’s hosted solutions, LavinMQ and RabbitMQ, support delayed messages through different approaches.

Delayed messages with LavinMQ

To configure a delayed exchange, you must set the x-delayed-type argument, which defines the underlying exchange type. LavinMQ supports all exchange types—direct, fanout, topic, or headers—enabling delayed functionality across various routing mechanisms. This flexibility ensures smooth integration with different exchange types.

You can set up a delayed exchange either through code or by using the LavinMQ management interface.

Example on LavinMQ.com

Delayed messages with RabbitMQ

Two common approaches for handling delayed messages in RabbitMQ are using message TTL (Time-To-Live) with dead-lettering exchanges or the RabbitMQ delayed message exchange plugin.

TTL and dead lettering

By combining the x-message-ttl and x-dead-letter-exchange properties, you can create a delayed messaging system. Messages are first published to a queue with a TTL (time-to-live). When the TTL expires, the message is rerouted to a specified dead-letter exchange, which then directs it to another queue for further processing, delaying it for the TTL duration.

Time-To-Live (TTL):

TTL defines how long a message remains in a queue before it expires. Using x-message-ttl , you can set a time limit for messages—if they’re not consumed within that time, they are either discarded or sent to the dead-letter exchange if one is configured.

Dead-Lettering:

Dead-lettering reroutes messages rejected, nacked, or expired to a specified exchange. You can use x-dead-letter-exchange to define the destination for messages that cannot be processed.

Step-by-step:

  • Declare the delayed queue:
    • Set the x-dead-letter-exchange argument property to the default exchange ""
    • Set the x-dead-letter-routing-key argument property to the name of the destination queue
    • Set the x-message-ttl argument property to the milliseconds you want to delay the message
  • Subscribe to the destination queue

Note: In RabbitMQ, messages can only be dead-lettered from the head of the queue. This means that if a message with a short TTL is positioned behind a message with a long TTL, it will be delayed longer than its specified TTL—it must wait for all messages ahead of it to be processed first.

RabbitMQ Delayed Message Exchange Plugin

The RabbitMQ delayed message exchange plugin extends RabbitMQ’s functionality by enabling support for delayed message handling.

This plugin lets you publish messages to an exchange with a specified delay. The plugin holds the message until the delay elapses and then routes it to the intended queue.

Before using this plugin, review its limitations, you can find them at GitHub.

GitHub - RabbitMQ Delayed Message Exchange

Differences and migration between RabbitMQ and LavinMQ

RabbitMQ's delayed message plugin and LavinMQ's delayed exchange are designed to handle delayed messaging. Although their core purpose is the same, differences in their implementations can affect aspects like message ordering and configuration.

Message ordering

In RabbitMQ, delayed messages managed by TTL-based expiration may not always expire in the desired order. For instance, when a message with a short TTL queues behind a message with a longer TTL, it experiences an additional delay because of its queue position. LavinMQ, however, maintains a delay-ordered queue that guarantees messages with shorter delays expire first, regardless of their arrival order.

Read more about LavinMQ delayed message exchange

Configuration and plugin

RabbitMQ, which requires a plugin installation and additional configuration for delayed messaging, LavinMQ includes this functionality as a built-in feature, making setup and maintenance simpler.