Scheduling Messages in RabbitMQ


Scheduling in RabbitMQ can be done in two ways,
Using TTL and Dead Letter Exchange
Using Delay Exchange
1. Using TTL and Dead Letter Exchange:
Time-To-Live (TTL) is a parameter that specifies how long a message can remain in a queue. If the message is not consumed within that time, it is either discarded or moved to a Dead Letter Exchange (DLX), if one is configured.
In this method, message delay is achieved using:
One normal queue, called the Delay Queue
One Dead Letter Exchange is associated with a queue called the Final Queue
TTL is set for each message individually. Once the TTL expires, the message is forwarded to the Dead Letter Exchange. From there, it is routed to the final queue for processing. This setup allows message delivery to be delayed as required.
Create a Delay Queue along with a Dead Letter Exchange:
Sending Message to Queue with Delay:
2. Using Delay Exchange:
Delay Exchange is a special type of exchange that can be used with the help of the x-delayed-message-exchange plugin. In
this setup, a parameter called x-delay is set while sending a message to a queue associated with this exchange. The message is held internally by the RabbitMQ server until the specified delay time elapses. Once the delay time has passed, the message is delivered to the corresponding queue based on the routing key and can be consumed like a normal message.
The x-delay parameter accepts a value in milliseconds.
If the x-delay header is not set, the message is routed to the queue immediately.
Installation:
Download the .ez file for your corresponding RabbitMQ version from: https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases
Place the downloaded .ez file in the plugins folder where RabbitMQ is installed.
Enable the plugin using the following command:
Creation of Delay Exchange:
Sending a Message to Delay Exchange:
--------------------- THANKS FOR READING๐ ---------------------
Subscribe to my newsletter
Read articles from Aishwarya S directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
