If your messages need to be consumed with a delay, you can use the native message delay scheme of Message Queue for RabbitMQ. This scheme is easier to use than other message delay schemes supported by open source RabbitMQ.
What is a delayed message?
A delayed message is consumed with a specific delay after it is sent by a producer.
Delayed messages are used in the following scenarios:
- A time window between message production and consumption is required. For example, when a transaction order is created on an e-commerce platform, a producer sends a delayed message to the Message Queue for RabbitMQ broker. The message is used to ask a consumer to check whether the order is paid within a required period of time and close the order if it is unpaid. The broker delivers the message to the consumer 30 minutes after it receives the message. After the consumer receives the message, it checks whether the payment has been fulfilled. If the payment has not been fulfilled, the consumer closes the order. Otherwise, the consumer ignores the message.
- Messages are used to trigger scheduled tasks. For example, you can use a delayed message to trigger a scheduled task that sends notifications to users.
- The value of the delay for a message must be a non-negative integer. Unit: milliseconds.
- The maximum value is 86400000 (one day). If the delay for a message is set to a larger value, the message is considered a common message.
Comparison with other message delay schemes
The following table lists the message delay schemes supported by Message Queue for RabbitMQ and open source RabbitMQ.
|Scheme||Open source RabbitMQ||Message Queue for RabbitMQ|
|Dead-letter exchange+Message time-to-live (TTL) of a queue||✔️||✔️|
|Dead-letter exchange+TTL of a message||✔️||✔️
Notice To use this scheme, you must make sure that the queue to which a delayed message is sent has consumers.
|Native message delay scheme of Message Queue for RabbitMQ (recommended)||✖||✔️|
Native message delay scheme of Message Queue for RabbitMQ
- A producer sets the delay parameter for a message and sends the message to an exchange.
- The exchange routes the message to a queue.
- After the time specified by delay elapses, a consumer consumes the message from the queue.
Best practices of the native message delay scheme
- Producer client
The native message delay scheme of Message Queue for RabbitMQ is easy to use. You need only to set the delay parameter when you send a message from the producer client.
Java sample code:
Map<String, Object> headers = new HashMap<>(); headers.put("delay", "xx"); AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().messageId(UUID.randomUUID().toString()).headers(headers).build();
For more sample code in other programming languages, see AMQP Demos.
- Consumer client
To ensure the timeliness of delayed messages, we recommend that you use the basic.consume method in push mode instead of the basic.get method in pull mode when you consume messages. Message Queue for RabbitMQ stores messages in distributed mode. If you use the basic.get method in pull mode to obtain messages, you may reach a node where the messages are not stored.
- Why is the consumption of a delayed message not delayed?
Message TTL is configured for the queue from which the message is consumed. Message TTL for queues conflicts with the delay parameter provided by the native message delay scheme of Message Queue for RabbitMQ. In Message Queue for RabbitMQ, queues are used to delay messages. If you configure message TTL for a queue, Message Queue for RabbitMQ does not use the native message delay scheme.
- Why is the actual delay longer than the specified delay value?
You use the basic.get method in pull mode to obtain messages. Message Queue for RabbitMQ messages are stored in clusters. If the basic.get method in pull mode is used, the consumer may be routed to a Message Queue for RabbitMQ broker where no required messages are stored. In this case, the consumer cannot obtain messages from other Message Queue for RabbitMQ brokers in a timely manner.