0

This is a question about Node.JS's architecture. Node's biggest advantage is it's Non-Blocking nature. The means for instance if I have an Ajax Call which is trying to get something from a DB and that takes a long time, Node still is able to do other things without spawning a new thread for each task.

Ok, so far so good. But what happens if I don't have ajax calls that takes so much time? What happens if I have a web app, that really just does small post/gets? Where is the advantage in this case? As I understood, Apache spawns a new thread for each incoming connection. Node doesn't, but how is this possible? Especially when I think of long polling.. You see I feel like I am a bit lost.

Joe
  • 41,484
  • 20
  • 104
  • 125
Christian
  • 6,961
  • 10
  • 54
  • 82
  • See this question -> http://stackoverflow.com/questions/14795145/how-the-single-threaded-non-blocking-io-model-works-in-node-js – Lars Juel Jensen Feb 05 '14 at 12:07
  • Yeah, I read this. Still I don't get how Node takes the connections coming from the client. Let's say I have 1000 clients doing longpolling with my server. I then would need 1000 sockets. That stays open until data will be send back to the client or the client terminates the connection. In this case, has Node.JS any advantages over any other (web)server? – Christian Feb 05 '14 at 12:20
  • Using any other web server would still need 1000 sockets + the thread scheduler needs to handle 1000 threads (depends on your thread pool size). NodeJS event loop reads the non-blocking sockets' status continuously and can spend more CPU time transferring data instead of context switching and waiting for blocking I/O. – Lars Juel Jensen Feb 05 '14 at 12:39
  • A serving thread in Apache goes to sleep when blocking, and gets awakened when there is data available. In this awakened state it will have to wait for the thread scheduler to schedule it for execution in addition to the time it takes to switch execution context before it is eligible to run. – Lars Juel Jensen Feb 05 '14 at 12:44
  • To ask again. At a traditional Server, let's say Apache does every incoming connection means a new Thread. How is it possible that Node don't need a new Thread for each connection? – Christian Feb 05 '14 at 15:43
  • Simply because NodeJS is running through it's event loop over and over again, checking all socket status and serving those who are ready. You can say that the event loop decides which callback gets called when. No threads are involved, just one big loop which knows how to talk to the underlying OS and check for retrieval of data. When all the data for a socket has been read (I guess it reads a little from all of the open sockets in turn), it will call the corresponding callback method which will process the data and then return control to the event loop which will do everything all over again. – Lars Juel Jensen Feb 05 '14 at 21:40
  • This example -> http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Frzab6%2Frzab6xnonblock.htm - does kind of the same thing in C++. – Lars Juel Jensen Feb 05 '14 at 21:43
  • When you call the underlying OS blocking socket functions, the OS will block your call (the thread hangs). When you call a non-blocking socket function the OS will reply with a status code indicating that you can or can not read data at the moment.. – Lars Juel Jensen Feb 05 '14 at 21:45

0 Answers0