In this article, we’ll discuss synchronous vs asynchronous communication between microservices.
Synchronous Microservices Communication
Microservice communicates with a different microservice by requesting and waiting for the response before further processing. In other words client blocks and waits for the response before continuing its processing.
The client and microservice are closely connected in synchronous communication since the client must wait for the microservice to finish processing before proceeding.
This type of communication is done via HTTP-based protocols, eg: REST API calls, etc.
Eg:

In the above example: Microservice 1 receives an order, and fulfilling it requires the order’s payment info. So M1(client) requests payment info from M2 via rest api call. M2 fetches the payment info from its database (M2DB2) and responds back to the request with a payment info response. After receiving the response M1 continued its processing, accepted the order, started the fulfillment process, and persisted the data in the database (M1DB1).
In this example, M1 has to wait until it gets the response from M2 because the system’s design requires waiting for the payment info to process further.
Asynchronous Microservices Communication
In asynchronous communication, the client makes the request to the microservice and does not wait for the response. The microservice handles the request on its own while the client carries on with its execution without any interruptions.
Microservices can send events or messages to message brokers or event buses, which forward them to the relevant microservices that have signed up to receive them. By doing this, we separate the sender and recipient microservices, allowing them to develop independently and at different rates.
Eg:

In the above example: Microservice 2 received the payment request, processed it and sent it to the centralized hub (Amazon Kinesis, Kafka, Rabbit MQ, etc). Microservice 1 subscribed to that stream or topic, allowing M1 to asynchronously consume the payment info and persist it in the database. As soon as M1 receives an order it already has the payment info in its db through which it can continue to process the order and had not to wait for the synchronous communication.
Key differences between sync and async communication
- Request-Response vs. Fire-and-Forget
- Blocking vs. Non-blocking
- Immediate vs. Delayed Response
- Coupling vs. Decoupling
For more related tech articles visit: https://blog.nashtechglobal.com/
Similar articles: https://medium.com/