RabbitMQ delayed messaging.

Max Kotliar
formapro
Published in
2 min readAug 8, 2017

--

RabbitMQ does not support delayed messaging from out of the box. However, at the end of the “RabbitMQ redelivery pitfalls” post, we presented code snippets that do delaying either by using a mix of ttl and dead letter exchanges or by using the delay plugin.

But it had been before amqp interop emerged and some other changes had happened in the field. Among the other great changes we had added some useful methods to the producer interface, like setDeliveryDelay, setPriority, setTimeToLive.

Let’s say we need this: a message, that is delayed for five seconds, so no consumer immediately sees it, its time to live is one minute, after it, it is simply removed from a queue and additionally, we need a bit higher priority for this particular message. Here’s how you would do it if AMQP supported delayed messaging:

But it is not, so RabbitMQ throws DeliveryDelayNotSupportedException when setDeliveryDelay method is called. Surely, you can ignore it by catching the exception, if delaying messages is of no importance for you:

But what should do those who really need it? No worries guys, we took care of you. Here’s a solution: The AMQP producer accepts a delay strategy and if it’s set, it does not throw anything but instead it calls the strategy when it comes to message delaying. So, other than going into detail about delaying, just plugin the strategy in and continue hacking! In addition, delay strategies work with any amqp interop compatible transport, such as enqueue/amqp-ext, enqueue/bunny, enqueue/amqp-lib.

We used RabbitMqDlxDelayStrategy strategy but it is not the only one, there are others. You can use them by installing enqueue/amqp-tools package.

Above all, you are free to implement your own delay strategy. It might come in handy when you have to deal with big delays (days or weeks). Although the standard solutions can serve such delays, they are not the best choice when this is the case. It is better to use a scheduler or database to store messages till the time comes. I’ll give chapter and verse for how php-quartz scheduler can manage big delays.

--

--