Your dinner is ready! Understanding asynchronous communication while preparing your meal

Esperanza Macarena Aviles Moron
Clarity AI Tech
Published in
6 min readDec 5, 2023

Introduction

These are the things you are going to learn with this article:

  • What is synchronous communication
  • What is asynchronous communication
  • How asynchronous communication works with a practical example written in Java using RabbitMQ and Spring Boot

And all of this while your dinner is cooking in the oven! 😉

What is synchronous communication?

In our daily lives we encounter situations that require us to be present at the moment: a video call, a doctor’s appointment or a get-together with friends. We find synchronous communication in all these situations, where people must be present to perform an action, whether it is having a drink or defining the next feature to be developed.

We also encounter other situations in which we have to follow steps in a specific order. For example, if you want to follow a recipe, you follow the different instructions one after the other. If you want to prepare a cake, first you will have to whisk the eggs, then mix them with the flour, then with the sugar, etc.

In programming you may come across some programs that run synchronously. That is, an instruction is not executed until all previous instructions have been executed. For example, in the following code:

You will not see the “Second!” line on the screen until “First!” has been printed first:

But what if you needed these instructions to be executed at different times, asynchronously?

What is asynchronous communication?

To understand how asynchronous communication works, let me show you another example.

You have on your cell phone an application called Appliances in which you can receive notifications when the different appliances in your house generate a notification.

For example, if you subscribe to this application, you will be notified when the washing machine has finished the program you have set to wash clothes. You will receive a notification on your cell phone that you can consult and, when it suits you, you will be able to hang the laundry.

This application will also notify you when the oven and microwave have finished the program you have set. As with the washing machine, you will receive a notification on your cell phone telling you that the oven or microwave beeped and that you can enjoy your meal.

This is a clear example of asynchronous communication, although it can also be called event-based communication. An event (the washing machine has finished its program) emits a notification (clean clothes!) that you receive and with which you carry out an action (hang the laundry) at the time that suits you best.

In essence it is an exchange of messages, in this case between two applications, that does not require a temporal coincidence: the message sender can transmit the message at one time and the subscriber can read it and act accordingly at any other time.

Asynchronous Communication in a Web Development Environment

Let’s translate this last example into code. I have built two web applications using Spring Boot and RabbitMQ. All the code you are going to see from now on you can find it in my GitHub repository:

Since the purpose of this article is not to explain how to build a Spring Boot application from scratch nor how to configure RabbitMQ, I will not go into the details of how these applications are built. However, you can find instructions in my GitHub repositories on how to execute these applications.

On the one hand, I have created an application called AppliancesProducer, whose purpose is to send messages when a request is made to one of the three configured routes:

  • /appliances/oven → the oven will make a request to this path
  • /appliances/washing-machine → the washing machine will make a request to this path
  • /appliances/microwave → the microwave will make a request to this path

On the other hand, I have created an application called AppliancesConsumer whose purpose is to listen to the first application. So, when a request is made to any of the three paths we have seen before, AppliancesConsumer will receive a message.

Let’s make a request to the /appliances/oven path. In the AppliancesProducer application we will see the following message, which means that the notification has been sent successfully:

However, in the AppliancesConsumer application you can see that the following messages have arrived:

Bravo! The two applications are communicating asynchronously. In this way, the consumer will be able to choose the action they want after receiving the notification. For example, go to the kitchen and take out from the oven that steaming pizza 😉

This communication from one application to another is not the only way of asynchronous communication. We could also have two modules communicating with each other within the same application. Or a single application could be listening on more than one channel at a time (in this example it is only listening on one channel). You can adapt the application to your needs.

Our use of event-driven communication in Clarity AI

At Clarity AI we use event-driven communication with Spring Boot and RabbitMQ in our backend environment. Specifically, we make several modules communicate with each other within the same application asynchronously.

For example, there are cases where when deleting a user in module A we need module B to also delete some related data. We could use a synchronous execution for this case, but this produces a coupling between both modules.

Our solution is to emit an event each time a user is deleted in module A. Thus, modules that are interested in knowing when a user has been deleted can subscribe to the channel we have created and act accordingly.

The advantages of this method are twofold:

  • Each team can choose how it will respond to an event and it is not an outside team that has to decide what actions to execute
  • We reduce the coupling between modules: module A does not have to know how module B works and vice versa

Conclusion

Throughout this article we have seen the differences between synchronous and asynchronous communication. If you want to prepare a cooking recipe, you have to follow each step consecutively: remember that if you want the cake to turn out well, you cannot put the flour in the oven without first mixing it with the sugar and the eggs. Therefore, you must follow a synchronous execution.

However, you can use asynchronous (or event-based) communication for other types of tasks, such as having your oven notify you when the cake is done. This way, you don’t have to wait for the baking program to finish.

I encourage you to try building the program and see how the two apps behave — you might even make a real app and you’ll never forget to hang your laundry again!

Acknowledgments

Many thanks to my amazing team at Clarity AI! They have supported me in the process of writing this article and they have been great reviewers.

References

--

--