Reactive Programming
Why Reactive Programming?
Lets discuss the traditional approach of developing the web services using spring MVC. In the diagram below you will see how the request is served in by using Spring MVC architecture.
The problems in above API design are
Thread Per Request Model
The Rest services which are developed using the Spring MVC creates a thread for each request comes to the server. If suppose each thread requires 1 MB space in the stack memory to store its state. For e-commerce site If there are 1 million customer online, it requires around 100 GB of memory for these number of threads, certainly server will be out of memory and eventually it will shut down. This is one of the problem why reactive programming is needed. In reactive programming single thread will be responsible for handling multiple request.
Blocking and Synchronous
The code below is the API for getting the product specification from the product id.
Getting the product specification from the product id is the third party service and that we are calling using the rest template, until the response comes from the product specification service the request thread will be blocked. This type of programming model is called as the imperative style of programming model. This is synchronous. In this type of mode system resources will be under utilized as threads are blocking and consuming the resources.
Back Pressure Compatibility
The third problem with the traditional way of developing the web services is no back pressure support. If the client is overwhelmed by the response from the server then in traditional approach there is no back pressure support in order to notify the server that client is not capable of receiving the large amount of data.