Single VS Multi-threaded Model – Node

Node JS may be the buzzword or jargon in today’s server side programming world but in order to understand its capabilities and pros and cons, first we have to understand it’s architecture. Node JS applications uses Single threaded model with event looping. Well that is quite a mouthful term.. isn’t it. Let’s discuss it by comparing this with “Multi-threaded Request-Response model”.

Traditional Web Application Model or Multi-threaded Request-Response model

Clients send request to the server and after some processing server returns the response to the client. Since HTTP is a Stateless Protocol, this model is also stateless. This model can be understood by these simple steps:

  • Client send request to the server.
  • Web server maintains some limited number of threads to tackle requests from client.
  • Web server is in constant loop and always waiting for incoming client request.
  • When Web Server receives client requests. It picks up one thread from the thread pool.
  • Assign the thread to that particular request.
  • This thread is responsible for handling a particular client request, reading it, processing it and performing any Blocking I/O operations and then preparing the response.
  • After preparing response, thread directs the response to the Web Server.
  • After receiving response from the thread, Web Server directs the response to the client.

For example, Suppose there are ‘n’ client  requests and ‘m’ no of threads present in the thread pool of Web server, then server starts by assigning each thread to a single request. If n>m (in most of the real life scenarios), then client’s request should wait in the queue until some of the busy threads finish their job and are available to pick up remaining client requests. Blocking I/O operations include interacting with Databases and file system, interaction with JMS queue and communication with some Web Service etc.

There are some drawbacks to be noted here:

  • Handling more client request that require Blocking I/O is bit difficult as compare to non-blocking I/O.
  • Handling more and more requests results in spawning more and more threads which eventually eat up more memory.
  • Significant amount of time gets wasted in processing Blocking I/O operations.


Node’s Single Threaded with Event Loop Model

In Node, a single thread is responsible for handling multiple client requests. In this model, Web server still maintains a limited no of threads to process multiple client requests.This model can be understood by following steps:

  • Web server receive client request. Server internally maintains limited number of threads to process client request.
  • Node JS server receives these requests and places them in a queue known as “Event Queue”.
  • Node JS Web server also has a program known as “Event Loop”. This Event Loop is the heart of Node JS Architecture.
  • This event loop uses single thread. This programs checks if any request is present in event queue.
  • If there is any request present in queue then it picks up that request and starts processing it. If the request does not require any Blocking I/O, then it process that request, prepare response and send it back to the server which in turn send it back to the respective  client.
  • If the request requires Blocking I/O then event loop picks up a thread from the internal thread pool and assigns the thread to that particular request.
  • After processing any Blocking I/O request, thread prepare the response and send it back to the event loop which in turn send it back to the server.
  • Server then send the request to the respective client.

Here in this model, handling of more and more request becomes easy. Node JS application spawn less threads which in turn consume less server space.

So in the end, both model has their own set of pros and cons. Single threaded model with event looping is different, sometimes it is best, sometimes it is not. We have to choose the right tool to do the job.