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

Image for post
Image for post

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

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 :

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 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:

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

Written by

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