kentoh - Fotolia
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.
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.
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.
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.
Dig Deeper on Java web services and SOA
Related Q&A from Bob Reselman
How do you move a set of REST resources to microservices? One way is to use an API gateway. Follow this example to learn how migrate to ... Continue Reading
Microservices-oriented architecture is more flexible than monolithic structures, but microservices comes with costs companies need to weigh against ... Continue Reading
Microservice-oriented architecture has its benefits, but it comes with some drawbacks as well, such as data translation and debugging. Let's go over ... Continue Reading