RxJava & API Performance
Below is a simple workflow of a shopping service. All the operations are executed in a sequence. There are 5 outgoing API calls to other microservices. Each microservice has its own SLA for its API's.
Time taken to complete Purchase = Sum( Time taken to complete all 5 API calls)
Its possible to re-orchestrate the API calls to below in order to improve performance and the reduce the total time for an item purchase. Independent API calls to different microservice can be executed in parallel.
Maximum time to complete parallel calls = Max(time taken by each call in a parallel block)
This is better than the previous approach where the total time to execute two call was the sum of the time taken for each !!!!!.
Making API calls in parallel would require the developer to write code that deals with thread management and asynchronous activity. This can become really complex when dealing with a really complex set of orchestrations.
RxJava to the rescue
RxJava is a library for composing asynchronous and event-based programs by using observable sequences. It extends the observer pattern and allows composition of multiple sequences together while abstracting concerns like synchronization, low level thread management and locks. This very abstraction of threading feature makes it easy to use this library for a wide variety of use cases.