Mick
Mick

Reputation: 8922

Do multiple node apps run on different CPU cores?

If I start the same Node app multiple times, does each of it run on a different CPU core?

E.g. on a quad core I start the app 4 times.

I would like to use Nginx as a proxy which is more performant than Node's cluster function which PM2 uses in cluster mode. Also Nginx has an ip hash function for sticky sessions which PM2 does bot have.

Upvotes: 1

Views: 644

Answers (2)

jfriend00
jfriend00

Reputation: 707716

It's the OS that decides which process/thread runs on which core. If you have only four busy processes and four CPUs, the OS will generally spread out those four processes across the four CPUs. That is the job of the OS task scheduler and what it is trying to do.

But, none of this is hardwired. If the OS decides something else needs to run, then it will dynamically adjust. The goal of the OS is that each process that wants CPU cycles gets a fair share of the CPU and the only way it can do that is by spreading out the work-load from different processes among the different CPUs.

Linux and Windows each have some different characteristics on how exactly they spread the load, but they are both shooting for the same general goal of spreading out the load across the different cores.

Upvotes: 2

tadman
tadman

Reputation: 211670

Not necessarily. It depends on CPU architecture, OS, load and what the kernel decides. In some cases it's more efficient to run all the processes on one core and keep that core running at high-speed while the others are deliberately powered off.

If you're running them flat out, each using 100% CPU, then they'll probably run on different cores, but with things like Hyperthreading you can't count on it. They could be running on two cores, each with two threads.

Upvotes: 2

Related Questions