DEV Community

Cover image for Microservice Architecture - Asynchronous Messaging
Evan Hameed
Evan Hameed

Posted on

Microservice Architecture - Asynchronous Messaging

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

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

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

Top comments (0)