# RabbitMQ

RabbitMQ is a message-queuing software that can be known as a message carrier or a queue manager. Simply put, it is a piece of software where queues are defined, serving the application with the purpose of transporting one or more messages.

&#x20;     So, why do we need RabbitMQ? Imagine, you currently have a web service that must receive many, many requests per second, but must ensure that not a single request is lost. And your web service is always ready to receive new requests instead of being locked because it is processing the previous request. So the idea here is to put them in a queue between the web service and the processing service. This will ensure that the two processes will be completely separate. In addition, the queue will store requests, without missing any requests when their number becomes extremely large.

### Basic concepts in RabbitMQ <a href="#id-11-nhung-khai-niem-co-ban-trong-rabbitmq-1" id="id-11-nhung-khai-niem-co-ban-trong-rabbitmq-1"></a>

* **Producer/Publisher** : Application that sends messages.
* **Consumer** : Application that receives messages.
* **Queue** : Stores messages.
* **Message** : Information transmitted from Producer to Consumer via RabbitMQ.
* **Connection** : A TCP connection between the application and the RabbitMQ broker.
* **Channel** : A virtual connection within a Connection. Publishing or consuming from a queue is done on the channel.
* **Exchange** : Is the place that receives published messages from Producer and pushes them into the queue based on the rules of each type of Exchange. To receive messages, the queue must be located in at least 1 Exchange.
* **Binding** : Takes on the task of linking between Exchange and Queue.
* **Routing key** : A key that Exchange relies on to decide how to route messages to the queue. To put it simply, the Routing key is the address for the message.
* **AMQP** : Advance Message Queuing Protocol, is a message transfer protocol in RabbitMQ.
* **User** : To be able to access RabbitMQ, we must have a username and password. In RabbitMQ, each user is assigned with certain permissions. Users can be assigned special rights to a certain Vhost.
* **Virtual host/Vhost** : Provides separate ways for applications to share a RabbitMQ instance. Different users can have different permissions for different vhosts. Queue and Exchange can be created, so they only exist in one vhost.

![](https://images.viblo.asia/a1571d98-cb4e-4f3a-9757-117a492be32c.png)

### **Exchange Types**

There are 4 types of exchange in RabbitMQ. RabbitMQ use Direct Exchange as default behaviour.

**Direct Exchange:** A Direct Exchange delivers the messages to the queue based on routing key. You can give the queue name to the routing key of exchange. A queue binds to the exchange with a routing key.

<figure><img src="https://miro.medium.com/v2/resize:fit:1396/1*7qh20tVdjrULoWGJnjlBMA.png" alt="" height="526" width="698"><figcaption></figcaption></figure>

**Fanout Exchange:** In this exchange type, the message is sent to all queues bind to the exchange. Routing key is ignored. If you want to send the published message to all queues ideal way is using the Fanout Exchange.

<figure><img src="https://miro.medium.com/v2/resize:fit:1396/1*-djPfoBr7TZ3nF2IlYLpEA.png" alt="" height="463" width="698"><figcaption></figcaption></figure>

**Topic Exchange:** Allowing us to send messages according to the subject. Topic exchange sends the messages based on routing key and routing key patterns.

<figure><img src="https://miro.medium.com/v2/resize:fit:1040/1*_IXWVhiuIQbmUpJ-OpDpgw.png" alt="" height="570" width="520"><figcaption></figcaption></figure>

**Header Exchange:** This exchange type delivers the messages to queues using headers instead of routing key. In the binding between exchange and queue, a specific argument termed “**x-match**” indicates whether all headers must match or only one. If “**x-match**” key’s value is “**all**” that means is all header values must match. If value of “**x-match**” key is “**any**” it means that one header value is enough to match.

<figure><img src="https://miro.medium.com/v2/resize:fit:1040/1*5ibWwmaGX0GqU9UFzvt2mQ.png" alt="" height="578" width="520"><figcaption></figcaption></figure>

### Activity diagram&#x20;

1. Producer sends a Message to an Exchange.&#x20;
2. Exchange uses Routing Key and Binding to route Messages to Queue.&#x20;
3. Consumer receives Message from Queue.
