RabbitMQ for beginners¶
what is RabbitMQ?¶
- RabbitMQ is a message-queueing software also known as a message broker or queue manager.
- It implements Advanced Message Queuing protocol.
- RabbitMQ is one of the most popular open-source message brokers.
terminology to be aware of before using RabbitMQ¶
- Producer: A producer sends or pushes messages to a queue based on a queue name
- Queue: A queue is a medium via which we can transfer and store messages or buffers
- Consumer: A consumer subscribes, receives, or consumes messages from the broker, and then processes or uses them in another process or application
- Exchange: An exchange is an entry point to the broker as it receives messages from a publisher and routes them to the appropriate queue
- Bindings: Bindings are rules that exchanges use (among other things) to route messages to queues. To instruct an exchange E to route messages to a queue Q, Q has to be bound to E.
- Broker: A message broker basically offers a storage mechanism for data produced from one application. This data is usually meant to be consumed by another application that connects to the broker with the given parameters or connection strings
- Channel: Channels offer a sort of lightweight connection to a broker via a singular and shared TCP connection. This is due to the fact that creating multiple open connections to a broker is an expensive operation
- Virtual host (Vhost): Virtual hosts make it possible for a single broker to host a couple of isolated environments
Standard RabbitMQ message flow¶
- The producer publishes a message to the exchange.
- The exchange receives the message and is now responsible for the routing of the message.
- Binding must be set up between the queue and the exchange.
- The messages stay in the queue until they are handled by a consumer.
- The consumer handles the message.
Types of exchanges in RabbitMQ¶
- The default exchange is a direct exchange with no name (empty string) pre-declared by the broker.
- It has one special property that makes it very useful for simple applications
- every queue that is created is automatically bound to it with a routing key which is the same as the queue name.
- A direct exchange delivers messages to queues based on the message routing key.
- A direct exchange is ideal for the unicast routing of messages
- A queue binds to the exchange with a routing key K
- When a new message with routing key R arrives at the direct exchange, the exchange routes it to the queue if K = R
- A fanout exchange routes messages to all of the queues that are bound to it and the routing key is ignored.
- If N queues are bound to a fanout exchange, when a new message is published to that exchange a copy of the message is delivered to all N queues.
- Fanout exchanges are ideal for the broadcast routing of messages.
- Topic exchanges route messages to one or many queues based on matching between a message routing key and the pattern that was used to bind a queue to an exchange.
- The topic exchange type is often used to implement various publish/subscribe pattern variations.
- Topic exchanges are commonly used for the multicast routing of messages.
- A headers exchange is designed for routing on multiple attributes that are more easily expressed as message headers than a routing key.
- Headers exchanges ignore the routing key attribute. Instead, the attributes used for routing are taken from the headers attribute.
- A message is considered matching if the value of the header equals the value specified upon binding.
- Install it using docker
docker run -itd --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.9-management
- After installing it with docker visit http://localhost:15672/ and login to rabbitmq web admin interface with username
- Try to create queue, bindings, exchanges and add messages to queues and recieve messages from the queues.
- for other type of installations, visit https://www.rabbitmq.com/download.html
When to use RabbitMQ?¶
- When we have long running processes and background jobs
- For quick HTTP responses (i.e add to queue and process later)
- When we have event driven systems at a microservices level
That's it folks. Let's see how we can use it with python's celery in upcoming articles.