Please enable Javascript to correctly display the contents on Dot Net Tricks!

Exploring Node.js Code Execution Process

Posted By : Shailendra Chauhan, 30 Dec 2015
Updated On : 06 Jul 2016
Total Views : 1,631   
 

By default, In Node.js all I/O operations like file I/O, database operations, network operations etc. are non-blocking. When these operations are completed, they are notified to the main thread using callback functions. You can also write blocking code for these operations using blocking version of Node.js functions which have Sync word as suffix.

Before going to explore Node.js code execution process, let's understand the blocking code and Non-blocking code with the help of example.

Blocking Code Example

The given code is a blocking code, since it will not continue the execution of the next lines of code until the read operation is completed.

var fs = require('fs');
//blocking code without callback function
var data = fs.readFileSync('text.txt','utf8'); //wait for reading
console.log(data);
console.log("Done!");
/* Output
 * Hello Node.js
 * Done!
*/

Un-blocking Code Example

The above blocking code can be converted into non-blocking code with the help of callback function. Hence, the given code will continue the execution of the next lines of code without waiting for read operation to be complete.

var fs = require('fs');
//un-blocking code with the help of callback function
fs.readFile('text.txt', 'utf8', function (err, data) {//doesn't wait and will read file asynchronously
 console.log(data);
});
console.log("Done!");
/* Output
 * Done!
 * Hello Node.js
*/

The Event Loop

Node.js execution model is based on JavaScript Event loop which is single threaded and based on JavaScript callback mechanism. The event loop facilitates the Node.js to handle concurrent requests. Let’s understand the Node.js execution model with the help of following diagram.

Node.js Event Loop

The steps of code execution are given below:

  1. Clients send theirs request to Node.js Server.

  2. Node.js Server receives those requests and places them into a processing Queue that is known as “Event Queue”.

  3. Node.js uses JavaScript Event Loop to process each client request. This Event loop is an indefinite loop to receive requests and process them. Event Loop continuously checks for client’s requests placed in Event Queue. If requests are available, then it process them one by one.

  4. If the client request does not require any blocking I/O operations, then it process everything, prepare the response and send it back to the client.

  5. If the client request requires some blocking I/O operations like interacting with database, file system, external services then it uses C++ thread pool to process these operations.

    Thread pool process steps are as:

    • Event loop checks for thread availability from internal thread pool. If thread is available, then picks up one thread and assign the client request to that thread.

    • Now, this thread is responsible for handling that request, process it, perform blocking I/O operations, prepare the response and send it back to the Event Loop.

    • Finally, the Event Loop sends the response to the respective client.

What do you think?

I hope you get the idea how Node.js code execution process work. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

 
Recommended for you
 
About the Author
Shailendra Chauhan

Shailendra Chauhan is an Entrepreneur, Author, Architect, and Corporate Trainer. He has rewarded as Microsoft MVP for his exceptional contributions in Microsoft Visual Studio and Development Technologies.

With more than 7 years in hand experience Shailendra Chauhan is a polymath in the domains of Microsoft .NET technologies and an array of other technologies including JavaScript, AngularJS, Node.js, Ionic and NoSQL Databases to name but a few.

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers , ASP.NET MVC Interview Questions and Answers , AngularJS Interview Questions and Answers and LINQ Interview Questions and Answers. Furthermore he is a technical reviewer for book on ASP.NET MVC 4 Mobile App Development. Know more...
 
Free Interview Books
 
10 SEP
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN 05:00 PM-06:30 PM IST

More Details
7 SEP
ASP.NET MVC with AngularJS Development (online)

Weekdays (Mon-Fri) 07:30 AM-09:00 AM IST

More Details
27 AUG
ASP.NET MVC with AngularJS Development (online)

Weekend (Sat, Sun) 03:00 PM-05:00 PM IST

More Details
27 AUG
PPC Marketing (offline)

Sat, Sun 03:00 PM-05:00 PM IST

More Details
26 AUG
NodeJS Development (online)

MON-FRI 06:00 AM-07:30 AM IST

More Details
20 AUG
NodeJS Development (offline)

Sat, Sun     11:00 AM-12:30 PM IST

16 AUG
NodeJS Development (online)

Mon-Fri     09:00 PM-10:30 PM IST

12 AUG
ASP.NET MVC with AngularJS Development (online)

Mon-Fri     09:30 PM-11:00 PM IST

5 AUG
AngularJS Development (online)

Mon-Fri     08:00 PM-09:30 PM IST

1 AUG
ASP.NET MVC with AngularJS Development (online)

Mon-Fri     07:30 AM-09:00 AM IST

24 JUL
AngularJS Development (offline)

Sat,Sun     08:00 AM-09:30 AM

24 JUL
ASP.NET MVC with AngularJS Development (offline)

Sat, Sun     09:30 AM-11:00 AM

14 JUL
PPC Marketing (offline)

Sat, Sun     12:00 AM-02:00 PM IST

11 MAY
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

BROWSE BY CATEGORY
 
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 
+