Microservice Architecture - Asynchronous Messaging
Tags: programming, microservices, javascript
Published: 2022-04-29 17:40:33

This article explains the core benefits of asynchronous communication, queues, and microservice architecture. 
When, how, and why they should be considered in software projects. 

__Why asynchronous messaging?__

Let us consider the following e-commerce goods business. Customers can buy things online and also get their items delivered immediately. 

Let's say these actions are involved in the process:
- Sending the order of the customer to the main ordering service
- Notify customers back via Email or SMS service. 
- Sending the action to a logistics service to deliver the order.

__First Approach: One component that does all the actions:__
A customer buys an item, the order goes to a centralized API that does all required calculations to place payments, discounts, offers, Notifying the customer back, and prepare the delivery of the purchased item.  
![single application communication](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ybmibsoy02a4bq7slvtb.png)

__Second Approach: Using Asynchronous Communication between microservices:__
The action of buying an item goes to a queue that acts as a middleware between the customer and the microservices. 
After placing the action in the queue, Each microservice will consume the message in the queue and do its own required function.  



![Asynchronous communication](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7somwluqv7adeoug07r.png)


__Advantages of Messaging communication and microservices.__

Now when working on a small project that does few simple functionalities, it makes sense to pick the first approach.
When it comes to building a scalable and flexible e-commerce website, picking the second approach will make a lot more sense for the following reasons:  

__1. Reliability:__ Having a microservice and messaging communication architecture will allow an ideal flow of data and actions. If one of the microservices is facing downtime, this will not affect the process of ordering, as other microservices will work independently.

__2. Consistency of actions:__ As explained above, when one of the microservices is down, this architecture will ensure that the actions that are needed to be done will not get lost. When this microservice gets fixed, it will consume all the actions that are not implemented yet during the downtime. 

__3. Scalability:__ As this e-commerce business grows, more orders will be pushed to the queue exponentially every day. Increasing the instances of these microservices that are acting as consumers will ensure the speed and efficiency no matter how much is the throughput and frequency of these actions. 

__4. Maintenance:__ This architecture improves maintainability, as the whole business is separated into smaller divided modules. 

__5. Testing:__ this isolation of having grouped functionalities in one microservice will improve the testability of the business, as testing will occur in individual parts of the system. 

__6. Flexibility:__ Having such architecture will make it easier in the future to hook other microservices to the business as it won't affect the overall behavior of the system. 


__Conclusion:__ We saw the difference between the synchronous and asynchronous systems. Synchronous systems are less reliable and sometimes one small damage in the functionalities or services will cause problems in the whole system.  
On the other hand, Asynchronous communication and microservice architecture provide more flexibility, reliability, maintainability, and consistency. Microservices are more independent and reliable. 

__Examples of messaging queues:__
- Apache Kafka
- RabbitMQ
- Amazon SQS