In monolithic architectural style, the application will be tightly coupled and all the service endpoints can access the database through the database access layer, so there won’t be a need of inter-service communication. But when it comes to Microservices architecture, the application will be divided into different services and each service may have its own database in most of the cases as a best practice.
So, there will be a business need to interact one service with another service because one microservice should not directly access the other Microservices database as per the Microservice’s recommendation. So inter-service communication will play a major role in implementing Microservices.
Inter-service communication is basically two Microservices communicating using either HTTP protocol or asynchronous message patterns.
There are two types of inter-service communication in Microservices:
- Synchronous communication
- Asynchronous communication
In synchronous communication, one service will communicate with another service through the rest endpoint using HTTP or https protocol. In this approach, calling service will wait until the caller service responds.
In asynchronous communication, one service will communicate with another service through the asynchronous messaging. The calling service will not wait to respond by the caller service. First, it will return a response to the user, then the remaining services will process the request.
Asynchronous I/O is different from asynchronous communication. Asynchronous I/O means request will not block the thread to complete the process. But coming to asynchronous communication in Microservices, one service will not wait for another service response.
Asynchronous communication in Microservices will be achieved through the messaging brokers like Apache Kafka, Apache ActiveMQ, etc.
The following diagram illustrates, create order flow with synchronous and asynchronous communication:
Advantages and Disadvantages
- Design complexity is low
- Error handling is easy in synchronous communication
- A real-time response on the go in synchronous communication.
- Service should be available all the time and if service is not available then thread will be blocked and it will cause the performance issue.
- Responsiveness is slow because the one service has to wait for the response of another service.
- Need a connection-oriented protocol
- No need for the connection-oriented protocol. It will transfer messages using message brokers.
- The response will be given immediately, so performance wise asynchronous communication is better than synchronous communication.
- The sender doesn’t need to know about the consumer. So coupling between services is less.
- No need for consumer service’s availability when the sender service sends a request
- Failure isolation is better in asynchronous communication. Because if message broker fails to send the message, once the message broker gets recovered it will resend the message again.
- Design complexity is high in asynchronous communication.
- Error handling is complex
- Coupling with message broker is high, so if you want to change the message broker infrastructure, it becomes difficult to manage in existing infrastructure.
- Latency is very high if message queue fills up.
- Cost of monitoring messaging infrastructure will be significant.
Where & When
In your application, if the request needs to be processed through multiple services and needs approval or further processing, you can go ahead with asynchronous communication.
For example, In the banking domain, loan request should be processed and needs approval at multiple levels. So in this case, when a user raises a request for the loan then the loan request service will provide some reference number immediately. Once all the approvals are done, will persist the loan request details in the database. So in this scenario, we can use asynchronous communication.
In your application, if one service needs the real-time data from another service then you can use synchronous communication.
For example, if we take any e-commerce application when a customer searches for a particular product to purchase then that product’s availability needs to be validated in the inventory by making a request to product availability service. Because customer should know about the current availability of the product to place the order. In this case, you can use synchronous communication to get the product’s real-time availability in inventory and price information.
In Microservices architecture style, communication between services will play an important role when it comes to performance. So based on your requirement, you have to choose the right approach for inter-service communication.
I hope this article was informative and leaves you with a better understanding of inter-service communication in Microservices. At Walking Tree, we are excited about the possibilities that Microservices ecosystem brings in. Stay tuned for more articles on this topic.