
How does single-threaded node performs better than multi-threaded apache/nginx? - rms_returns
I need some clarity on understanding Node.JS. To the best of my knowledge, my entire server.js app works on an event-based, non-blocking but single-threaded model. Unlike apache, it doesn&#x27;t spawn a new thread for each new request, right?<p>But if you consider the below code:<p>setTimeout(function () {
	     for (var i = 0; i &lt; 1000000000; i++){}
	}, 1000);<p>Imagine that the above code is inside my server script. I&#x27;ve placed a 1 second timeout which is non blocking, but after that I&#x27;ve placed a great loop that blocks the entire thread for a long time.<p>In between that loop, if a new request comes, then what will happen? Node.JS will create a new instance of my server, right? So, Node.JS itself can&#x27;t be single threaded because its already busy with my loop. So overall, how many threads is my entire app (including the V8 engine) consuming?
======
mdomans
Node.js internally has a small pool of threads. Processing of response/request
is by design handled in one thread designated to handle event loop.

Therefore - if your handler blocks, the _handling_ of requests will be
blocked. Your server will still be able to ingest new requests.

The downside of this is that requests are stored internally in a queue so if
your handler blocks for a long time, you can bring the whole node down.

Just in case you want a longer read on this:
[https://www.toptal.com/nodejs/top-10-common-nodejs-
developer...](https://www.toptal.com/nodejs/top-10-common-nodejs-developer-
mistakes)

Or if you want to go really bare internals:
[http://www.journaldev.com/7462/node-js-processing-model-
sing...](http://www.journaldev.com/7462/node-js-processing-model-single-
threaded-model-with-event-loop-architecture)

------
vectorEQ
This might be interesting for you. Sorry not Node expert myself, but it seems
to touch some of your question.
[http://blog.mixu.net/2011/02/01/understanding-the-node-js-
ev...](http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/)

