Single Threaded Event Loop Model

In this section, we will take a look at popular asynchronous model known as 'Single Threaded Event Loop Model' which is widely used in building scalable server side applications.

Topic

Traditionally, web servers which serve HTTP requests are multi-threaded. They spawn a new thread for every HTTP request which arrives. This thread would be responsible for sending the response, which might include database calls which are Blocking I/O operations. This kind of server may not scale as the request traffic increases exponentially.

A new model, known as 'Single Threaded Event Loop Model' has emerged wherein instead of spawning one thread per request; a single thread would cater to all requests. But how does this scale?

This single thread would perform the processing and whenever it has to make blocking I/O operation, it would spawn a separate thread to perform that operation. Upon completion, the thread which performs the I/O would make a call back to the main thread the response would be returned to the client. As shown in the above diagram, all blocking operations are delegated to a separate thread asynchronously and the once the blocking operation completes, it would perform a call back and provide the response, which is eventually returned to the client.

Reference Implementations:

Frameworks like Node JS, Reactive Java support Single Threaded Event Loop Model.

results matching ""

    No results matching ""