<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by llya Myasnikov on Medium]]></title>
        <description><![CDATA[Stories by llya Myasnikov on Medium]]></description>
        <link>https://medium.com/@ektaros?source=rss-e1649eeb0f2d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*WuMyuK42v0q6MPVZDCB41A.jpeg</url>
            <title>Stories by llya Myasnikov on Medium</title>
            <link>https://medium.com/@ektaros?source=rss-e1649eeb0f2d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 17:20:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ektaros/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[RabbitMQ delayed messages without delayed plugin]]></title>
            <link>https://medium.com/@ektaros/rabbitmq-delayed-messages-without-delayed-plugin-94f0e65ea4b0?source=rss-e1649eeb0f2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/94f0e65ea4b0</guid>
            <category><![CDATA[rabbitmq]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[llya Myasnikov]]></dc:creator>
            <pubDate>Sun, 28 Nov 2021 16:01:33 GMT</pubDate>
            <atom:updated>2021-11-29T11:22:09.533Z</atom:updated>
            <content:encoded><![CDATA[<h3>RabbitMQ delayed messages without delayed message plugin</h3><p>Delaying messages in RabbitMQ is a very useful concept for various tasks. But the most common approach — <a href="https://github.com/rabbitmq/rabbitmq-delayed-message-exchange">RabbitMQ Delayed Message Plugin </a>— has several <a href="https://github.com/rabbitmq/rabbitmq-delayed-message-exchange#limitations">limitations</a>. In addition to already listed problems there are these:</p><ul><li>You cannot see the messages that are delayed. That means it is impossible to use metrics to track the amount of delayed messages, to investigate delayed messages and to delete them if you have to for some reason.</li><li>Messages stored in exchanges use RAM and many of such messages could cause performance problems.</li></ul><p>In this article we gonna build delayed messages system using only build-in RabbitMQ instruments: <a href="https://www.rabbitmq.com/tutorials/amqp-concepts.html#queues">queues</a> and <a href="https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchanges">exchanges</a>.</p><h3>RabbitMQ key concepts</h3><p>To build the topology for delaying our messages we need to use and understand these concepts:</p><ol><li><a href="https://www.rabbitmq.com/ttl.html"><strong>Time-To-Live and Expiration</strong></a><strong> (TTL)</strong></li><li><a href="https://www.rabbitmq.com/dlx.html"><strong>Dead Letter Exchanges</strong></a><strong> (DLX)</strong></li><li><a href="https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchange-headers"><strong>Headers exchange</strong></a></li><li><a href="https://www.rabbitmq.com/tutorials/tutorial-four-python.html#:~:text=We%20will%20use%20a%20direct%20exchange%20instead.%20The%20routing%20algorithm%20behind%20a%20direct%20exchange%20is%20simple%20-%20a%20message%20goes%20to%20the%20queues%20whose%20binding%20key%20exactly%20matches%20the%20routing%20key%20of%20the%20message."><strong>Message routing key</strong></a></li></ol><p>I will give a brief description of these concepts that should be enough to understand their role in incoming topology.</p><p><strong>TTL </strong>will give us an opportunity to set a timer on a message. After this timer expires the message will be dead lettered and RabbitMQ will send it into queue’s <strong>DLX.<br></strong>This solution will use per-queue <strong>TTL (message-ttl queue argument)</strong> to dead letter messages.</p><p><strong>DLX (dead-letter-exchange queue argument) </strong>is just and exchange name that the queue will send dead lettered messages to.</p><p><strong>Headers exchange </strong>is a type of an exchanges that uses information from headers (as opposed to direct exchange that uses routingKey) to determine to which queue it should send message to. <a href="https://www.cloudamqp.com/blog/part4-rabbitmq-for-beginners-exchanges-routing-keys-bindings.html#headers-exchange">It could be configured to use multiple key-value pairs in match-all or match-any fashion</a>.</p><p><strong>Message routing key</strong> is a property of a messages that is used by direct exchange to determine there to send it. Default exchange (exchange that sends messages to queues in the first place) is also a direct exchange and it expects queue name in message’s routing key property.</p><p>Now we are ready (I hope :) ) to see the topology!</p><h3><strong>RabbitMQ topology for delayed messages</strong></h3><p>First things first — it is impossible to implement delayed messages with different delays using only one additional queue and per-message TTL due to <a href="https://www.rabbitmq.com/ttl.html#per-message-ttl-caveats">this</a>. <br>tl;dr message cannot leave the queue while where are messages in front of it. So it creates problems then you have higher delay message prior to lower delay messages in one queue.</p><p>Here it is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V4QUtp59T_W1ZXBsoY3DLg.jpeg" /><figcaption>RabbitMQ topology for delayed messages</figcaption></figure><p>So let’s go through it step by step, it not that complicated after all :)</p><ol><li><strong>Publisher</strong> — your regular publisher that wants to publish a message to some destination queue with some delay. For it to work with this particular solution it should set header “delay” to desired delay, set routing key as desired queue name and publish this message to <strong>Headers exchange</strong>.</li><li><strong>Headers exchange</strong> — decides there to put our message based on its “delay” header. Should be binded to one “delay queue” for one “delay” value.</li><li><strong>Delay queues — </strong>queues with no consumers that will store delayed messaged until the delay expires. There must be one queue for each used delay. Queues must have <strong>message-ttl</strong> argument set to desired delay in ms and <strong>dead-letter-exchange </strong>argument set to an empty string (<strong>Default exchange</strong>). Could run in <a href="https://www.rabbitmq.com/lazy-queues.html">lazy mode</a> for significant delay values to save RAM usage.</li><li><strong>Default exchange — </strong>it is created by RabbitMQ by default and is used to route messages to queues using routing key. Important thing here is if you want to set <strong>Default exchange</strong> as your dead letter exchange you should set queue’s <strong>dead-letter-exchange </strong>argument to an empty string. <strong><br></strong>Here is the definition of <strong>Default exchange </strong>from RabbitMQ: “<em>The default exchange is implicitly bound to every queue, with a routing key equal to the queue name. It is not possible to explicitly bind to, or unbind from the default exchange. It also cannot be deleted</em>”</li><li><strong>Queues</strong> — your destination queue, could be anything.</li></ol><p>This approach allows us to send delayed messages by publishing them to headers exchange, setting desired queue name as routing key and setting the “delay” header to equal the desired delay.</p><h3>Node.js implementation</h3><p>You can see full code at once in <a href="https://github.com/Ektaros/delayedMessages">this GitHub repository</a>.</p><p>In this implementation we gonna use <a href="https://github.com/squaremo/amqp.node">amqplib</a>. I configure delays in minutes for better readability but it could be easily changed. <br>Let’s define <em>DelayedQueuesManager</em> constructor what takes an amqplib channel and many option as arguments and its future methods:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9f8e86dbdc64eee8eb02bd0d13c84fa/href">https://medium.com/media/c9f8e86dbdc64eee8eb02bd0d13c84fa/href</a></iframe><p><em>delayQueuePrefix</em> and <em>delayExchange</em> name could be anything it’s just names after all, but if you want to use same queues and exchange in different parts of your system make sure that names are the same (an option as well or there will be an error). Also if you want to use several different <em>delayQueuePrefix</em> and <em>delayExchange </em>in your project <strong>they must come in pairs</strong> or there will be binding duplication and this will cause duplication of messages.</p><p><em>delayQueueOptions </em>are your external queue options. <a href="https://www.rabbitmq.com/lazy-queues.html#configuration">Pass queueMode: “lazy” for less RAM usage</a>.</p><p>Now let’s write <em>setupDelayedTopology</em> method. It takes as an argument Set or Array of finite positive delays in minutes and creates headers exchange, delay queue for each provided delay and binds them together:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c24f9fdbda6b27fcdca4366ed58ba626/href">https://medium.com/media/c24f9fdbda6b27fcdca4366ed58ba626/href</a></iframe><p>To use a delay you have to first call this method with that to create or check exchange and corresponding delay queue. Configured delays are stored in “delays” property.<br><em>setupDelayedTopology</em> is reusable after some delays were already added. <br>To add more delays just pass new them to <em>setupDelayedTopology.</em></p><p>Now we have a way to create required topology to send messages with delay, let’s write the sending method itself. Everything is strait forward here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54c17bda630f712cc755d249d2702208/href">https://medium.com/media/54c17bda630f712cc755d249d2702208/href</a></iframe><p>Important note is that we are publishing a message into headers exchange and not into queue and routing key is not used by the exchange we are publishing in. It will be used later by Default exchange then the message will be dead lettered from delay queue.</p><p>And to close this up here is a quick usage example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/635db19588425e287c085f8d70710ff8/href">https://medium.com/media/635db19588425e287c085f8d70710ff8/href</a></iframe><p>This example will create 4 delay queues (for 0.5, 1, 5 and 30 minutes delays) and one headers exchange.</p><p>So as you can see it is possible and not at all complicated to implement delayed messaged in RabbitMQ without using delayed plugin — it could be done only using few dozens lines of code.</p><p><a href="https://github.com/Ektaros/delayedMessages">GitHub repo for those who scroll right down to the end</a>.</p><p>Hope you find this solution helpful. Thanks for reading :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94f0e65ea4b0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>