0

I have a nodejs application that gets data from one server and pushes into another. For testing I sent 1000 requests to my node server and saw what happens on the system monitor. There I could see that all 4 processors were 100% occupied.

Now, from what I have read on nodejs, it seems that it by default uses only 1 thread(which means 1 processor?). But how come all my computer's processors were occupied? Is this load balancing happening at OS level(I am on ubuntu 14)

And in case the balancing was done by OS then what is the difference between this automatic OS level load balancing and explicitly using clusters to divide the load? What are the advantages/disadvantages of each?

Any help would be deeply appreciated :)

harsh atal
  • 411
  • 6
  • 16
  • ¿How is exactly your setup?, While node uses only 1 thread, some modules that you may be using could be using multiple threads. If you have a nginx or apache server it could be automatically using a thread per request. I doubt it has anything to do with the OS – angrykoala Jul 20 '16 at 09:29
  • There is no nginx or any other server....I am using simple node app with express listening at 8000 port. I am using following modules - Q, moment, express,request and log4js. – harsh atal Jul 20 '16 at 10:08

1 Answers1

0

Though the application is driven by a single thread, there are helper threads inside node to facilitate the execution within the runtime environment. Examples are JIT compiler thread and GC helper threads. Though they wont consume CPU in proportion to the application load, they will be driven by the characteristics internal to the virtual machine.

Hooking onto a live debugger shows how many threads are there any what they are doing:

gdb) info threads
  6 Thread 0x7ffff61d8700 (LWP 23181)  0x00000034d080d930 in sem_wait () from /lib64/libpthread.so.0
  5 Thread 0x7ffff6bd9700 (LWP 23180)  0x00000034d080d930 in sem_wait () from /lib64/libpthread.so.0
  4 Thread 0x7ffff75da700 (LWP 23179)  0x00000034d080d930 in sem_wait () from /lib64/libpthread.so.0
  3 Thread 0x7ffff7fdb700 (LWP 23178)  0x00000034d080d930 in sem_wait () from /lib64/libpthread.so.0
  2 Thread 0x7ffff7ffc700 (LWP 23177)  0x00000034d080d930 in sem_wait () from /lib64/libpthread.so.0
* 1 Thread 0x7ffff7fdd720 (LWP 23168)  0x00000034d04e5239 in syscall () from /lib64/libc.so.6
(gdb) 
Gireesh Punathil
  • 1,344
  • 8
  • 18
  • Thanks for your response Gireesh.If resources are well distributed even without clustering...then what is the use case for using cluster?? – harsh atal Jul 20 '16 at 10:22
  • Cluster is basically to balance the load on your application, where the single threaded mode cannot manage all the concurrent requests. The single threaded mode works well for the most common workloads, and in such scenarios, these additional threads are employed to make sure that the CPU intensive house-keeping activities (such as compilation and GC) do not meddle with the application code and un-neessarily slow down the request-response path. So bottom line is that if your web application anticipates large amount of concurrent requests, cluster mode is ideal. – Gireesh Punathil Jul 20 '16 at 11:16
  • https://github.com/nodejs/node/blob/9beef23b605a0eaaccf9d0ba680f6c5ada3e8c4f/deps/v8/src/libplatform/default-platform.cc#L80 – Gireesh Punathil Jul 20 '16 at 11:57
  • https://github.com/nodejs/node/blob/c5c28c3d50fa6bcde1329b855ca79d5b7c87c0d4/src/node.cc#L3901 – Gireesh Punathil Jul 20 '16 at 11:57
  • FYI - these are some examples of thread creation - from the real node source code. – Gireesh Punathil Jul 20 '16 at 11:57