Open In App

Blocking and Non-Blocking in Node

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Node is based on an event-driven non-blocking I/O model. This article discusses what does Blocking and Non-Blocking in Node means.

What is Blocking?

It refers to the blocking of further operation until the current operation finishes. Blocking methods are executed synchronously. Synchronously means that the program is executed line by line. The program waits until the called function or the operation returns.

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

Javascript




const fs = require('fs');
  
const filepath = 'text.txt';
 
// Reads a file in a synchronous and blocking way
const data = fs.readFileSync(filepath, {encoding: 'utf8'});
 
// Prints the content of file
console.log(data);
 
// This section calculates the sum of numbers from 1 to 10
let sum = 0;
for(let i=1; i<=10; i++){
    sum = sum + i;
}
 
// Prints the sum
console.log('Sum: ', sum);


Run the index.js file using the following command:

node index.js

Output:

This is from text file.
Sum: 55

What is Non-Blocking ?

It refers to the program that does not block the execution of further operations. Non-Blocking methods are executed asynchronously. Asynchronously means that the program may not necessarily execute line by line. The program calls the function and move to the next operation and does not wait for it to return.

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

Javascript




const fs = require('fs');
  
const filepath = 'text.txt';
 
// Reads a file in a asynchronous and non-blocking way
fs.readFile(filepath, {encoding: 'utf8'}, (err, data) => {
    // Prints the content of file
    console.log(data);
});
 
 
// This section calculates the sum of numbers from 1 to 10
let sum = 0;
for(let i=1; i<=10; i++){
    sum = sum + i;
}
 
// Prints the sum
console.log('Sum: ', sum);


Run the index.js file using the following command:

node index.js

Output:

Sum:  55
This is from text file.

Explanation: In the non-blocking program the sum actually prints 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. And when the readFile() function returns it prints the content.

How Concurrency and throughput is handled?

Node JS is single threaded so callback functions are sent to event loop where after completing other processes callback function is executed. To ensure concurrency event loop efficiently handles non-JavaScript operations such as I/O operations and concurrent code is executed aterwards.

Non-blocking asynchronous operations are much faster as compared to Blocking synchronous operations. This is different in JavaScript as most other languages create multiple threads to handle concurrency

Drawback of mixing Blocking and Non-Blocking Code

When doing multiple operations we may come across a situation where we accidentally combine blocking and non blocking operations which may lead some operations to execute even before their required operations are not completed. For example, if a file content is printed on the console even before the file has been read. To overcome this situation it is always preferred to write non-blocking code for related functions.



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

Similar Reads