Open In App

How Node.js overcome the problem of blocking of I/O operations ?

Last Updated : 16 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Node.js uses non-blocking I/O, the mechanism that allows you to have a single thread of execution running your program. If Node.js had to use blocking I/O, you wouldn’t be able to do anything else while waiting for an I/O to complete. Below is an example image of what happens when Node.js needs to use blocking I/O:

Blocking vs Non-blocking I/O operation

The Node.js project includes a JavaScript engine, event loop, and I / O layer. It is often referred to as a non-blocking web server.

Note: “I/O” generally refers to the interaction between the system’s disk and network and is supported by libuv.

Why was blocking I/O operations an issue?

In traditional programming languages ​​like C and PHP, all of the instructions are blocked by default unless you explicitly “enable” it to perform asynchronous operations. Let’s say you make a network request to read some data or you may want to read some file and want to display its data to the user, but then the execution of that said thread is blocked, until the answer response is ready but to solve this problem to of blocking of I/O stream JavaScript allows you to create asynchronous and non-blocking code in a very simple way, by using a single thread, call-back functions, and event-driven programming.  

Let us see with an example to better understand this!

Example 1: Following example uses the readFileSync() function to read files and demonstrate Blocking in Node.js:

Javascript




// The fs module provides access to 
// interact with the file system.
  
const fs = require('fs');
const data = fs.readFileSync('/sample.txt');
  
/*This line of code will block the main thread, 
  until the file is read.*/
  
console.log(data);
console.log("This is a beautiful message");
console.log("This code is doing some great work")


Output:

This data is from the text file
This is a beautiful message
This code is doing some great work

Explanation: In the above example, we see that the Blocking method executes synchronously [or you can say line by line]. Now you see that each line of code waits for the previous line to be executed for the result which can become a problem especially with slow operations like reading or updating or you can say operation related to I/O, because each line blocks the execution of the rest of the code and we say that it is blocking code as the next line of code can only be executed after the one before has finished and because how Node.js was designed this turned out to be a huge problem which leads us to the non-blocking methods which execute the code asynchronously means that the code is not executed line by line but rather callbacks are used to achieve this non-blocking behavior. Below is the same example, we used above to describe synchronous behavior but now we have modified it using the Call-back function to make it asynchronous.

Example 2: Following example uses the readFile() function to read files and demonstrate Non-Blocking in Node.js

Javascript




const fs = require('fs');
  
// This piece of code doesn't block the main thread
// but will run after the file is completely read.
  
fs.readFile('/sample.txt', (err, data) => {
    if (err) throw err;
    else {
        // Call-back function
        console.log(data);
    }
});
  
console.log("This is a beautiful message");
console.log("This code is doing some great work")


Output:

This is a beautiful message
This code is doing some great work
This data is from the text file

Note: In the above example, we see that in the non-blocking method the console, actually prints the messages before the content of the file. This is because the program does not wait for the readFile() function to return and move to the next operation making it async. And when the readFile() function returns it prints the content of the text file.

How did node.js solve the problem?

You may have heard about this concept called non-blocking I/O and how Node.js uses it to solve the problem of blocking calls and to run super-fast, but what non-blocking I/O is, and why does it help? We will understand this later but first, you need to understand how servers and threads work along the way and how servers handle requests, Before moving onto node.js let’s talk about servers and threads as an overview.

The server is nothing but what takes requests, and do some work to calculate the response needed to be sent back, For example when you get to google.com you send an HTTP request to a Google server that calculates the HTML response you should see on your browser homepage. But on the inside the server is splitting the work between one or more threads inside [a thread can be considered as a single worker], processing the requirements and requests of other users at the same time without blocking the main thread.

We can explore this using a restaurant as an analogy, here in a frequently visited restaurant there is only one waiter because it is not very popular, the customer tends to come one by one the waiter serves each party before moving on to the next one as the waiter finishes a party it is either waiting for more customers or switching to another this example works well for describing servers.

Synchronous vs Asynchronous

The restaurant we just mentioned is like a server with the frequency of getting requests and the waiter is just like a thread and the request is like customers.

Now imagine if two parties enter the restaurant at the same time and a waiter can only serve one person at a time, but that’s not the case, see while the customers are busy with the menu they don’t need your help, so the waiter can move or you can say switch between tables and help both at the same time as each needs help without making someone stand or wait for their turn.

Well, what happens when both of the friends need help at the same time, then one needs to wait a little longer than if it’s just one party. The basic idea here is that a waiter can serve more than one table at a time because each table has some downtime.

All of this applies to servers and Node JS as well!

Just like a table we need help with time request processing, there is also a part that requires active attention and the part that does not require active attention generally it is called CPU work as it requires the central processing unit of the computer where we spend time thinking and calculating results.

The CPU work requires a thread to process it like a table that requires a waiter to process it, but the part that doesn’t require activation attention is called I/O because it was waiting for something else to provide input or send output.

Here’s the exit part, just as the waiter can save time by changing the table, the same blocking allows the thread to sabotage time by changing the request, when a request makes an I/O it greatly increases the amount of effective work the I/O can do!

Note: So, the solution to this problem in node.js is to use asynchronous non-blocking code and Node.js uses an event loop for this. “An object that handles and processes external events and converts them into call-back calls” is what an event loop is.

Non-blocking I/O event-loop

When data is needed, Node.js logs a callback and sends the action to this event loop. The callback is called when data is available. So basically, we are offloading the heavy workload to run in the background, and then when the job is done a callback function is called to process the result, we saved earlier, and during this time the rest of the code is still executable while the currently blocked heavy task is running in the background.

In a nutshell:

Everything runs parallel, except your code and we always try to pass a call-back function that will be called once we finish with the task and we continue with the processing. We don’t wait for this to end before we continue with the rest.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads