DEV Community

Cover image for Behind the Scenes: Understanding the Inner Workings of Node.js
mayar
mayar

Posted on

Behind the Scenes: Understanding the Inner Workings of Node.js

Unveiling the Inner Workings of Node.js: Journey into the heart of this remarkable runtime environment and discover how its event-driven architecture and non-blocking I/O model revolutionize web development.

Node.js is the JavaScript runtime environment which is based on Google’s V8 Engine , with the help of Node.js we can run the JavaScript outside of the browser.

How Node.js Combines Chrome's V8 Engine and Libuv ?

Node.js is built upon the powerful Chrome V8 engine, which handles the execution of JavaScript code. It also utilizes Libuv, a C library, to provide support for managing asynchronous I/O operations and events within event loops and thread loops. Despite being written in C or C++, Node.js can still be utilized purely with JavaScript, offering developers a familiar language for building applications.

Node.js's Single-Threaded Magic

The first question that often arises is how Node, being single-threaded, can effectively handle multiple requests simultaneously πŸ€”

also how can we mitigate the impact of potentially blocking code in a single-threaded Node.js application to ensure uninterrupted performance ?
So, to keep the Node.js application running, asynchronous code must be used everywhere having callback functions because as we know that asynchronous code keeps on running in the background and the callback gets executed as soon as the promise gets resolved rather than synchronous code which blocks the whole application until it gets finished executing, asynchronous code is managed and executed within Event-loop. Event-loop :is what allows Node.js applications to run non-blocking asynchronous I/O based operations .
The whole idea behind the event-loop is that it works on Event-driven architecture. Event-driven architecture : a powerful approach to software design that can improve scalability, flexibility, and performance.

So what about a synchronous code in node js applications ,
we can use our synchronous code which is in this case known as Top-Level code.

Here's an example to illustrate the difference between synchronous and asynchronous code in a Node.js application

// Synchronous Code Example
const fs = require('fs');

// Read file synchronously
const data = fs.readFileSync('file.txt', 'utf8');
console.log('File content:', data);

console.log('Application finished.'); // This line is not executed until the file is read

// Asynchronous Code Example
const fs = require('fs');

// Read file asynchronously
fs.readFile('file.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log('File content:', data);
});

console.log('Application finished.'); // This line is executed immediately after the file read operation

Enter fullscreen mode Exit fullscreen mode

What will happen if some tasks are too heavy to be executed in our event-loop

The tasks that need more processing power and shouldn't slow down the main task are given to a group of extra threads called the thread-pool, which is provided by Libuv. These threads can handle heavy tasks without blocking the main thread. You can increase the number of threads if needed, and you don't have to specifically mention which tasks should be offloaded because the event-loop takes care of it automatically. However, you have the option to specify the number of threads if you want.

Multi threading and multiple process in Node.js

Answer to the first question πŸ’‘
Node.js effectively handles multiple requests simultaneously by leveraging its event-driven, non-blocking I/O model, which allows it to process requests asynchronously and continue executing other tasks while waiting for I/O operations to complete.

Top comments (0)