When building web applications, there are many different tools that developers can use to optimise performance. One important group of tools are message brokers like RabbitMQ. But what is RabbitMQ, and how does it help performance?
To describe the benefits of RabbitMQ we must first define message brokers and distributed computing systems.
What is distributed computing?
A distributed computing model is one where multiple computers operate in a network, and interact with each other, sharing tasks and components. Machines in this network communicate by passing messages. One way of passing these communications between machines is through message queues which use message brokers to manage the demand.
What is a message broker?
A message broker ‘sits’ between the machines in a distributed computing system process flow. Instead of machines passing messages directly to each other, the messages are first sent to a message broker like RabbitMQ, which then orders the messages in an optimised queue, and passes them on to the receiving machines when the machines are ready to process the messages.
In this context, a message might be a command to process an order, run a specified task, a pull request made to a database, or something more simple like sending an email.
The machine that sends the original message is called the ‘producer’, the machine that receives the message is the ‘consumer’, and the bit in the middle is the ‘broker’.
The 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 interoperability between systems that RabbitMQ facilitates.
This model is different to 'brokerless' models like AJAX, which send requests directly from the machine to the web server.
What is RabbitMQ?
RabbitMQ (Rabbit Message Queue) is just one example of this message broker model. The RabbitMQ server sits between the machines and Rabbit 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 tasks from a particular queue.
What are the benefits of RabbitMQ?
RabbitMQ allows multiple different programming languages and platforms to work together. Thanks to RabbitMQ a message can, for example, be written in C# by the producer but handled in Java by the consumer. The two machines aren’t trying to talk to each other in a language that they don’t both understand, but they’re talking to each other with the multilingual broker RabbitMQ as a proxy. Messages can be written/received by C#, Java, PHP or Perl for instance, but are usually passed around by the broker in XML/JSON/plaintext format.
Separating the producers from the 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, the consumer. The producer just sends a message to the broker (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.
If a consumer is unavailable for any reason (e.g. for server maintenance), messages sent from the producer will just be held in the queue until the consumer is available again.
This means the producer can 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 idle until the consumer was ready. Instead of overloading the consumer with work-loads it can’t handle, RabbitMQ allows the consumer to pick up the work when it is ready to process it.
Similarly, if there are too many messages for a consumer to handle, the messages can just remain in the queue to be consumed later. Alternatively, more consumers can be added to handle the messages coming from the queue. This will improve processing times. This queueing system allows developers to see which machines in the network are doing a lot of work, and when consumers can’t keep up with producers, RabbitMQ helps with scaling up those frequently used parts.
Alternatives to RabbitMQ
RabbitMQ is just one message broker system that makes use of message queues, but there are plenty of other viable options. For example Apache Kafka and ActiveMQ are similar tools to RabbitMQ and even Redis provides message queueing functionality.
Whichever message broker you choose for your projects you'll need a powerful and scalable platform to host it on. Visit the Fasthosts website for more information on our flexible Cloud Servers.