This content originally appeared on Level Up Coding - Medium and was authored by Calvin Puram
To set the stage for this tutorial, we will start by learning a bit about nodejs architecture, and I will represent this architecture in terms of node.js dependencies which are just a couple of libraries on which nodejs depends on.
Nodejs runtime has several dependencies and the most important ones are the v8 engine and libuv. Nodejs is a Javascript runtime based on the google v8 engine and that’s why it appears here as a dependency and the v8 engine enables Nodejs to understand the javascript code that we write. The v8 engine is what converts javascript into a machine language that the computer can understand.
But that is not enough to create a whole serverside framework and that is why we also have libuv. Libuv is an open-source library with a strong focus on asynchronous I/O (input/output). This layer is what gives Nodejs access to the underlying computer operating system, file system, networking, and more. Libuv also implements two extremely important features of Nodejs which are the event loop and the thread pool and in simple terms, the event loop is responsible for handling easy tasks like executing callbacks and network I/O while the thread pool deals with heavy work like file access or compression. we will dive deep into this later in this series.
One important thing to note here is that libuv is completely written in C++ and not in Javascript and v8 engine also uses C++ code besides Javascript, therefore, nodejs itself is a program written in C++ and Javascript and not only Javascript as you might expect. The beauty of this is that Nodejs packs all of these technologies and gives us access to their functions in pure Javascript. It really provides us with a layer of abstraction in other to make our life a lot easier. This architecture allows us to write 100% pure Javascript code running in Nodejs and still access functions for file reading which behind the scene are actually implemented in other libraries in the C++ language.
Nodejs also depend on HTTP parser for parsing HTTP, C-ares for DNS request stuff, OpenSSL for cryptography, and Zlip for file compression. So in the end, we have all these pieces nicely fitted together to give us a lightweight and scalable framework.
Processes, Threads, and The Thread Pool
When we use Nodejs on a computer, it means there is a node process running and the node process is just a program in execution. In Nodejs, we have access to the process variable and in that process, Nodejs run in a single thread and a thread basically is just a sequence of instructions. Just imagine a thread as being a box where our code is being executed in the computer processor. Nodejs runs in just one thread which makes it easy to block Nodejs application and this is something that is worth noting because this is one of the unique features that Nodejs bring to the table. No matter the number of users accessing your application, Nodejs only run in one thread and that is why you need to be very careful about not blocking the thread.
What happens in a single thread when we run our application?
So when a program is initialized, all the top-level codes are executed which means all the codes that are not inside callbacks. Next, all the modules that your app needs are required, and all the callbacks are registered after all this, the event loop will then start to loop.
But some tasks are really heavy and expensive to be executed in the event loop because they will then block the single thread so that’s where the thread pool comes to the rescue. The thread pool is provided to Nodejs by the libuv library which gives us four more additional threads that are completely separated from the main thread. We can also configure the thread pool to give us 180 more threads but these four threads are far enough in most cases. The event loop offloads heavy tasks to the thread pool and all this happens automatically behind the scenes.
We don’t decide what goes into the thread pool as developers this happens automatically and these task that gets offloaded to the thread pool are operations dealing with file system APIs, cryptography, compression, DNS lookup (which basically matches web domain to their corresponding IP address). These are the kinds of stuff that can easily block the main thread and Nodejs take care of offloading them into the thread pool automatically this is the most important thing to take note of in this part of the series. In the next part, we will deep dive into what event loop is all about and why it matters.
How Node.js Works: A Look Behind The Scenes was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Calvin Puram
Calvin Puram | Sciencx (2022-01-06T14:45:42+00:00) How Node.js Works: A Look Behind The Scenes. Retrieved from https://www.scien.cx/2022/01/06/how-node-js-works-a-look-behind-the-scenes/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.