Let’s start by defining synchronous and asynchronous communication; Synchronous communication is occurring in real time. Like a phone call. You have to wait until the person on the other end answers your question in real time. When you are using asynchronous communication, you are not waiting for a response in real time. You can move on to another task before your first task is completely finished or once you are done with your part of a task. Email is a good example of asynchronous messaging. As soon as the email is sent from you, you can continue handling other things without the need of getting an immediate response from the receiver. You can do other things while the message is in transit.
Imagine what happens when you dial someone and the line is busy or when you're in a phone call and the one you are talking to needs to respond to some questions from someone else in the background; You will not get any kind of response or you will need to wait until the person on the other end has answered the questions from the other one, before you will get your answer. If you in this scenario would have sent an email, you would never have been put on hold - the other person talking in the background would never have stolen time from you and the line would never have been busy. Your question would have been sent and you would have gotten the response when the person on the other end of the line had time to answer it for you.
Asynchronous communication enables flexibility: You can send your message out and afterwords just keep working on other things - while in synchronized communication you have to wait for a response in real time.
Asynchronous communication in computer systems
If one system goes down in a system of asynchronous applications, the other system will not be impacted. The task will be on hold until the other system is up and running again. If you have a web application receiving a lot of requests, you can let this web application generate tasks in response to user inputs. Tasks that are sent to a receiver. A receiver can retrieve the task and process it when the receiver is ready and return a response when it is finished. In this a way the user interface can remain responsive all the time.
How to implement asynchronous messaging in the cloud?
Asynchronous messaging in the cloud is usually implemented by using message queues, a message broker. The basic architecture of a message queue is simple, there are client applications called producers that create messages and deliver them to the message queue. Another application, called consumer, connects to the queue and get the messages to be processed. Messages placed onto the queue are stored until the consumer retrieves them - it does not even have to be running concurrently. When a message is retrieved, it is processed by the receiver and afterwords removed from the queue. This way of building an application decouples the sender from the receiver - the sender and the receiver of the message do not need to interact with the message queue at the same time.
RabbitMQ as message broker
An example of a message broker is RabbitMQ. A message broker usually provides features around the messaging, and I will now highlight some of the features available in RabbitMQ.
When sending a message to a queue, you start by sending the message to an exchange instead of sending it straight to the queue. RabbitMQ exchanges can be seen as a telephone exchange, but instead of routing phone calls it will route messages. A telephone exchange will help routing the phone call to correct receiver with help of a given phone number, while an exchange in RabbitMQ will help routing the message to correct queue with help of attributes provided by the sender, attributes located in the message.
Different types of exchanges can be used to reach different goals, different routing logics. The message can be sent out to a single queue (a single address), or to a lot of queues that broadcast the message to multiple consumers.
You can exchange messages in a format of your choice (eg JSON or binary). One great approach with this kind of setup is the ability to broadcast events – your service does not really have know its audience. The exchange could for example send the message "User update". The services that are interested in such information subscribe to theses messages and process them one by one.
RabbitMQ provide four different types of exchanges.
In order to make sure that a message is never lost, RabbitMQ supports message acknowledgments. An acknowledgement is like saying thank you after you have received something. The acknowledgement is sent back from the receiver to tell the message broker that a particular message has been received and that the message queue is free to delete the message. If a receiver dies without sending an acknowledgement, the message queue will understand that the message wasn't processed fully and it will redeliver the message to the queue. That way you can be sure that no message is lost.
Persistence, clustering and highly available queues
Message queues and messages can be persistent meaning that information will not be lost in case you restart. A message queue in RabbitMQ gives your messages a safe place to live until they are received. Several RabbitMQ servers can be clustered together, forming a single message broker. Queues can be mirrored across several machines in a cluster, ensuring that even in the event of hardware failure your messages are safe.
Management UI, tracing and plugins
The RabbitMQ Management is a user-friendly interface that let you monitor and handle your RabbitMQ server. Among other things queues, connections, exchanges, users and user permissions can be handled - created, deleted and listed in the browser. You can monitor message rates and send/receive messages manually.
RabbitMQ offers tracing support, which will help you find out what's going on if your system is misbehaving.
A lot of users are using RabbitMQ, and some features has been missing. Luckily, it's easy to write plugins to RabbitMQ and today a lot of the missing features have been added as plugins or are added into RabbitMQ. RabbitMQ ships today with a variety of plugins!
How do I get started?
As you can see, there's a lot of features around messaging in RabbitMQ and it's an awesome tool that can be used when you need to build a distributed system that communicate via asynchronous messaging. If you are eager to get some more information about RabbitMQ, I recommend you to read our RabbitMQ beginner guide.
Please email us at firstname.lastname@example.org if you have any suggestions, questions or feedback.