<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ rabbitmq - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ rabbitmq - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Mon, 25 May 2026 22:38:23 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/rabbitmq/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Create Multiple Virtual Hosts on a Single RabbitMQ Instance ]]>
                </title>
                <description>
                    <![CDATA[ By Ridwan Yusuf Hi, friends, and welcome to another tutorial. We'll be talking about RabbitMQ, a tool that is widely used as a message broker in distributed and Pub-Sub systems.  While multiple applications can share a single RabbitMQ instance, it's ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-multiple-virtual-hosts-on-one-rabbitmq-instance/</link>
                <guid isPermaLink="false">66d460c5d7a4e35e384349a7</guid>
                
                    <category>
                        <![CDATA[ distributed systems ]]>
                    </category>
                
                    <category>
                        <![CDATA[ message broker ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ rabbitmq ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 26 Jul 2023 21:25:15 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/07/Untitled-design.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Ridwan Yusuf</p>
<p>Hi, friends, and welcome to another tutorial. We'll be talking about RabbitMQ, a tool that is widely used as a message broker in distributed and Pub-Sub systems. </p>
<p>While multiple applications can share a single RabbitMQ instance, it's a great approach to configure it to have different virtual hosts for each application. </p>
<p>One practical usage in a Python application is using it as a Celery broker URL or simply as a message queue.</p>
<p>One benefit of this is that it saves the costs that comes with spinning up multiple instances of RabbitMQ. Creating RabbitMQ virtual hosts is similar both locally and when running in a self-managed solution such as AWS RabbitMQ</p>
<p>By the end of this guide, you will be able to:</p>
<ol>
<li>Create multiple virtual hosts for a single RabbitMQ instance.</li>
<li>Test the connections using the correct credentials, that is user, password, and virtual host.</li>
</ol>
<p><strong>Prerequisites to follow along</strong>:</p>
<ol>
<li>Ensure you have Docker and Docker Compose installed, or</li>
<li>Have a self-managed RabbitMQ instance from AWS, CloudAMQP, or any other service provider</li>
</ol>
<p>Without any delay, let’s get started</p>
<h2 id="heading-how-to-run-a-rabbitmq-instance-locally">How to Run a RabbitMQ Instance Locally</h2>
<p>To keep things simple, we are going to run the RabbitMQ instance in a Docker container.</p>
<p>Create a file named docker-compose.yml and update it with the following content:</p>
<p><em>docker-compose.yml</em></p>
<pre><code class="lang-yaml"><span class="hljs-attr">version:</span> <span class="hljs-string">"3.8"</span>
<span class="hljs-attr">services:</span>
  <span class="hljs-attr">rabbitmq:</span>
    <span class="hljs-attr">image:</span> <span class="hljs-string">rabbitmq:3.8-management-alpine</span>
    <span class="hljs-attr">container_name:</span> <span class="hljs-string">'rabbitmq_test'</span>
    <span class="hljs-attr">environment:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">RABBITMQ_DEFAULT_USER=sampleuser</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">RABBITMQ_DEFAULT_PASS=samplepassword</span>
    <span class="hljs-attr">ports:</span>
        <span class="hljs-bullet">-</span> <span class="hljs-number">5672</span><span class="hljs-string">:5672</span>
        <span class="hljs-bullet">-</span> <span class="hljs-number">15672</span><span class="hljs-string">:15672</span>
    <span class="hljs-attr">volumes:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq</span>
</code></pre>
<p>To start the container, run this command in the terminal:</p>
<pre><code class="lang-yaml"><span class="hljs-string">docker-compose</span> <span class="hljs-string">up</span>
</code></pre>
<p>Once the container is up and running, access the GUI by visiting this link: <a target="_blank" href="http://localhost:15672/">http://localhost:15672/</a></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/07/ezgif-2-5cc0fa8243.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>RabbitMQ GUI login page</em></p>
<p>Use the credentials specified in the docker-compose file to gain access to the dashboard – “sampleuser” and “samplepassword” in my case.</p>
<p><strong>Note:</strong> If you are using a self-managed RabbitMQ instance from AWS or CloudAMQP, the RabbitMQ connection string will look similar to this:</p>
<p>amqps://ausername:<a target="_blank" href="mailto:apassword@32wewjijiokkoo.mq.eu-west-3.amazonaws.com">apassword@32wewjijiokkoo.mq.eu-west-3.amazonaws.com</a>:5671</p>
<p>To access the RabbitMQ admin dashboard, follow these steps:</p>
<ol>
<li>Visit the URL using the HTTPS protocol: <a target="_blank" href="https://32wewjijiokkoo.mq.eu-west-3.amazonaws.com">https://32wewjijiokkoo.mq.eu-west-3.amazonaws.com</a></li>
<li>Remove the username, password, and port from the URL.</li>
<li>After accessing the URL, the system will prompt you to enter the username and password specified in the RabbitMQ connection string (“ausername” and “apassword” in this case). Provide these credentials to log in to the admin dashboard.</li>
</ol>
<h2 id="heading-how-to-create-a-virtual-host">How to Create a Virtual Host</h2>
<p>Once you are logged in, click on the ‘Admin’ tab, and then navigate to the ‘Virtual Hosts’ tab as shown in the image below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/07/ezgif-4-82232fd2b0.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>RabbitMQ Virtualhost tab</em></p>
<p>Click on ‘Add a new virtual host’, and provide it with an appropriate name. Finally, click on the ‘Add virtual host’ button to create the new virtual host.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/07/ezgif-4-26e9083f62.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>New Virtual Host created</em></p>
<h2 id="heading-how-to-create-a-new-user">How to Create a New User</h2>
<p>On the Users tab, click on the ‘Add a user’ toggle. Specify a Username and Password for the new user. If you wish, you may grant the user access to the Admin (GUI) interface. Finally, click on the ‘Add user’ button to create the new user.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/07/ezgif-4-e787f0c102.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Create a new user</em></p>
<h2 id="heading-how-to-assign-the-new-user-to-the-created-virtual-host-vhost">How to Assign the New User to the Created Virtual Host (Vhost)</h2>
<p>On the Users tab, locate and click on the previously created user, which was named ‘user1’ in my case.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/07/ezgif-4-cb6b620bdf.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>New user in users list</em></p>
<p>For the new user, choose the virtual host as ‘new-virtual-host’, and then click the ‘Set permission’ button. This grants the user access to the specified virtual host. To confirm this, go back to the Users tab and verify that the user now appears with access to the ‘new-virtual-host'</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/07/WhatsApp-Image-2023-07-25-at-19.59.44.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>Assign new user to the created virtual host</em></p>
<p>By now, the new user 'user1' has been configured to access the vhost 'new-virtual-host'.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/07/Screenshot-from-2023-07-26-06-22-12.png" alt="Image" width="600" height="400" loading="lazy">
<em>New user assigned to the created virtual host</em></p>
<h2 id="heading-how-to-test-the-connection-for-the-new-user-and-vhost">How to Test the Connection for the New User and Vhost</h2>
<p>To test if the new user can access the new vhost, you can use the following sample Python script. Once everything is working correctly, you can use the URL string to connect to the RabbitMQ broker in any application.</p>
<p>_test<em>rabbit.py</em></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> pika

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_rabbitmq_url</span>(<span class="hljs-params">url</span>):</span>

    <span class="hljs-keyword">try</span>:
        params = pika.URLParameters(url)
        connection = pika.BlockingConnection(params)
        connection.close()
        print(<span class="hljs-string">"Connection successful!"</span>)

    <span class="hljs-keyword">except</span> pika.exceptions.AMQPError <span class="hljs-keyword">as</span> e:
        print(<span class="hljs-string">"Connection failed:"</span>, e)

test_rabbitmq_url(<span class="hljs-string">'amqp://user1:password@localhost:5672/new-virtual-host'</span>)
</code></pre>
<p>Note how we have specified the RabbitMQ URL: it has the username as “user1” and password as “password9,” while the virtual host is named ‘new-virtual-host.’</p>
<p>Remember to install pika by running <code>pip install pika</code> on the command line.</p>
<p>To run this script on the terminal, enter the following command:</p>
<pre><code class="lang-bash">python test_rabbit.py
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/07/ezgif-4-4dda3c97ef.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Successfully connected to RabbitMQ</em></p>
<p>This indicates a successful connection to the RabbitMQ instance.</p>
<p>Change the password in the utility function to an incorrect one, and you should see an outcome similar to this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/07/ezgif-4-85582a7c7f.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Connection to RabbitMQ failed</em></p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>In conclusion, creating multiple virtual hosts on a single RabbitMQ instance can significantly enhance your message queue management capabilities. </p>
<p>By effectively segregating applications and resources, you ensure better organization, security, and scalability. With the step-by-step guide provided in this article, you now have the knowledge to harness the full potential of RabbitMQ</p>
<p>Thanks for reading, and I hope you enjoyed the article.</p>
<p>For more interesting content and tips, please feel free to check out my video collection on <a target="_blank" href="https://www.youtube.com/@ridwanray">YouTube</a> and consider hitting the subscribe button.</p>
<p>You can also find me on <a target="_blank" href="https://linkedin.com/in/ridwan-yusufa/">LinkedIn</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ An introduction to RabbitMQ, a broker that deals in messages ]]>
                </title>
                <description>
                    <![CDATA[ By Chandrabhan Singh An introduction to RabbitMQ, message broker, AMQP model and more. In distributed systems, communication between various applications plays an important role. Effectively passing messages between applications was always a crucial ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/rabbitmq-9e8f78194993/</link>
                <guid isPermaLink="false">66d45dd936c45a88f96b7cc3</guid>
                
                    <category>
                        <![CDATA[ message broker ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ queue ]]>
                    </category>
                
                    <category>
                        <![CDATA[ rabbitmq ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 05 Apr 2019 20:42:10 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*HCpBJmTd_sELllvhVOaevg.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Chandrabhan Singh</p>
<h4 id="heading-an-introduction-to-rabbitmq-message-broker-amqp-model-and-more">An introduction to RabbitMQ, message broker, AMQP model and more.</h4>
<p>In distributed systems, communication between various applications plays an important role. Effectively passing messages between applications was always a crucial decision in system design. One of the elegant solutions to pass messages around your distributed system is a message broker.</p>
<p>In distributed systems, communication between various applications plays an important role. Effectively passing messages between applications was always a crucial decision in system design. One of the elegant solutions to pass messages around your distributed system is a message broker.</p>
<p>They bring in decoupling between the applications and provide an effective way to communicate. With message brokers, an application needs no prior knowledge of their recipients to communicate.</p>
<p>However, what is RabbtiMQ? How does RabbitMQ fit in this picture? Also, what is AMQP?</p>
<p>By the end of this article, we will be able to answer these questions. I also added a few animations so that you can visualize RabbitMQ concepts.</p>
<p>So are you excited? I am! If you ever had difficulties understanding message brokers, as I did, then this article is the right place to start your journey. Stay with me ?</p>
<h1 id="heading-message-broker">Message Broker</h1>
<p>In general, a broker is a person who facilitates trades between a buyer and a seller. An example could be a real estate agent or a stockbroker.</p>
<p>Similarly, if we want to trade messages between two distributed software components, we need a mediator. This mediator is known as the message broker. It receives incoming messages from a sender and sends them to a recipient. This way the sender and receiver can be totally isolated.</p>
<p>Another analogy for a message broker can be a Post Office (see Figure 1). Let’s take a scenario where you are going to send a letter to your cousin living in another city. Then as per this analogy, you are a producer, your cousin is a consumer, and the post office is a message broker.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*nUaJxRXwLbgZyM4KWwDfig.png?q=20" alt="Image" width="48" height="24" loading="lazy">
<em>Figure 1: Analogy for message broker</em></p>
<h1 id="heading-rabbitmq-as-message-broker">RabbitMQ as message broker</h1>
<p>Now we know that the purpose of a message broker is to route messages from a producer to a consumer. Let’s examine one such message broker — RabbitMQ. It’s one of the most extensively used message brokers these days.</p>
<p>The way RabbitMQ routes messages depends upon the messaging protocol it implements. RabbitMQ supports multiple messaging protocols. However, the one we are interested in is AMQP. It is an acronym for Advanced Message Queuing Protocol.</p>
<p>So without any further ado let’s have a closer look at the AMQP protocol model.</p>
<h1 id="heading-advanced-message-queuing-protocol">Advanced Message Queuing Protocol</h1>
<p>The conceptual model of AMQP is quite simple and straightforward. It has three entities:</p>
<ol>
<li>Queue</li>
<li>Binding</li>
<li>Exchange</li>
</ol>
<p>When a publisher pushes a message to RabbitMQ, it first arrives at an exchange. The exchange then distributes copies of these messages to variously connected queues. Finally, consumers receive these messages.</p>
<p>Consider a message as a piece of data. It is necessarily a package with a payload and some meta-data. The payload contains full data whereas meta-data are properties used by RabbitMQ.</p>
<p>Figure 2 depicts a graphical representation of the AMQP model.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*F1NaCmmz72StUZZHfRfpvw.png?q=20" alt="Image" width="48" height="25" loading="lazy">
<em>Figure 2: AMQP model</em></p>
<p>AMQP is a programmable protocol. Programmers have the liberty to use libraries to configure entities (exchange, binding, and queue) as per their own needs. A RabbitMQ admin has no role in setting up these entities.</p>
<p>There are plenty of libraries available to work with RabbitMQ. You can choose from <a target="_blank" href="http://www.squaremobius.net/amqp.node/">Nodejs</a>, <a target="_blank" href="https://pypi.org/project/pika/">Python</a>, .<a target="_blank" href="https://github.com/rabbitmq/rabbitmq-dotnet-client">Net</a>, <a target="_blank" href="https://github.com/rabbitmq/rabbitmq-java-client/">Java</a>, and many more.</p>
<h1 id="heading-queues">Queues</h1>
<p>These queues are somehow similar to the queues from our data structure classes. RabbitMQ queues also follow FIFO — First-In-First-Out methodology. A queue is a place where RabbitMQ stores messages/data.</p>
<p>Programmers can configure queues through available programming libraries. You can make a queue durable ( with the <code>Durability</code> property) to safeguard your data in case the broker crashes. You can also provide a name(with the <code>Name</code> property) to a queue. Other than <code>Name</code> and <code>Durability</code>, a queue has a few other properties like auto-delete, exclusive, and arguments.</p>
<p>Before moving any further, it’s important to understand who is a direct consumer of these queues. Moreover, how many ways can a user consume messages from a queue?</p>
<h1 id="heading-consumers">Consumers</h1>
<p>Consumers are the ones who are going to use messages stored in a queue. It is possible to connect more than one consumer to a queue at a time. Consumers can either pull the message from the queue by pooling it or queues can even push the message to various connect consumers.</p>
<h1 id="heading-bindings">Bindings</h1>
<p>Bindings are the rules that a queue defines while establishing a connection with an exchange. You can have a queue connected to multiple exchanges. Every queue is also connected to a default exchange. An exchange will use these bindings to route messages to queues.</p>
<h1 id="heading-exchanges-and-their-types">Exchanges and their types</h1>
<p>An Exchange is a gateway to RabbitMQ for your messages. The distance the message has to travel inside RabbitMQ depends on the type of exchange. Primarily there are four types.</p>
<ul>
<li>Direct</li>
<li>Fanout</li>
<li>Topic</li>
<li>Header</li>
</ul>
<h2 id="heading-direct">Direct</h2>
<p>The name explains it all! — A direct exchange delivers a message directly to the queues that satisfy the below condition:</p>
<pre><code>Routing key == Binding key
</code></pre><p>A routing key is an attribute of the message. On the other hand, a binding key is something you specify while creating a binding between a queue and an exchange.</p>
<p>Figure 3 is a visual explanation of how messages flow while using a direct exchange.</p>
<p>A message originates from a producer (green circle) with a routing key — <code>img.resize</code>. Once it reaches the exchange (Orange circle), the exchange will try to find all queues with binding key — <code>img.resize</code>. In case of a match, the message is pushed to all matched queues (resize in our case). If there is no match found, the message can be sent back to the producer or can even be discarded. We are lucky that we found a match in our example ?</p>
<p>[gif image]</p>
<p>Once the message reaches the desired queue (resize in our case), they are distributed in round-robin fashion to all the connected consumers (resizer.1/resizer.2 in our case).</p>
<p>By distributing messages in a round-robin fashion, RabbitMQ makes sure that the messages are load balanced.</p>
<p>You must have noticed that the queue named <strong>crop</strong> is not receiving any messages. Because the routing key in this example is <code>img.resize</code>. To send messages to this queue, we need to send messages with a routing key that would match the binding key (say <code>img.crop</code> for instance).</p>
<h2 id="heading-fanout">Fanout</h2>
<p>A Fanout exchange ignores routing keys and distributes a message to all the connected queues. No wonder it is called Fanout (blowing messages to all connected queues! ?).</p>
<p>One of the use cases for this type of exchange is message broadcast.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/07/1_f4bddksBjxqZjYq3VDa3wg.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Please note that RabbitMQ will still do round robin if there is more than one consumer of the queue.</p>
<h2 id="heading-topic">Topic</h2>
<p>A topic exchange routes a message by matching routing key with a pattern in the binding key.</p>
<pre><code>Routing key == Pattern <span class="hljs-keyword">in</span> binding key.
</code></pre><p>RabbitMQ uses two wild card characters for pattern matching <code>*</code> and <code>#</code>. Use a <code>*</code> to match 1 word and a <code>#</code> to match 0 or more words.</p>
<p>Figure 5 is a visual depiction of a topic exchange. Messages with routing key — <code>logs.error</code> will match patterns — <code>logs.error</code> and <code>logs.*</code>. Hence these messages will end up in the queues — <code>only error</code> and <code>all logs</code>.</p>
<p>Whereas for the producer at the bottom-left, messages with routing key— <code>logs.success</code> will match patterns of binding key <code>#success</code> and <code>logs.*</code> . Hence these messages will end up in the queues — <code>all logs</code>and <code>only success</code> .</p>
<p>[gif]</p>
<p>This type of exchange has a vast range of use cases. It can be used in the publish-subscribe pattern, distributing relevant data to desiring workers processes and many more.</p>
<h1 id="heading-header">Header</h1>
<p>A header is a particular type of exchange that routes messages based on keys present in the message header. It overlooks routing key attribute of the message.</p>
<p>When creating bindings for a header exchange, it is possible to bind a queue to match more than one header. In such a case, RabbitMQ should know from the producer if it should match all or any of these keys.</p>
<p>A producer/application can do this by providing an extra flag called ‘x-match’. ‘x-match’ can have <code>any</code> or <code>all</code> values. The first one mandates that only one value should match while the latter mandates that all must match.</p>
<h1 id="heading-message-acknowledgement">Message Acknowledgement</h1>
<p>Once a message reaches its destination, the broker should delete the message from the queue. It is necessary because a queue overflow can occur if it keeps accumulating messages.</p>
<p>Before deleting any message, the broker must have a delivery acknowledgment. There are two possible ways to acknowledge message delivery.</p>
<ol>
<li>Automatic acknowledgment: Once a consumer receives the message</li>
<li>Explicit acknowledgment: When a consumer sends back an acknowledgment</li>
</ol>
<p>In most cases, explicit acknowledgment is used as it makes sure that the consumer has consumed the message without any failover.</p>
<h1 id="heading-whats-next">What’s next</h1>
<p>RabbitMQ is a very mature and useful product. This article is only a high-level introduction to RabbitMQ. I simplified the concepts to provide a reference point for you to move further. Visit the <a target="_blank" href="https://www.rabbitmq.com/">RabbitMQ</a> website for more complex topics.</p>
<p>Hope you like the article. Don’t forget to clap(or applaud ?). Follow to read my upcoming stories. Until next time, keep Queuing.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
