Delayed Message Exchange

There is no need to deliver every message at once. Some scenarios may require an order to process after a specific amount of time, or the system may need a chance to complete a setup process.

LavinMQ delayed exchange accomplishes this without needing extra code by delivering the messages after a predetermined amount of time.

What is a Delayed Message Exchange?

LavinMQ delayed exchange creates a waiting period between the time a message reaches an exchange and its delivery to a queue. You can specify an offset in milliseconds every time you publish a message.

When to use the Delayed Message Exchange?

Instantaneous delivery is not always the best option when processing messages. Delayed Message Exchange simplifies cases where there is a hard waiting period, a setup process, or you are trying to ensure that customers read your text messages.

Delayed Message Exchange

There are many use cases where a message delay is necessary. LavinMQ allows you to hold onto messages for a finite amount of time before delivery through the delayed message exchange.

LavinMQ delayed exchange creates a waiting period between the time a message reaches an exchange and its delivery to a queue. It lets you mimic standard exchange types. Messages are routed based on a designated type after the specified waiting period. Create direct, fanout, or even a custom exchange and complex topologies without the need for multiple extra layers.

No information is lost, as acknowledgment and other features trigger on arrival in the destination queue.

Creating a Delayed Message Exchange

Declare the exchange using a special x-delay-type parameter and the x-delayed-message type:

channel.exchange_declare(exchange='test-exchange',
                                 exchange_type='x-delayed-message',
                                 arguments={"x-delayed-type": "fanout"})

The example exchange uses the fanout strategy. Messages reach each attached destination.

Add a delay to each message using the x-delay:

channel.basic_publish(exchange='test-exchange',
                              routing_key='test_route',
                              properties=pika.BasicProperties(
                                  headers={'x-delay': 2500}
                              ),
                              body='Hello World!')

The target queue attached to the delayed_route receives the message after 2500 milliseconds. Set the header for whichever delay is needed.

Checking if a message was delayed

It is possible to ensure that messages process after a given delay. The exchange negates and returns the value passed in x-delay.

The value of x-delay becomes -4000 if you tell the delayed exchange to wait for 4000 milliseconds. The header never drops.