1

I have an a big NodeJS 7.2.0 application that require a lot of cpu power. I started diving it to separate applications in order for each process to use a different CPU.

i have a server on amazon with 8 Virtual CPUs.

I've been told lately that each nodejs process uses CPU0 by default and there is no way to change that, which means that diving my big application so as many microservices as I want wouldn't really resolve my issue.

is that really the case? will all nodejs processes use CPU0 by default? is there a way to change that ?

any information regarding the issue would be greatly appreciated.

thanks

ufk
  • 30,912
  • 70
  • 235
  • 386
  • 3
    I can't say whether Amazon is doing something special with it's virtual CPUs, but a Linux OS will automatically spread different apps out to different actual CPUs. They won't be locked to a particular CPU by default, but if all the apps are busy executing and there are enough CPUs to go around, they will each be on a separate CPU. This is the point of node-clustering, it allows your node.js clusters to each execute on a separate CPU whereas a single node.js process will mostly only use one CPU. – jfriend00 Nov 24 '16 at 17:20
  • @jfriend00 - thank you for your answer, so you're saying I don't really need to restrict each nodejs process to a different cpu ? the os will divide the processes among the cpus where required ? – ufk Nov 24 '16 at 17:25
  • 1
    Yes, the OS will naturally spread different processes out among different CPUs. You speak of Amazon virtual CPUs so I can't say how that might be different than a plain OS with real CPUs. Amazon might be limiting you somehow. – jfriend00 Nov 24 '16 at 17:29

1 Answers1

1

I don't think you can choose which cpu to run your process in the node.js side. However you can set the cpu priority in the OS level.

Ref: taskset equivalent in windows

Another solution is using the node cluster. https://nodejs.org/api/cluster.html

Community
  • 1
  • 1
iKoala
  • 870
  • 4
  • 11
  • hi! :) thanks for your answer. my server is a linux server, i read the taskset equivalent in windows, and it helped me understand I can restrict a process to a specific cpu which is great. i also read the cluster api, and I didn't quite understand how there I can control which process will go to which cpu. i don't quite understand the multicore cpu benefits of using node-cluster over running several processes on pm2. – ufk Nov 24 '16 at 17:15
  • 1
    Because a node.js single instance run in single thread, if you use cluster to spawn 8 worker instance, each instance will use 1 cpu in your case, i.e. optimise multicore cpu usage. (assuming your process can run in cluster mode) If all your processes run in cluster mode, all the process will be shared equally (in theory) among all the cpus. – iKoala Nov 24 '16 at 17:23
  • i prefer the micro-services method which each process runs on it's own without using node-cluster, are you saying that node-cluster will have a better multi-core usage then running several processes individually ? – ufk Nov 24 '16 at 17:25
  • No I didn't mean that. The best solution really depends on your use case and application requirement. – iKoala Nov 24 '16 at 17:28
  • your answer and @jfriend00 comments helped me to confirm that my direction of nodejs micro-services is the right direction. and in case i still want to restrict a process to a specific cpu, I can. thanks – ufk Nov 24 '16 at 17:34