Is NodeJS really Single Threaded?

Sopan Mittal
Brain Boner
Published in
3 min readOct 18, 2019

Node.js came into existence when the original developers of JavaScript extended it from, something you could only run in the browser to something you could run on your machine, as a standalone application.

You might have heard from various sources that the NodeJS is single-threaded but, is node really single-threaded? Let’s find out.

We all are familiar with the event loop in the NodeJS, it’s one of the most unique features of the NodeJS which makes it different from other backend languages. It performs I/O in a non-blocking manner but the interesting part is its single-threaded Event loop. Let’s see what this means.

Single Thread Event Loop

NodeJS follows Single-Threaded with Event Loop Model rather than the Request/Response Multi-Threaded Stateless Model. As NodeJS follows this architecture, it can handle more and more concurrent client requests very easily. The main heart of the NodeJS Processing model is “Event Loop”, but before Event Loop comes the “Event Queue”.

  • NodeJS Web Server receives client requests and places them into a Queue, known as “Event Queue”.
  • The Event Loop checks any Client Request is placed in the Event Queue. If there are no requests in the Event Queue, then it waits for incoming requests indefinitely.
  • On the other hand, If there are any requests in the Event Queue then the Event Loop picks one request and Starts processing it.
  • If that Client Request does not require any Blocking I/O Operations, then it process everything, prepares the response and then send it back to the client.
  • If that Client Request requires some Blocking I/O Operations like interacting with Database, File System, External Services then it will follow a different approach :
  1. It checks Threads availability from Internal Thread Pool.
  2. Picks up one Thread and assign this Client Request to that thread.
  3. That Thread is responsible for taking that request, process it, perform Blocking I/O operations, prepare the response and send it back to the Event Loop.
  4. Event Loop, in turn, sends that Response to the respective Client.
NodeJS Architecture

Diagram Description:

  • There are multiple requests at same time
  • There are multiple Concurrent requests from clients. Let’s say clients as Client 1, Client 2, Client 3…. Client n.
  • NodeJS Web Server receives Client 1, Client 2… and Client n requests and places them in the Event Queue.
  • NodeJS Event Loop Picks up those requests one by one.
  • Event Loop pickups Client 1 request (Let’s assume its a non-blocking request) its check for non-blocking.
  • Event Loop process all steps provided in that request and prepares response and send it back to Client 1.
  • Event Loop pickups Client 2 request (Let’s assume its a blocking request) its check for non-blocking.
  • Event Loop checks for an available Thread in the Thread Pool (Currently assume all the threads are free). Event Loop picks up Thread T1 from the Internal Thread pool and assigns this Client 2 request to Thread T1.
  • When the request is assigned to T1, then the Event Loop picks another request from the Event Queue and start processing it. So, it is handling multiple requests at the same time.
  • T1 reads and process the request, perform necessary Blocking I/O or Computation task, and finally prepares the response and send it to Event Loop.
  • Event Loop sends this Response to Client 2.
  • Similarly, all the n requests are processed.

The point to note is that the by default the Node’s Thread Pool has 4 threads. This is not fixed we can change this although its not recommended. Changing the size of Thread Pool can significantly Increase the performance but, this could go the other way around also, depending on the system hardware. (We will see this in the next story).

So in conclusion, we can say that NodeJS use “Single Threaded Event Loop” which handle the clients requests and assign a Thread from the Thread Pool to a Blocking operation and the Event Loop is responsible for sending response to the Client. So, its not the NodeJS which is single threaded, its the magical Event Loop which is single threaded.

--

--