Why do you need Reactive Web Stack

Spring boot is great framework to build micro services . But what about building micro service based on reactive stack ? Well for this exact reason reactive stack web framework has been introduce in Spring 5.

Well why do we need reactive and what are the advantages of using reactive stack ?

Consider simple web app that receives request from web browser , do some operation with data base (GET or POST call) and returns a response to the browser. Whenever request comes to server it allocates thread to process the request . Now this same thread is then process the data received be it queryParam or POST form data . If there is a data base operation then there is data base query call to remote data base server and this threads waits for the response , once response is received from DB then thread returns it to web browser. Now this is generally followed and simplest explanation to request response based web application.
So do you see any problem with above application ? Let’s say in an ideal scenario there are no errors and our simple application can return a data to GET HTTP request by querying data base and we know for every request there is a thread allocation by server.

IMG-1 Blocking Request

As shown in diagram the GET request waits for the result from DB and then result is sent back. If this DB call takes lot of time the thread allocated to will certainly be blocked until the result is obtained or will throw request time out error .

Consider there are hundreds of such request to the server at same time, what will happen ? Server will allocate thread to each request but soon it will run out of those threads if the request are too many or beyond what is configured as max thread pool size for server. We can define it in spring boot by following property


For application if this size is not enough , we can increase it up to certain number or consider adding one more instance of application along with load balancer .

But what if consume the resources more efficiently. This is what exactly we will see now. There are may ways to effectively consume those threads, one of them we are covering here using reactive manner.

So in above example instead of waiting for the data base to respond what if thread get notified when result is received ? may be like call back ? This kind of behaviour is very common in Node js application, in-fact all node js application are written in callback manner because node js can perform only single threaded , non blocking async operations .But in java the basic JDBC drivers are blocking so to solve this we have R2DBC project which is developing drivers for multiple platform out of which we will cover My-SQL in upcomming posts . So drivers are resolved what about controller stack ? For controller we will use spring WebFlux . This both together will be our complete non-block web stack, note here that webflux uses Reactor Netty server. I urge you go and check the official documentation of Spring WebFlux it is very well written.

In Next blog post we will cover what is Spring WebFlux and try to implement controller layer to get our hand dirty with this exiting framework.

Leave a Reply