Codementor Events

NodeJS is Non-Blocking I/O Model ? What does this mean ?

Published Feb 19, 2020
NodeJS is Non-Blocking I/O Model ? What does this mean ?

Often when we start learning any new technology or framework we often hear a ton of jargon from people who more experienced than us in that field. These concepts even though trivial in nature, create uncertainty in regards to learning the technology because of its goliath length . In the coming blogs I will be talking about the same and will break it down into easily consumable date for your brain.

A Normal Blocking I/O Code

// assume myAPI to be an API call
console.log("API Call 1");
var data1 = myAPI('123');
console.log("Data is:", data1);

console.log("API Call 2");
var data2 = myAPI('456');
console.log("Data is:", data2);

var sum = 1 + 2;
console.log("sum is:", sum);

Now, if we consider this example the flow of control will be like this-

  • the control reaches the first API call and it remains at the same place until the API request results in a response.
  • after it gets the first response then only the control reaches the second API request and the same procedure as above is followed
  • now even though the calculation of sum had nothing to do with the above two API response yet it had to wait for both API requests.

Note – [for people knowing about multi-threading]

Yes, we can avoid this by using multiple threads in a language that supports it. But it leads to an overhead on system resources.

NodeJS aims to solve this problem by handling the API call asynchronously.

Another jargon, Yeah Sorry!

So Asynchronously basically means the system can continue with further execution and when the response is received the program will let the system know. This can be easily achieved in NodeJs by simply using callback functions.

A Non-Blocking I/O Code

console.log("API Call 1");
myAPI('123', function(response) {
  console.log("Data is:", response);
});

console.log("API Call 2");
myAPI('456', function(response) {
  console.log("Data is:", response);
});

var sum = 1 + 2;
console.log("sum is:", sum);

Now, if we consider this example the flow of control will be like this-

  • the control reaches the first API call and continues the execution
  • the control reaches the second API request and continues the execution
  • the sum is calculated and displayed
  • as and when the API calls yield a response the callback function associated with them is executed.

Lets see the output of each of them –

CASE 1-

API Call 1

Data is: {“id” : 123, “name” : “Aryan”}

API Call 2

Data is: {“id” : 456, “name” : “Jai”}

sum is:3

CASE 2-

API Call 1

API Call 2

sum is:3

Data is: {“id” : 123, “name” : “Aryan”}

Data is: {“id” : 456, “name” : “Jai”}

An Analogy – >

Blocking IO: The waiter takes the order, he walks to the kitchen and requests the food. The kitchen staff then create the dish. The whole time this is happening the waiter can’t do anything else as he is waiting for the food to be cooked.

Non-Blocking IO: A similar restaurant setup. The waiter is still taking orders, but here he is passing the order to the kitchen as a message, and then forgets about it.

CONCLUSION

Now we can clearly see that the later yielded a better performance. The API Call here could also have been an Input / Output operation.

Thus NodeJs gets its name Non-Blocking I/O model. Several other languages also support async operations now.

Would love to hear about any other technical jargon that you heard and want to know more about. Drop them in the comment box. The next blog can be on that.

Peace.

Discover and read more posts from Aryan Gupta
get started