Blocking and Non-Blocking Operations In Nodejs

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.1173.0.1.1205.250.255.0

Contents of users file :

paulsmithdogSnakevar fs = require('fs'); var contents = fs.readFileSync('users','utf8'); console.log(contents); console.log("Welcome Node\n"); var contents = fs.readFileSync('hosts','utf8'); console.log(contents); console.log(“Welcome again!”);

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

paulsmithdogSnakeWelcome Node190.158.0.1173.0.1.1205.250.255.0Welcome 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.1173.0.1.1205.250.255.0

Contents of users file :

paulsmithdogSnakevar fs = require('fs');var contents = fs.readFile('./users','utf8', function(err,contents){   console.log(contents);});console.log("Welcome Node\n"); 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 NodeWelcome again!paulsmithdogSnake190.158.0.1173.0.1.1205.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.

Ecommerce consultant

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store