海洋饼干

Introduction to Node.js (Part 2)

Hello Folks! Welcome to the part 2 of the Introduction to Node.js blog. Before you read this blog further, it is must to read the previous blogs for better understanding. Here is the Introduction to Node.js (Part 1) link. So let's proceed.

Node.js Architecture:

As said before, Node.js is majorly dependent upon the Chrome's V8 engine and LIBUV library for its working. Now I guess you know what is V8 Engine and LIBUV library from the Part 1 of this blog. It is to be mentioned that LIBUV is a library in Node.js which is written in C++ and V8 Engine also uses C++ besides using JavaScript. Therefore, Node.js is not just JavaScript as you might have thought.

Now, its time for us to discuss in detail the "Event Loop" and the "Thread Pool" in Node.js.

The Event Loop enables Node.js to perform non-blocking I/O operations in Node.js, even though Node.js is single threaded. The Event Loop is responsible for executing lighter tasks like executing callbacks and network I/O. The Thread Pool in the other hand is responsible for doing heavy tasks line Compression, Cryptography etc.

The Thread Pool:

When we run Node.js in our computer, it's a process which runs in our computer.The process is nothing but an executing program which gets executed in our local computer. When such a process runs, it runs in single thread. Thread is nothing but a sequence of instructions, which are dedicated to perform a given task. We need not to go deep into the thread and process concepts but the necessary parts will be discussed as and when required. But let us remember that as Node.js runs on single thread we must try not to block the thread, no matter how many users or clients access our web application.

When a program is initialized, all the code gets executed apart from the codes , which are inside any callback functions. All the necessary modules are required and the callbacks are registered. Thereafter, the event loop starts running. Thought this event loop will be discussed after this, but for now keep in mind that the "Event-Loop" is the heart of the entire Node.js architecture. It is responsible for major tasks performed in our application.

But sometimes, the tasks are too heavy to be performed by the main thread of Node.js instantly and it then tends to block the execution. The solution to this is the "Thread Pool". It is basically a set of 4 additional threads provided by the LIBUV library. When a heavy task appears, the main thread offloads it to the additional threads to execute it. The heavy tasks like dealing with file system API, compression, cryptography and DNS lookups are offloaded generally. All of these are handled by Node alone. I hope now you have a little idea about the Thread Pool. So, let's talk about the Event Loop.

The Event Loop:

As mentioned in the previous section, the event loop is the heart of the Node.js application. The codes which are in the callback functions runs in the event loop.The event loop is also responsible for handling the offloaded tasks. So, let's see how it works.

Node.js has an event triggered architecture as it uses a lot of these callback functions. If you don't come here from a JavaScript background then here is the definition of callback function. A callback function gets executed as a work gets finished. Soon after a work is done, an event is emitted. The event listener listens to those events and accordingly trigger their respective callback functions. This is how Node.js remains asynchronous. If you want to know more, you can refer to the Node.js documentation at https://nodejs.org/en/docs/

In the part 3 of this Introduction to Node.js blog, we will download Node.js in our local PC, set the path and get our hands dirty. Click here to learn how to learn Node.js Hope you like the Introduction to Node.js (Part 2). To learn more follow the next blogs.