Welcome back to Tech Orb after a long time. In this article I’m going to give you an introduction to RabbitMQ. Briefing the article content, this will cover
- What is RabbitMQ?
- How does it work?
- Features of RabbitMQ
- Installing RabbitMQ
- RabbitMQ Management Panel
RabbitMQ is one of the most popular open source message brokers. Next question that would come to your mind is what is a message broker?
Literally message brokers are the building block of message-oriented middleware (MOM). Systems that use a messaging broker are by definition distributed. Though it’s not a replacement for RPC (have no idea about RPC? If so refer my previous article about RPC). Simply what message broker does is, it accepts,stores and delivers the messages to the correct destination. This stores messages in queues which lives inside RabbitMQ. Queue is a large message buffer. There are other message brokers than RabbitMQ. Azure Event Hub, ActiveMQ,OMS,Apache Kafka,JMS,Redis and Service Bus are some of them. Without further explanations let’s understand how it works. That would help you to understand what RabbitMQ is exactly.
Before understanding the process there are some words that you need to understand first.
Producer: Program that sends messages
Consumer: Program that waits to receive messages
Queue: Name for a post box which lives inside RabbitMQ (a message buffer)
First we should start the RabbitMQ server.Then publisher will connect to RabbitMQ which is on the local machine or on a different machine, send a single message, then exit. Consumer will keep running, waiting for messages. For now just think the process as producer sends messages to queue and consumer takes the relevant messages from the queue.
Many producers can send messages that goes to one queue, and many consumers can try to receive data from one queue. An application can be both the producer and the consumer, too. Most importantly producer, consumer, and broker do not have to reside on the same host.
Previously I told that producers send messages to queues and consumers takes those messages from the queue. But what actually happens is producer never directly sends messages to queue, it uses exchanges and exchanges connect with the queues and pushes messages to relevant queues.That relationship between an exchange and a queue is called a binding. If no queue is bounded to the exchange messages will be lost. We need to supply a routing key when sending a message and it is compared with the binding key. Let’s illustrate this with the following diagram.
Q1 is bound by the binding key “big_bang_theory” and Q2 is bound by the binding keys “firefly” and “star_trek”. That means messages published to the exchange with the routing key “big_bang_theory” will be routed to Q1 and messages with a routing key of “firefly” or “star_trek” will go to Q2. All other messages will be discarded.
- Exchanges decide what to do with the received messages by its type.If it is
- Fanout -> send message to all the queues it knows
- Direct -> send to the queue which the binding key= routing key
- Topic -> send to the queues which partial matches to the key
- Header -> uses message header instead of routing key
Following are the features of RabbitMQ. Though I won’t explain those since it is bit heavy for an introduction. If you are interested about it, go to this link and read more: https://www.rabbitmq.com/#features
- Asynchronous Messaging
- Develop cross-language messaging with favorite programming language
- Distributed Deployment
- Enterprise & Cloud Ready
- Tools & Plugins
- Management & Monitoring
Next I’ll brief the commands that you need to use to install and run RabbitMQ on your Linux machine.
- First update the system :
- RabbitMQ uses erlang. So first install erlang :
apt-get install erlang
- Next install RabbitMQ server :
apt get install rabbitmq-server
- Then enable RabbitMQ service :
systemctl enable rabbitmq-server
- Start RabbitMQ server :
systemctl start rabbitmq-server
- To check the status :
systemctl status rabbitmq-server
As I mentioned before there’s the management and monitoring feature in RabbitMQ. So management plugin provides a nice dashboard for RabbitMQ which allows us to see queues, exchanges etc. Enable the plugin by the command:
sudo rabbitmq-plugins enable rabbitmq_management
Then open the browser and type
http://localhost:15672 You will direct to a login page and by default this has the username and password
guest. Once you logged in you will see the dashboard.
So as an intro to RabbitMQ I’ve covered the very basics of it from this article. Finally I like to suggest you to refer the RabbitMQ tutorial and try those hands-on examples, choosing your preferred language to learn more about RabbitQ. See you soon in the next article:)