Blocking and Non-Blocking Operations In Nodejs
By: Haribabu Gopal
Tags: Nodejs, JavaScript, Node Js Tutorial, Node Js Development

In this article, we try to provide you a detailed illustration of the difference between blocking and non-blocking calls in Nodejs. This blog will refer to the event loop and libuv.

BLOCKING AND NON-BLOCKING IN NODEJS

A blocking or non-blocking operating model is a process that deals with input/output operations.

Reading from a source or writing to a resource is considers as an I/O operations. “I/O” refers to the interaction with the system’s disk and network supported by libuv module.

On the other hand, the random-access memory (RAM) has the ability to access files from any location in the same amount of time irrespective of its physical location. This memory can be addressed quickly and doesn’t require waiting. So accessing memory is not considered as an I/O operation.

The most common tasks carried out by most of the developers is File I/O, this fundamental process has three processes:

Open a File.

Read its contents and print.

Executing something.

BLOCKING

A Blocking term is originally originated from the operating system process model. It is actually a multitasking operating system, each labeled process with a state depending on how ready those processes are to be put on the CPU for execution.

When JavaScript execution in the Nodejs process has to wait until a Non-JavaScript operation to complete is called blocking.

A process is labeled as blocked if it is not ready for execution. So it will wait for an event to occur. Each Input/Output event indicates either progress or completion in an I/O operation.

Performing a blocking system call forces the entire process to enter into the blocked state.

Let’s see how this can be done with blocking code in Nodejs:

Here we try to read simple files: hosts and users and printing their contents, meanwhile printing a few welcome messages.

BLOCKING/SYNCHRONOUS CODE :

Contents of the hosts file :

190.158.0.1 173.0.1.1 205.250.255.0

Contents of users file :

paul smith dog Snake var fs = require('fs'); var contents = fs.readFileSync('users','utf8'); console.log(contents); console.log("Welcome Node

"); var contents = fs.readFileSync('hosts','utf8'); console.log(contents); console.log(“Welcome again!”);

Let’s see the output down here for Blocking/Synchronous code :

paul smith dog Snake Welcome Node 190.158.0.1 173.0.1.1 205.250.255.0 Welcome again!

By relating to the output code, we may come to know about some major points:

It’s a blocking code

We see the contents of the user file, the ‘Welcome Node’ strongly support the above point.

The same happens with the next file.

NON BLOCKING

A non-blocking operation in Node js does not wait for I/O to complete. Whenever a blocking operation happens in the process, all other operations will put on hold at the operating system level. Performing such non-blocking I/O operation, the process continues to run in the non-blocking mechanism.

A non-blocking call initiates the operation and leaves it for OS to complete returning immediately without any results.

Let’s see how this can be done with non-blocking code in Nodejs:

Here we try to read simple files: hosts and users and printing their contents, meanwhile printing a few welcome messages.

Read: What is callback function in Node.js?

NON BLOCKING/ASYNCHRONOUS CODE:

Contents of the host’s file :

190.158.0.1 173.0.1.1 205.250.255.0

Contents of users file :

paul smith dog Snake var fs = require('fs'); var contents = fs.readFile('./users','utf8', function(err,contents){ console.log(contents); }); console.log("Welcome Node

"); var contents = fs.readFile('./hosts','utf8', function(err,contents){ console.log(contents); }); console/log(“Welcome again”);

Let’s see the output down here for Non-blocking or Asynchronous code :

Welcome Node Welcome again! paul smith dog Snake 190.158.0.1 173.0.1.1 205.250.255.0

By comparing with the previous sync code output, we can conclude the below points with async code :

The above-executed codes show a non-blocking output

CONCLUSION

We hope that this blog helped you in a good way. Still, confused with the process? Please Contact our Nodejs experts for more details regarding the development process.