First, we will see the flow of spring-mvc.
Whenever a web server gets a request, a new servlet thread is assigned to it. It takes the request and do
some processing. After that, it makes a database call. Database call is an I/O operation between our web
server and the database. During the database call, this servlet thread is going to a waiting state. It is on
waiting state until database driver fetches the data bundle. Once it gets data successfully, the servlet
thread gives response to the client. This is the normal flow of spring-mvc.
If another request come into the web server during this servlet thread waiting period (during I/O
operation) a new servlet thread is assigned to it and the same process is continued.
Let us say that we have many concurrent requests into the web server. Imagine our web server thread
pool size is 200. Assume that the web server gets 200 requests within the same time. In such a case, 200
servlet threads are created and database calls are made to fetch data. If more of the web server
requests come, they have to wait until the rest of the servlet threads do their operations and release.
In this case, more number of web requests have to wait until other servlet threads are released. If the
server handles more number of concurrent requests, server always gets busy and resource usage
becomes high. Therefore, we want better mechanisms to serve more number of concurrent requests
What we ideally want is, whenever the request comes, we assign a servlet thread (ST1) and do some
processing. Then instead of waiting for the thread to complete the I/O operation, we tell the database
driver to notify to the servlet thread to receive the response once the I/O operation (OP1) is completed.
While doing that, the I/O operation servlet thread can serve another server request and make a
database call to make the I/O operation (OP2) and release. During that time, if the previous I/O
operation (OP1) is completed, the servlet thread (ST1) or any other servlet thread can get the output of
I/O operation (OP1) and send that response to the client. In this mechanism, servlet thread does not
need to stay in a blocking state and waiting for the I/O operation. This mechanism can be used to avoid
the servlet thread going into a blocking state or a waiting state. If we expand this concept to all the
available threads, this call back event base mechanism. This mechanism is call as event loop.
In this event loop we have limited number of servlet threads to serve the user requests. Whenever there
is a request, it does some basic process and immediately delegate the request to the database driver for
I/O operation as an event. On the other side, database driver will take the event, and will do the I/O
operation and whenever it is ready with the data it also fires an event saying ‘I am ready with the data’.
After that any of the threads can get the response of I/O operation and can give to the client.
If we visualize traditional request and event loop request handle mechanism, it is as follows.