Synchronous and Asynchronous Communication

Developing Microservices that work independently is not our end goal. We would also need our services to talk to each other. For example, the Product service and the Order service are hosted on separate servers. Both of these servers are independent of each other. These services are based on REST, and have their own endpoints using which they communicate with each other. 

There are two main ways in which services communicate.

  1. Synchronous
  2. Asynchronous

Synchronous 

In this case, the client makes a request to the remote service for specific functionality, and then it waits until it gets the response.

Asynchronous

In this case, the client makes a request to the remote service for specific functionality. The client doesn’t wait, but it does care about the response.

There are a couple of ways in which Synchronous and Asynchronous communication can be implemented.

  1. Request/response
  2. Event-based

Request/response

In this case, the client sends a request and waits for the response from the server. This is an implementation of synchronous communication. However, the request/response isn’t just an implementation of synchronous communication; we can use it for asynchronous communication as well. 

In the previous lesson, we developed ProductService. This service includes the GetProduct method, which is synchronous. The client has to wait for a response whenever it calls this method.

[HttpGet]
[Route("GetProduct")]
public IActionResult Get() => 
return new
OkObjectResult(_productRepository.GetAll().ToViewModel());

In the code snippet above, when ever the GetProduct is called by the client, it will have to wait until the ToViewModel() extension method is executed.

By tweaking that code a bit, we can make it work for asynchronous communication as well. Code below:

[HttpGet]
[Route("GetProductSync")]
public IActionResult GetIsStillSynchronous()
{
   var task = Task.Run(async() => await
   _productRepository.GetAllAsync());
   return new OkObjectResult(task.Result.ToViewModel());
}

Event-based

Only asynchronous communication can be implemented based on events. To understand event-based asynchronous communication, let’s take an example of a user ordering a product online. It would involve the following steps:

  1. With the help of the website’s search functionality, the user finds a specific product that he/she wish to buy.
  2. Clicking on the search result, the user can view details of the product.
  3. As soon as the user proceeds to the Checkout, the system will make sure that it displays the right quantity. For example, if the available quantity is 10 and the user checks out with one, the available quantity should now show nine.
  4. The system will generate an invoice for the purchased product and sends it to the customer via the registered email address.

So, in the above example, there are multiple events that are happening, and the services are expected to be called based on the beginning/completion of those events. Invoice service and Product service are triggered when the Order service is executed and so on.

You might be interested in the following courses:

Course Category

Back to: Microservices with C#, .NET Core and Azure > Communication