Node.js Non Blocking Nature

I am new to node.js and still trying to understand the philosophy behind it . As I learned , node.js runs in one process only as opposed to php which opens a process\thread for each request. And while you can say Node is 'non-blocking' for i/o , it is blocking for requests(the requests pile up since there is no new thread for each new request) and theoretically if you've written a node.js app that doesn't deal fast with each request you get in trouble.

My question is this - how can I tell if a certain handling of a request takes too long so that it will block all other requests for too long and hinder the performance of my app ?

I know that all the 'heavy' actions on the server (db querying , file system searching) are done by callbacks and therefore cannot block node. However what if all other operations that are done synchronously by the server to handle a request just take too long ?

For example- the server needs to write a lot of html to the response . What happens then ?

How can the node programmer know if he's doing too much with a certain request (in a blocking manner) , is it experience , intuition or are there clear guides on how to do it ?

There are no clear guidelines as to where is the limit between synchronous code and asynchronous code, it is more of a matter with the application flow. Asynchronous operations should be preferred since they allow Node.js main process to start handling other requests in the meantime.

That said, simply using callbacks for every function is not a solution since a piece of code as such:

function sum(a, b, callback){
   var sum = a + b;
   callback(sum);
}

sum(2,3, function(sum){
   console.log(sum);
}

Is still synchronous. To make it asynchronous process.nextTick could be used as such:

function sum(a, b, callback){
   var sum = a + b;
   process.nextTick(function(){
     callback(sum);
   });
}

sum(2,3, function(sum){
   console.log(sum);
}

The general rule of thumb is to avoid synchronous recursive calculations, heavy loops and IO operations.

Finding out if a request takes too long and thus will hinder the performance can not be defined so generally as the limits are application specific. Those request are located by running performance tests on the application.