Threads and Node.js in a nutshell
Tags: javascript, node, architecture
Published: 2021-04-13 11:15:20

**1. Overview of Node Internals:**
Once we write JavaScript code(index.js) and execute the command "node index.js" from the command line, we are invoking the Node.js project. This thus invokes V8 (JavaScript engine) and Libuv (C/C++ Library, it abstracts non-blocking I/O operations). 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f68yoa2xuxu9di547dde.jpeg)

Above we can see the internal architecture of Node.js

The purpose of node is to give us a consistent API for us to work with and the implementation internally is performed by one of V8 or Libuv.

**2. What are Threads, Scheduling and Execution?**
A thread is a sequence of programmed instructions that are managed by scheduler. 
The computer we use are backed by multicore (Quad-core/ Octa-core) system which makes us perform simultaneous operations without any interruption. 
Thread Scheduling is a decision made by the operating system to process a thread at a given time. The executions of thread happens at the CPU.
A Multicore systems support MultiThreading, which makes the process run on separate cores at the same time. This thus increases the overall speed of program execution. 
MultiProcessor System is two or more CPU's present in the same computer. The system is more reliable than multicore system(uses single CPU) since failure of one processor does not affect other processors. 

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1qeoz9kskoyi5vvau21.png)
 
Laptop that we use are backed by a single CPU with multiple cores. But, modern day laptop uses Hyperthreading which creates a logical processor(two virtual processors) thus improving the performance of CPU.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcjgtz3mqndsxvxopj4c.PNG)
Above picture highlights the CPU, Cores and Logical Processors.
 
Note: Hyperthreading are outside the scope of current topic, the idea was to give how our system performs multiple processes at the same time. Now let's look into how Node.js performs the threading..

**3. How Node.js uses Multithreading...**
Not all of the code that we write are executed in a single thread in node. Some standard library function calls the Node C++ side(C++ Add-ons from the Node architecture) which then calls Libuv. Libuv internally makes use of thread pool that creates 4 threads. Libuv is a major dependency for node.js which features event loop and thread pool.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nthso4lbss8l3a7os33p.jpg)

Event Loop:
When we start the Node.js application, the single threaded Event loop executes all lightweight work, such as requiring the modules, the initialization, executing the callbacks. Also, it helps in offloading heavy tasks(file I/O) to the Thread pool. 

Thread Pool: 
Thread Pool handles the "heavy tasks". The works delegated are I/O intensive/CPU intensive. For example: DNS, File System (I/O) and Crypto, Zlib (CPU). By default the Thread pool creates 4 threads to execute a given task.
If there are 5 calls involving I/O or CPU, the first 4 tasks are scheduled in the thread pool and once they are completed the 5th task is placed in the thread pool by the scheduler.

Note: Non-blocking asynchronous network I/O tasks are performed by event loop and DNS tasks are performed by thread pool.  

To increase the number of threads we can use the below piece of code in our js file,
process.env.UV_THREADPOOL_SIZE = 5;