kentoh - Fotolia
Synchronous vs. asynchronous microservices communication patterns
What's the difference between synchronous, asynchronous and hybrid communication for microservices? Follow the examples below to establish how microservices communicate.
There are three ways to set up communication in a microservices-oriented application:
- synchronous, in which communication happens in real time;
- asynchronous, in which communication happens independent of time; and
- hybrid, which supports both.
Often, hybrid microservices architecture is overlooked when comparing synchronous and asynchronous communication, but it's an important model to know. Here's how these microservices architectures work.
Synchronous
A synchronous microservice is one in which data moves to and from an endpoint of a service in a blocking interaction. A typical example of a synchronous data exchange is an HTTP request/response interaction, seen in Figure 1 below. When a request is made to an endpoint under HTTP, the caller is locked in the interaction until a response is received.
The caller might receive the response in a mere millisecond or in a few seconds. Regardless of the application latency, the caller cannot move forward to the next task until the response is received. REST is a good example of a synchronous microservice.
Asynchronous
An asynchronous microservice is one in which a request to a service and the subsequent response occur independently from each other. The general practice for implementing an asynchronous microservice is to use a message broker technology, such as Kafka or RabbitMQ, to act as a go-between for services, as seen in Figure 2 below. One service will publish a message to another service using the message broker.
The intended service receives the message in its own time. The sending service is not locked to the broker. It simply fires and forgets.
Hybrid
A hybrid microservice is one that supports both synchronous and asynchronous interactions. For example, a hybrid service will support both HTTP and messaging protocols.
As shown in Figure 3 below, Microservice B accepts an HTTP request from Microservice A. But, before Microservice B responds to Microservice A, it sends a message to Microservice C. The message sent to Microservice C mostly likely will contain information that is relevant to the concern of Microservice C.
Microservice B sends the message to Microservice C asynchronously, without having to wait for a response from Microservice C. This hybrid pattern has a definite advantage for those concerned with the speed of interactions.
GraphQL is a technology that supports hybrid microservices. Callers can interact with a microservice published under GraphQL via its synchronous query and mutation technology. But a caller can also receive a message asynchronously from the microservice using GraphQL subscriptions.