Habilelabs-Logo
Blog

Node.js – Blocking & Non-Blocking and Asynchronous Operations

November 19th, 2020 . 5 minutes read
Blog featured image

Node.js is a powerful framework for building back-end systems which can scale very well. It was developed on Chrome’s V8 JavaScript Engine that compiles the JavaScript directly into the native machine code.

Nodejs framework is used for developing server-side applications and extends JavaScript API to provide the server-side functionalities. The popular uses of NodeJS include developing Single Page Applications, Video Streaming Sites and other web applications.

The Asynchronous coding paradigm enables us to write a non-blocking code. This helps the single threaded JavaScript run with full efficiency. A Single thread is a thread in node.js that can do only one thing at a time. Nodejs uses this Single thread to get non-blocking execution.     

This blog provides the basic introduction of Nodejs with some of its major features and also the detailed description of Blocking, Non-Blocking, Asynchronous Operations, and Callbacks-

Major Features of Node.js:

Node.js is an open source framework that has a huge fan following. The Nodejs community is quite active and have contributed a lot to add new capabilities in its long-list of features.

  • Simple and Fast: The Node.js libraries are capable of faster & easier code execution.
  • Asynchronous: All Nodejs libraries are asynchronous, which means the node.js based servers move on to the next API without waiting for an API to send back the response.
  • Single Threaded: Node.js follows a single-threaded model that means a single program is able to handle multiple requests through event looping.
  • No Buffering: One of the major features of Node.js is that it never buffers any data.
  • Cross platform: Nodejs can easily be built and deployed on various platforms like Windows, Mac and Linux.

Also Check- Hydra Microservice Library in Node.js

Now, we will understand Blocking and Non-blocking in node.js-

Blocking and Non-blocking in Node.js:

In this example code, the task1() function executes first, and once it returns, the task2() function executes. This is not a blocking code in JavaScript even if the task1() takes more time before returning (like it performs some CPU intensive task such as finding the inverse of a matrix).

Blocking in Nodejs

When JavaScript execution in a node.js process (Each program is a process) waits until a non-JavaScript operation completes, its execution is what we call ‘blocking’ in node.js.

Non-Blocking in Nodejs

In contrary to the Blocking process, when JavaScript execution in node.js process(Each program is a process) do not wait for the non-JavaScript operation to complete is called non-blocking.

The non-JavaScript execution refers to mainly I/O operations as they are blocking for they, firstly, refers to the interaction with the system’s disk and network.

Let’s take another example here –

Non-blocking Process

As you can see in the above image, task1 internally calls getAllUsers which sends a request to the database to fetch all the users. The task2 internally calls getAllServices which makes an HTTP request to get all the services of some third party through their API.

Here, the task2() blocks until task1() returns because a single thread can execute only one thing at a time.

Now, you must know how node.js can convert the blocking calls into non-blocking execution.

How to Convert Blocking Calls to Non-Blocking Ones?

It uses the Event Loop and callback mechanism to move the I/O operations from the JavaScript thread to the system’s kernel.

Most of the System kernels are multi-threaded, and thus, can handle multiple requests and execute operations in the background concurrently. When one of these operations’ execution completes, the kernel informs to node.js and then the callback may be added to the queue to be executed.

So, here we convert the blocking code into the non-blocking code using callbacks.

Blocking code into non-blocking code

In the above code, functions getAllUsers and getAllServices take callbacks. Node.js uses this callback mechanism when the kernel finishes with the I/O operations.

Structure of Callback in Node.js

A ‘Callback’ is a JavaScript function which is called when a given task completes. There are certain conventions for these callback functions:

  • It passes as the last parameter to any function, after the function is done with all of its operations.
  • All Callback functions have error value as a first parameter. If there is no error, then the first parameter is set to be null and the rest being return value.
Structure of Callback

Now, you must understand that node.js is able to make asynchronous calls for non-blocking operations using the callback mechanism.

It makes NodeJS very fast and efficient, one of the reason in Nodejs that no function directly performs I/O. So, the process never blocks. Using callbacks in these asynchronous calls allows you to have as many I/O operations as your OS can handle, happening simultaneously.

  • The non-blocking code invariably takes a callback function as a parameter.
  • Some of the blocking equivalent names usually ends with Sync. These functions execute synchronously and blocked the code.

Let’s take an example of Synchronous & Asynchronous modes using file system-

Synchronous File Read

Synchronous File Read

In the Synchronous version, if an error has occurred then it must have to be caught else, the process will be crashed.

Asynchronous File Read

Asynchronous File Read

In the asynchronous version, the developer is responsible for choosing the way of error handling. We should always use non-blocking variant over the blocking one.

Conclusion:

Node.js is an outstandingly flexible and customizable JavaScript framework. Due to its high popularity in the IT industry, more and more developers are getting attracted towards it over all other frameworks available out there.

The node.js web applications load faster as compared to the other frameworks. It provides several benefits to the developers which is basically the actual reason why some of the best companies around the world use node.js for their projects.

For more interesting blogs, subscribe to our Medium page here.

Reach out to us if you want to discuss your Node.js project, we assure you will find our work beyond satisfactory.

Happy coding!!

Author: Rishabh Pachori
Share: