Speeding up services with the Reactive API

Services can often be optimized with asynchronous processing, even without changing their behavior toward the outside world. The reason why some services aren't efficient is that they need to wait for other services to provide a result to continue further. This is especially true when a service executes several calls to other services that don't have to wait for each other. Since the calls are independent, they can be executed in parallel and produce results much faster than if called sequentially.

Looking at the Forecast service in detail, we'll find out that it calls the Temperature service multiple times sequentially in a loop for each city. It waits for each call to finish and return results before doing another call. The following diagram illustrates what's happening:

In order to be more efficient, the Forecast service could do all the calls to the Temperature service in parallel. All the calls are completely independent of each other so there's no need to wait for one to return results before doing another call. The following diagram shows our goal:

Since the calls to the Temperature service happen simultaneously, they can finish much faster than in the sequential scenario. The Forecast service can, therefore, produce the final list of forecasts in a shorter time.

The Building a client section in Chapter 3, Connecting Microservices Together, explained how to use the JAX-RS request builder's async() or rx() methods to retrieve the results of a single asynchronous call. We now need to compose the results of multiple asynchronous calls. Therefore, we will use the rx() method, which provides an easy way to do it with a convenient, fluent API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.125.51