I often hear Node runs in only one thread. However, the thing I don't understand is how node can do non-blocking with only one thread. Let's say there are 100 concurrent requests coming to a node web server, the server generates 100 callbacks to handle the requests. If each of the 100 callbacks takes 1 second to complete, and if all of them are in 1 thread, they have to be done in series, does it mean it will block for 100 seconds?
-
2Node.js isn't single threaded. That's kind of a misleading statement. Just the js engine runs on a single thread. But, there is a thread pool elsewhere. Have a look at this http://rickgaribay.net/archive/2012/01/28/node-is-not-single-threaded.aspx – om_deshpande Nov 26 '13 at 18:05
-
Thanks @om_deshpande. That makes real sense to me. So actually the callbacks are simply NOT running in the same thread as the main thread. This explanation totally makes sense. – Nov 27 '13 at 09:22
-
1Exactly! The concepts to describe node.js correctly are "event loop" and "asynchronous " NOT "single threading" – om_deshpande Nov 27 '13 at 13:16
-
1These comments are slightly misleading. Your nodejs programme including all npm libraries will run in one thread. Thus it is called single threaded. But V8 engine and all IO run in separate threads (simplified). – ivoszz Nov 27 '13 at 18:42
1 Answers
From the blog Understanding the node.js event loop
The largest waste with current programming technologies comes from waiting for I/O to complete.There are several ways in which one can deal with the performance impact (from Sam Rushing):
- synchronous: you handle one request at a time, each in turn. pros: simple cons: any one request can hold up all the other requests
fork a new process: you start a new process to handle each request. pros: easy cons: does not scale well, hundreds of connections means hundreds of processes. fork() is the Unix programmer’s hammer. Because it’s available, every problem looks like a nail. It’s usually overkill
threads: start a new thread to handle each request. pros: easy, and kinder to the kernel than using fork, since threads usually have much less overhead cons: your machine may not have threads, and threaded programming can get very complicated very fast, with worries about controlling access to shared resources.
Node.js keeps a single thread for your code…
It really is a single thread running: you can’t do any parallel code execution; doing a “sleep” for example will block the server for one second:
while(new Date().getTime() < now + 1000) {
// do nothing
}
So while that code is running, node.js will not respond to any other requests from clients, since it only has one thread for executing your code. Or if you would have some CPU -intensive code, say, for resizing images, that would still block all other requests.

- 10,882
- 10
- 60
- 81
-
Excellent article, thanks for sharing @Damodaran. By reading the article, I have the feeling the callbacks are running in different threads than the main thread. Let's make the question extremely explicit, if we have a callback doing db query, another callback doing file access, now the questions is: are the main loop/thread and the db query call back, and the file access callback on earth running the in the same thread? – Nov 26 '13 at 18:13
-
@ivoszz, if they are in the same thread, they are to be executed in series. Is this statement correct? If this statement is correct, how do they not block each other? Thanks. – Nov 27 '13 at 09:19
-
@ElgsQianChen Refer this http://stackoverflow.com/questions/8243682/multi-threading-how-does-concurrent-threads-work – Damodaran Nov 27 '13 at 09:31
-
Thanks @Damodaran, the questions seems to be my understanding about the concept of "thread" now. I learned the concept of "thread" from Java. My understanding is that tasks in a thread have to be executed in series. My initial question is depending on my understanding about the concept of "thread". If my understanding of "thread" is correct, then I still am not convinced that the callbacks are running in the same thread as the main thread. However, if my understanding regarding "thread" is wrong, I should hold my question and go back to do some more study. – Nov 27 '13 at 09:41
-
@ElgsQianChen Your comments make me google and I am also learning Thanks. Check this post http://stackoverflow.com/questions/14795145/how-the-single-threaded-non-blocking-io-model-works-in-node-js – Damodaran Nov 27 '13 at 09:54
-
@ElgsQianChen, yes, they will be executed in series. Similar concept is sometimes called cooperative multitasking or corutines. It is used in Lua programming language also. The idea is that code (corutines, callbacks) has responsibility to not occupy CPU for a long time and switch to another task frequently, usually doing IO or calling process.nextTick (in Node). – ivoszz Nov 27 '13 at 18:08
-
2This concept has one big advantage. Your code can't be interrupted. This means you don't have to worry about concurrency in your libraries. – ivoszz Nov 27 '13 at 18:14
-
@ivoszz agreed! Coding in the all non blocking way is very confortable and much less pain. – Nov 27 '13 at 18:36