Friday, March 4th, 2022
RabbitMQ is an open-source message broker software (middleware) that implements the Advanced Message Queuing Protocol (AMQP). It is used for exchanging messages between applications, decoupling the components of a distributed application. It is written in the Erlang programming language.
RabbitMQ message queues work by holding messages that are sent to them until they can be processed by consumers. Producers send messages to exchanges, which are routing hubs that determine which message queues the messages should be sent to based on rules set up in bindings.
Topics, Queues, Exchanges, and Vhosts
In RabbitMQ, a queue is a buffer that holds messages that are waiting to be delivered to consumers. A queue is a named resource in RabbitMQ to which messages can be sent and from which messages can be received. A queue must be bound to an exchange to receive messages from producers.
An exchange is a routing hub that determines which message queues a message should be sent to based on the rules set up in bindings. Exchanges receive messages from producers and use the routing key associated with each message to determine which message queues the message should be sent to. There are several types of exchanges, including direct, fanout, topic, and headers, each with its own routing algorithm.
Exchanges and bindings are used to route messages from producers to message queues in a flexible and configurable way. Exchanges can be of different types, such as direct, fanout, topic, and headers, each of which has a different routing algorithm. Bindings are relationships between exchanges and message queues, specifying the routing rules for the exchange.
A virtual host (vhost) is a separate instance of RabbitMQ, with its own queues, exchanges, bindings, and users. A vhost acts as a separate namespace, providing logical isolation between different applications or users within a single RabbitMQ server. This means that resources created within one vhost are not accessible from another vhost. This allows multiple applications or users to share a single RabbitMQ instance while maintaining separate and independent message routing configurations.
Topic Based Routing
A topic exchange in RabbitMQ is a type of exchange that allows messages to be routed to multiple message queues based on wildcard matching of routing keys. The routing key is a string attached to each message that determines the message’s topic or subject.
When a producer sends a message to a topic exchange, it specifies a routing key for the message. The exchange then evaluates the routing key against the bindings set up between the exchange and the message queues. If the routing key matches a binding, the exchange will route the message to the corresponding message queue.
The routing key can contain multiple words separated by dots (periods), and the exchange uses a wildcard match to evaluate the routing key against the bindings. There are two types of wildcard characters: the hash (#) character, which matches zero or more words, and the star (*) character, which matches exactly one word.
For example, consider a topic exchange with three message queues:
If a producer sends a message with the routing key “news.sports.football”, the first message queue will receive the message because the routing key matches the binding.
If a producer sends a message with the routing key “news.finance.stocks”, the third message queue will receive the message because the routing key matches the binding.
If a producer sends a message with the routing key “news.technology”, the second message queue will receive the message because the routing key matches the binding.
In this way, topic exchanges provide a flexible and powerful routing mechanism for distributing messages to multiple message queues based on the content of the messages.
RabbitMQ provides persistence options to ensure that messages are not lost if a server fails. Persistence options include disk-based persistence and memory-based persistence with disk backups.
Example uses of RabbitMQ include: