Asynchronous Functions

In a microservices architecture where a program sends a request to a microservice using APIs, the microservice usually sends a response with some data to the requesting program. In a typical sequential flow of execution, the requesting program will wait to receive the response from the microservice before it proceeds with the next instruction. This sequential approach is referred to as synchronous execution.

A response from the microservice may be reasonably fast, but sometimes it may take some time if the network bandwidth is reduced or the microservices server is receiving too many requests. In an ecosystem where applications make extensive use of APIs and microservices called over the Internet, this approach may result in a delay or a poor user experience.

There is an alternative, referred to as asynchronous execution. In this approach, the requesting program sends the request to the microservice using APIs, but it does not wait for the response and continues executing its instructions. With this approach, even if the microservice takes some time to send back a response while some functionality in the calling program continues. The main benefits of using asynchronous programming are improved application performance and responsiveness.

When the microservice is ready, it uses a mechanism known as a callback or a promise to send the response to the calling program. The calling program interrupts its processing wherever it processes and responses it has just received and continues processing from wherever it was interrupted.

While this significantly improves the application responsiveness, but there are some challenges to programming asynchronous flows. The biggest challenge the designer and developer need to keep in mind is that any processing that continues in the main execution while waiting for the asynchronous response, cannot be dependent on the data returned.

Synchronous vs Asynchronous Flows