When building web applications, there are many different tools that developers can use to optimise performance. One important group of tools are message brokers, such as RabbitMQ. But what is RabbitMQ, and how does it help performance?
Well, before we look at the benefits of using a Rabbit MQ server, we need to understand what a distributed computing system and broker is, and how they interact.
What is distributed computing?
Unlike standard computing models, a distributed computing model is one where multiple computers operate in a network. This allows them to interact with each other as well as share tasks and components.
Machines in this network communicate by passing messages, and one way of doing this is through message queues. These queues use what's called a message broker to quickly and easily manage the demand between computers for maximum efficiency.
What is a message broker?
In simple terms, a message broker ‘sits’ between the machines in a distributed computing system process flow. Instead of each machine passing messages directly to each other, the messages are first sent to a message broker, like RabbitMQ, which then orders the messages into an optimised queue. These messages are then passed on to the relevant receiving machines when those machines are ready to process the messages.
In this context, a message might be a command to process an order, run a specified task, pull requests made to a database, or to simply send an email.
Within this network, the machine that sends the original message is called the ‘producer’, while the machine that receives the message is the ‘consumer’. The 'broker' is the bit in the middle, the machine that brings the 'producer' and 'consumer' together.
This entire messaging process is controlled by the Advanced Message Queueing Protocol (AMQP). AMQP is a set of standards which covers the entire messaging process, and allows the 'conversations' between the message broker systems that RabbitMQ facilitates. This is unlike 'broker-less' models which send requests directly from the machine to the web server.
How does distributed computing work?
We explained above the roles of the ‘producer’, ‘consumer’ and ‘broker’, and how the AMQP works, but what about all of the concepts in between? In order to understand distributed computing before we delve into RabbitMQ, we’ve made a list of each concept and how it works below:
- The queue: This is the buffer, and it acts as storage for the messages being sent between machines. Usually a message broker such as RabbitMQ will have created the queue.
- The message: A message is the information being sent from the ‘producer’ to the ‘consumer’ via a message broker. As we discussed earlier, the message could be a command, a request or even an email.
- The connection: This is simply the connection between your chosen application and your message broker, in this case RabbitMQ. However, it’s a certain type of connection called the Transmission Control Protocol (TCP). And this TCP connection allows the exchange of messages between your network and your machine.
- The channel: For the messages to be received from a queue, a channel is needed. This channel exists virtually inside the connection.
- The exchange: For a ‘consumer’ to receive messages, the queue needs to use an exchange. The exchange receives messages from a ‘producer’ and transfers these to a queue ready for the ‘consumer’.
- The binding: In order to connect the queue and exchange together, a binding is used to create a link.
- The routing key: This is used by the exchange to decide which queue to send a message to.
Now that you know the parts that make up distributed computing, we can move onto the message broker RabbitMQ.
What is RabbitMQ?
As stated above, RabbitMQ (Rabbit Message Queue) is just one example of a message broker model. The RabbitMQ server sits between the machines and manages the queue. Tasks are added to the queue by the message broker and it’s up to the consumers to connect to Rabbit and specify that they are ready to receive and execute tasks from a particular queue.
How to set up RabbitMQ on Windows
The installation method of RabbitMQ on Windows will depend on which software management tool you use. RabbitMQ recommends using either Chocolatey or the official installer for the setup. We’re going to run through both of these.
Note: You will need to have Erlang installed on your Windows device prior to the installation.
The RabbitMQ site states that once Chocolatey has been installed, the following command will be needed in order to install RabbitMQ:
choco install rabbitmq
This command can be run from the command line or via the task automation PowerShell. That’s basically it for this installation method, however the official installer proves a little bit trickier.
Using the official installer
The RabbitMQ site explains that for every new RabbitMQ release, there’s an accompanying installer. The benefit of opting for this installation method is that users get increased flexibility, however there are certain things the installer requires that every user should be aware of:
- Only one version of Erlang should be installed on your device.
- The version of Erlang you possess needs to be installed via an administrative user.
- It’s also a recommendation that RabbitMQ is installed as an administrator.
- American Standard Code for Information Interchange should be the only characters used in the installation path, and the RabbitMQ site recommends that no spaces are used either.
- The user may need to copy the files used by their CLI tool.
- For CLI tools to work, Windows console will need to operate in UTF-8 mode.
If the above are not met, it’s likely that reinstallation will be needed to ensure that RabbitMQ functions properly.
The benefits of RabbitMQ
So, why should you use RabbitMQ?
Distributed computing systems and other message broker models are incredibly useful on their own, providing numerous benefits over other 'broker-less' setups. Below are some of the key benefits provided by the RabbitMQ server and other message broker models.
Firstly, RabbitMQ allows multiple different programming languages and platforms to work together. Thanks to RabbitMQ, a message can be written in C# by the producer, but handled in Java by the consumer. The two machines are essentially trying to talk to each other in different languages that they don’t both understand. But, through the multilingual broker RabbitMQ server as a proxy, they can communicate without any issues. Messages can be written and received in languages such as C#, Java, PHP or Perl, but are usually passed around by the broker in XML/JSON/plaintext format.
On its own, separating a producer from its consumers has many advantages. The producer of the message doesn’t need to know anything about the consumer, which means it doesn’t need to bloat itself by learning how to connect to or communicate with it. The producer just sends a message to the message broker, in this case RabbitMQ, and the consumer picks it up. Because they are decoupled, it doesn’t matter to the producer if a consumer moves to a new server or is replaced by a different process, they can continue to seamlessly communicate via the RabbitMQ server.
If a consumer is unavailable for any reason (e.g. for server maintenance), messages sent from the producer can simply be held in the queue until the consumer is available again, at which point it can quickly process them all.
This has the added benefit of allowing the producer to continue with its own work until the consumer is back up and ready to process messages from the broker. Without the message queue the producer would have to wait around until the consumer was ready and then send all its requests in one go.
So, instead of overloading the consumer with a workload it can’t handle, RabbitMQ allows the consumer to pick up the work as and when it's ready to process it.
Similarly, if there are too many messages for a consumer to handle while active, the messages can just remain in the queue to be consumed later. Or, more consumers can be added to handle the messages coming from the queue. This will improve processing times and allow developers to see which machines in the network are doing a lot of work and when consumers can’t keep up with producers. The RabbitMQ server can then be used to help with scaling up those frequently used parts.
Alternatives to RabbitMQ
RabbitMQ is just one of several message broker systems that make use of message queues, but there are plenty of other RabbitMQ alternatives that are viable options. For example, Apache Kafka and ActiveMQ are similar tools to RabbitMQ, and even Redis provides message queueing functionality.
But whichever message broker system you choose for your projects, you'll need a powerful and scalable platform to host it on. Take a look at our range of flexible cloud servers, each one of which is secure, powerful, and ready to take your business to the next level.