Tuesday, November 19, 2019

Reactive Programming: Blocking(Sync) vs Non-Blocking(Async) Request Processing



Blocking Request Processing:

Most of the time, when we expose any endpoint to external world, internally it follows below flow:

controller <-> service <-> dao <-> DB

The existing servlet frameworks, assign a separate thread to each request and that thread will be blocked for that user, until response is sent or process is erred out. Its a synchronized processing of each request.


Blocking request processing


Non - Blocking Request Processing:

This is the new style of programming, where the flow of processing would be same as below, however with each request we have event handlers and call back functions.

                                    controller <-> service <-> dao <-> DB

When any request comes, new generation of web containers ( like netty, undertow, Servlet 3.1+) will start assigning threads to each request, however as any request wait for any slow process to finish, that request will be moved to some kind of queue via handlers and that thread will be freed up for new requests, when slow process will finish for that request, callback will be initiated with received data. Here basically requests are asynchronous in nature and threads are not blocked.

Non-blocking request processing

image courtesy: https://howtodoinjava.com/spring-webflux/spring-webflux-tutorial/

No comments: