Threads ,Thread Pool and Event Loop in Node.js
Threads in Node.js
when we use node in a computer it means that there is a node process running on that computer. Node.js runs on a so called single thread and a thread is basically just a sequence of instructions. If you run your node app it will run in just a single thread no matter it is getting used by 10 users or 10 million users.
How node.js process works?
Single Thread --> Initialize program --> Execute top-level-code --> require modules --> register event callbacks --> start event loop .
So basically node.js runs javascript code in single thread by default so each new user request something the request will be sent to a thread and it will only execute one request at a time .
Node.js also provides hidden threads through libvu library . libuv is a multi-platform support library with a focus on asynchronous I/O. It was primarily developed for use by Node.js, but it’s also used by Luvit, Julia, uvloop, and others.
What is Thread Pool ?
Some tasks are too heavy to execute in single thread or in event loop and they also blocks the single thread. Here comes the thread pool in action .Thread pool is also known as worker pool. Node.js uses two types of threads that is event loop and thread pool (worker pool) .
So if a thread is taking too long time to execute a callback or task we call it as a blocked code . Suppose one client sends the request to the server and while working on the request node.js or thread fails to execute the request then it will not be able to handle requests from the other users . So because of this we should never block neither a thread pool (worker pool) nor a event loop . If you regularly perform the heavy task on either a thread pool or event loop then your server will suffer . So the thread pool gives us four additional threads that are completely sepertate from the main single thread then event loop automatically sends the heavy task to thread pool .
Handle heavy expensive tasks
- File system API's
- Cryptography
- compression
- DNS lookups
What is event loop ?
Event loop is the heart of Node.js architecture , All the application code that is inside callback function is executed (non-top-level-code). So when we require or let's say import some built in modules or third party moudles that is not included in a event loop . event loop is nothing just a callback function which executes the javascript code asynchronously . Node.js is built around callback functions and event loop is one of the callback function which offloades the heavy tasks in the thread pool .
The event loop is what allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible. Event loop works on FIFO (First in First Out) .
Let's say we have given a task of reading a text file in event loop and after the event loop we have written some code or just console.log . So what will happen now is event loop will try to read the file if it is taking too much time then event loop will simply offload that task in thread pool and then node.js will execute other code which is outside of event loop. Then event loop will take back the offloaded task from the thread pool and executes it .
JavaScript has a runtime model based on an event loop, which is responsible for executing the code, collecting and processing events, and executing queued sub-tasks. This model is quite different from models in other languages like C and Java.
SetTimeOut function is the best example to execute a event loop . We just have to write a code inside setTimeOut function and it will be executed in the last .