{"id":8971,"date":"2020-11-09T04:44:01","date_gmt":"2020-11-09T08:44:01","guid":{"rendered":"http:\/\/staging.modulebazaar.com\/blog\/?p=8971"},"modified":"2020-11-11T01:30:05","modified_gmt":"2020-11-11T05:30:05","slug":"rabbitmq-magento2","status":"publish","type":"post","link":"https:\/\/www.modulebazaar.com\/blog\/rabbitmq-magento2\/","title":{"rendered":"Instrumental Role of RabbitMQ in Magento 2"},"content":{"rendered":"\n<h2><strong>Magento 2 RabbitMQ<\/strong><\/h2>\n\n\n\n<p>With the advent of RabbitMQ support in Magento 2, the asynchronous messaging capabilities of&nbsp;<strong><a href=\"https:\/\/www.egrovesys.com\/magento-development-services\" target=\"_blank\" rel=\"noreferrer noopener\">Magento development<\/a><\/strong>&nbsp;have now become full-fledged. Let\u2019s have a quick look at asynchronous messaging before we get to know how RabbitMQ works.<\/p>\n\n\n\n<h3>Asynchronous Messaging<\/h3>\n\n\n\n<p>Asynchronous Messaging is serial communication between two systems. The initiating system puts messages in a queue and proceeds to work on its other processes without waiting for an immediate response. Imagine a system where the sender and receiver need not be continually connected other than loose bindings based on rules of the message exchange. Just like how you would drop a mail in the postbox and the postman picks it up to eventually deliver to the addressee. One of the most popular asynchronous messaging protocols is the Advanced Message Queuing Protocol (AMQP).<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.modulebazaar.com\/blog\/wp-content\/uploads\/2020\/11\/RabbitMQ-for-asynchronous-messaging-4-1024x282.jpg\" alt=\"\" class=\"wp-image-8978\"\/><figcaption> <br>RabbitMQ-for-asynchronous-messaging <\/figcaption><\/figure>\n\n\n\n<p>Recently, we had run into a task of making our Magento application transmit information to multiple applications and return quickly to its mainstream work. We turned to RabbitMQ \u2013 the lightweight yet feature-rich messaging solution which could also be deployed across distributed networks. It worked like a charm and was able to reduce the burden on our core Magento application and took care of sending the messages to appropriate third-party applications. Soon it was helping our application scale-up and process 1,00,000 requests at ease from 25,000 earlier.<\/p>\n\n\n\n<h3><strong>What is this RabbitMQ and how does it work?<\/strong><\/h3>\n\n\n\n<p>RabbitMQ is a message broker which primarily uses AMQP for processing communications between 2 applications. It is a middleman where Queues can be defined and applications can connect to the Queues to pull the messages. Some tasks might be time-consuming. There would also be a need to handle multiple requests. The message broker puts these tasks into corresponding queues for the other application to process these independently.<\/p>\n\n\n\n<p>Message Queuing allows web servers to respond quickly to requests and delegate them to a third party for processing rather than perform the task instantaneously. This helps in handling multiple, resource-intensive requests efficiently.<\/p>\n\n\n\n<h3><strong>Components of RabbitMQ Architecture:<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.modulebazaar.com\/blog\/wp-content\/uploads\/2020\/11\/RabbitMQ-components-2-1024x436.jpg\" alt=\"\" class=\"wp-image-8979\"\/><figcaption> <br>RabbitMQ-components <\/figcaption><\/figure>\n\n\n\n<ul><li>Producer \u2013 The application or component which takes the requests from the users and pushes to the exchange. A request can either be a task, simple broadcast information or a file<\/li><li>Exchange \u2013 The Exchange takes the request and sends to the queues &nbsp;based on Exchange Type and rules for routing called \u2018Bindings\u2019<\/li><li>Queue \u2013 These queues hold on to the request\/information until the appropriate Subscriber requests for the information. &nbsp;The Queues are bound to exchanges identified by the \u2018Binding Key\u2019<\/li><li>Consumer \u2013 These are dedicated systems that pull information from the queue and process them independently<\/li><\/ul>\n\n\n\n<h3><strong>Types of Exchange:<\/strong><\/h3>\n\n\n\n<p><strong>Topic Exchange \u2013&nbsp;<\/strong>Messages are routed based on a wildcard search result of the routing key with the binding key.<\/p>\n\n\n\n<p><strong>Headers Exchange \u2013&nbsp;<\/strong>Messages are routed based on the Message Header with the Binding Header of the Queue.<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td><strong>Direct Exchange<\/strong><\/td><td>Messages are routed to the corresponding queue by exactly matching the binding key of the queue to the routing key of the message. This is the default exchange type of RabbitMQ.<\/td><\/tr><tr><td><strong>Fanout Exchange<\/strong><\/td><td>Messages are routed to all the queues bound to the exchange.<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h2><strong>RabbitMQ in Magento 2<\/strong><\/h2>\n\n\n\n<p>Magento 2 added asynchronous messaging capabilities with the implementation of RabbitMQ.&nbsp;<strong><a rel=\"noreferrer noopener\" href=\"https:\/\/www.modulebazaar.com\/modules\/magento2-extensions.html?mode=grid\" target=\"_blank\">Magento Commerce<\/a><\/strong> and Magento open-source use RabbitMQ to manage message queues. As of Magento release 2.3.0, RabbitMQ can also be used on Magento open source installations.<\/p>\n\n\n\n<p>Magento was using MySQL adapters for messaging and cron jobs to ensure delivery of messages. This wasn\u2019t reliable and scalable.<\/p>\n\n\n\n<p>RabbitMQ could typically be used in situations where Magento needs to communicate with an ERP System or a dedicated logistics application. In a real-world scenario, using RabbitMQ would allow us to decouple an ERP integration process from the customer order fulfilment process. For example, when Magento triggers an Order Creation event, we would just pass the order id (message) into the order export message queue (publisher) and let the order complete normally. Now RabbitMQ queues the order ids (messages) until another process (consumer) takes an order id (message) and processes that order in its ERP System. Use RabbitMQ to get back messages from the other systems into Magento 2. With the Consumer now running as a background process, we have great scalability, flexibility in runtime and better error handling.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><strong>Read also: <a href=\"https:\/\/www.modulebazaar.com\/blog\/5-best-magento-extensions-for-your-online-store\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"5 Of The Best Magento Extensions For Your Online Store  (opens in a new tab)\">5 Of The Best Magento Extensions For Your Online Store<\/a><\/strong><a href=\"https:\/\/www.modulebazaar.com\/blog\/5-best-magento-extensions-for-your-online-store\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"5 Of The Best Magento Extensions For Your Online Store  (opens in a new tab)\"> <\/a><\/p><\/blockquote>\n\n\n\n<h3><strong>Possibilities for Implementing RabbitMQ in Magento 2 are huge:<\/strong><\/h3>\n\n\n\n<p>A lot of scheduled tasks (cronjobs) in Magento can now be queued and processed asynchronously using RabbitMQ. With the growth of e-commerce and increasing volume of orders, message queuing helps by decoupling resource-intensive processes. Furthermore, this improves the frontend response time giving a better user experience.<\/p>\n\n\n\n<p>Now let\u2019s see how to set up RabbitMQ and establish an asynchronous communication between a Publisher and a&nbsp;<a href=\"https:\/\/www.cio.com\/article\/3395457\/study-shows-migrating-to-magento-commerce-drives-revenue-growth.html\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Consumer in Magento 2<\/strong>.<\/a><\/p>\n\n\n\n<h4><strong>Configure RabbitMQ<\/strong><\/h4>\n\n\n\n<p>Define the environment variables in the environment file located at \u2018app\/etc\/env.php\u2019<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'queue' => [\n\u00a0\u00a0\u00a0'amqp' => [\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'host' => '127.0.0.1',\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'port' => '5672',\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'user' => 'guest',\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'password' => 'guest',\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'virtualhost' => '\/',\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'ssl' => false\n\u00a0\u00a0\u00a0]\n]<\/code><\/pre>\n\n\n\n<h4><strong>Setup the channel and type of message to communicate:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\"communication.xml\"\n\n&lt;topic name=\"sample_topic_name\" request=\"string\"\/><\/code><\/pre>\n\n\n\n<h4><strong>Setup the exchange and bind the topic to the queue with a Binding Key:<\/strong><\/h4>\n\n\n\n<p>Interestingly, we have taken the TOPIC exchange type for example which is more flexible and adds scalability to the application.<\/p>\n\n\n\n<p><strong>File: &nbsp;&nbsp;&nbsp;&nbsp;queue_topology.xml<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;exchange name=\"sample_exchange\" type=\"topic\" connection=\"amqp\" autoDelete=\"true\" durable=\"true\" >\n\n\u00a0\u00a0\u00a0&lt;!-- publisher -->\n\n\u00a0\u00a0\u00a0&lt;binding id=\"bindingid_publisher\" \u00a0\u00a0topic=\"sample_topic_name\" \u00a0destinationType=\"queue\" destination=\"queue-name\"\/\n\n&lt;\/exchange><\/code><\/pre>\n\n\n\n<h4><strong>Set up the publisher and link to an exchange:<\/strong><\/h4>\n\n\n\n<p><strong>File: &nbsp;&nbsp;&nbsp;queue_publisher.xml<\/strong><\/p>\n\n\n\n<p><em>&lt;<\/em><em>publisher&nbsp;<\/em><em>topic<\/em><em>=\u201dsample_topic_name\u201d<\/em><em>&gt;<\/em><\/p>\n\n\n\n<p><em>&lt;<\/em><em>connection&nbsp;<\/em><em>name<\/em><em>=\u201damqp\u201d&nbsp;<\/em><em>exchange<\/em><em>=\u201dsample_exchange\u201d<\/em><em>\/&gt;<\/em><\/p>\n\n\n\n<p><em>&lt;\/<\/em><em>publisher<\/em><em>&gt;<\/em><\/p>\n\n\n\n<p>As a matter of factly, we have now completed establishing the exchange and publisher, we will now setup the consumer side of communication channel.<\/p>\n\n\n\n<h4><strong>Consumer Queue:<\/strong><\/h4>\n\n\n\n<p><strong>File: &nbsp;&nbsp;communication.xml<\/strong><\/p>\n\n\n\n<p><em>&lt;<\/em><em>topic&nbsp;<\/em><em>name<\/em><em>=\u201dsample_topic_name\u201d&nbsp;<\/em><em>request<\/em><em>=\u201dstring\u201d<\/em><em>\/&gt;<\/em><\/p>\n\n\n\n<p><strong>File: &nbsp;queue_topology.xml<\/strong><\/p>\n\n\n\n<p><em>&lt;<\/em><em>exchange&nbsp;<\/em><em>name<\/em><em>=\u201dsample_exchange\u201d&nbsp;<\/em><em>type<\/em><em>=\u201dtopic\u201d&nbsp;<\/em><em>connection<\/em><em>=\u201damqp\u201d&nbsp;<\/em><em>autoDelete<\/em><em>=\u201dtrue\u201d&nbsp;<\/em><em>durable<\/em><em>=\u201dtrue\u201d&nbsp;<\/em><em>&gt;<\/em><\/p>\n\n\n\n<p><em>&nbsp;&nbsp;&nbsp;<\/em><em>&lt;!\u2013 consumer \u2013&gt;<\/em><\/p>\n\n\n\n<p><em>&nbsp;&nbsp;&nbsp;<\/em><em>&lt;<\/em><em>binding&nbsp;<\/em><em>id<\/em><em>=\u201dbindingid_consumer\u201d&nbsp;<\/em><em>topic<\/em><em>=\u201dsample_topic_name\u201d&nbsp;<\/em><em>destinationType<\/em><em>=\u201dqueue\u201d&nbsp;<\/em><em>destination<\/em><em>=\u201dqueue-name\u201d<\/em><em>\/&gt;<\/em><\/p>\n\n\n\n<p><em>&lt;\/<\/em><em>exchange<\/em><em>&gt;<\/em><\/p>\n\n\n\n<p><strong>File: &nbsp;&nbsp;&nbsp;queue_consumer.xml<\/strong><\/p>\n\n\n\n<p><em>&lt;<\/em><em>consumer&nbsp;<\/em><em>name<\/em><em>=\u201dconsumer-name\u201d&nbsp;<\/em><em>queue<\/em><em>=\u201dqueue-name\u201d&nbsp;<\/em><em>connection<\/em><em>=\u201damqp\u201d&nbsp;<\/em><em>handler<\/em><em>=\u201dclassName methodname\u201d&nbsp;<\/em><em>consumerInstance<\/em><em>=\u201dMagento\\Framework\\MessageQueue\\BatchConsumer\u201d<\/em><em>\/&gt;<\/em><em>::<\/em><\/p>\n\n\n\n<h4><strong>Using the Publisher to push a message in Magento:<\/strong><\/h4>\n\n\n\n<p><em>use&nbsp;<\/em><em>Magento\\Framework\\MessageQueue\\PublisherInterface;<\/em><\/p>\n\n\n\n<p><em>public function&nbsp;<\/em><em>__construct(<\/em><\/p>\n\n\n\n<p><em>&nbsp;&nbsp;&nbsp;PublisherInterface&nbsp;<\/em><em>$publisher<\/em><\/p>\n\n\n\n<p><em>) {<\/em><\/p>\n\n\n\n<p><em>&nbsp;&nbsp;&nbsp;<\/em><em>$this<\/em><em>-&gt;<\/em><em>publisher&nbsp;<\/em><em>=&nbsp;<\/em><em>$publisher<\/em><em>;<\/em><\/p>\n\n\n\n<p><em>}<\/em><\/p>\n\n\n\n<p><em>public function&nbsp;<\/em><em>publishData()<\/em><\/p>\n\n\n\n<p><em>{<\/em><\/p>\n\n\n\n<p><em>$message = \u201ctest message\u201d;<\/em><\/p>\n\n\n\n<p><em>$this<\/em><em>-&gt;<\/em><em>publisher<\/em><em>-&gt;publish(<\/em><em>\u2018<\/em><em>&nbsp;sample_topic_name<\/em><em>&nbsp;\u2018<\/em><em>,&nbsp;<\/em><em>$message<\/em><em>);<\/em><\/p>\n\n\n\n<p><em>}<\/em><\/p>\n\n\n\n<h4><strong>Run the Consumer from Terminal:<\/strong><\/h4>\n\n\n\n<p><em>bin\/magento queue:consumers:list<\/em><\/p>\n\n\n\n<p><em>bin\/magento queue:consumers:start&nbsp;<\/em><em>[<\/em><em>\u2013max-messages<\/em><em>=<\/em><em>&lt;value&gt;]&nbsp;<\/em><em>[<\/em><em>\u2013batch-size<\/em><em>=<\/em><em>&lt;value&gt;]&nbsp;<\/em><em>[<\/em><em>\u2013pid-file-path<\/em><em>=<\/em><em>&lt;value&gt;]&nbsp;<\/em><em>[<\/em><em>\u2013area-code<\/em><em>=<\/em><em>&lt;value&gt;] &lt;consumer_name&gt;<\/em><\/p>\n\n\n\n<p>Hope we were able to briefly explain about implementing RabbitMQ in Magento 2. The possibilities are now endless with the inclusion of RabbitMQ in Magento 2. Should you have any queries or feedback, please reach us on <strong><a href=\"https:\/\/www.modulebazaar.com\/contacts\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"modulebazaar (opens in a new tab)\">modulebazaar<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 RabbitMQ With the advent of RabbitMQ support in Magento 2, the asynchronous messaging capabilities of&nbsp;Magento development&nbsp;have now become full-fledged. Let\u2019s have a quick look at asynchronous messaging before we get to know how RabbitMQ works. Asynchronous Messaging Asynchronous Messaging is serial communication between two systems. The initiating system puts messages in a queue and proceeds to work on its other processes without waiting for an immediate response. Imagine a system where the sender and receiver need not be continually connected other than loose bindings based on rules of the message exchange. Just like how you would drop a mail in the postbox and the postman picks it up to eventually deliver to the addressee. One of the most popular asynchronous messaging protocols is the Advanced Message Queuing Protocol (AMQP). Recently, we had run into a task of making our Magento application transmit information to multiple applications and return quickly to its mainstream work. We turned to RabbitMQ \u2013 the lightweight yet feature-rich messaging solution which could also be deployed across distributed networks. It worked like a charm and was able to reduce the burden on our core Magento application and took care of sending the messages to appropriate [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":9024,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1100],"tags":[3337,3421],"yst_prominent_words":[1283,3426,3428,3425,3430,3424,1282,1392,1281,1807,3429,1812,3434,3427,3433,3431,3432,3423,3422,1270],"_links":{"self":[{"href":"https:\/\/www.modulebazaar.com\/blog\/wp-json\/wp\/v2\/posts\/8971"}],"collection":[{"href":"https:\/\/www.modulebazaar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modulebazaar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modulebazaar.com\/blog\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modulebazaar.com\/blog\/wp-json\/wp\/v2\/comments?post=8971"}],"version-history":[{"count":1,"href":"https:\/\/www.modulebazaar.com\/blog\/wp-json\/wp\/v2\/posts\/8971\/revisions"}],"predecessor-version":[{"id":9026,"href":"https:\/\/www.modulebazaar.com\/blog\/wp-json\/wp\/v2\/posts\/8971\/revisions\/9026"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modulebazaar.com\/blog\/wp-json\/wp\/v2\/media\/9024"}],"wp:attachment":[{"href":"https:\/\/www.modulebazaar.com\/blog\/wp-json\/wp\/v2\/media?parent=8971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modulebazaar.com\/blog\/wp-json\/wp\/v2\/categories?post=8971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modulebazaar.com\/blog\/wp-json\/wp\/v2\/tags?post=8971"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.modulebazaar.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=8971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}