Node: one core, many processes
Asked Answered
S

1

5

I have looked up online and all I seem to find are answers related to the question of "how does Node benefit from running in a multi core cpu?"

But. If you have a machine with just one core, you can only be running one process at any given time. (I am considering task scheduling here). And node uses a single threaded model.

My question: is there any scenario in which it makes sense to run multiple node processes in one core? And if the process is a web server that listens on a port, how can this ever work given that only one process can listen?

Subedit answered 10/8, 2016 at 3:53 Comment(0)
C
13

My question: is there any scenario in which it makes sense to run multiple node processes in one core?

Yes, there are some scenarios. See details below.

And if the process is a web server that listens on a port, how can this ever work given that only one process can listen?

The node.js clustering module creates a scenario where there is one listener on the desired port, but incoming requests are shared among all the clustered processes. More details to follow...

You can use the clustering module to run more than one process that are all configured to handle incoming requests on the same port. If you want to know how incoming requests are shared among the different clustered processes, you can read the explanation in this blog post. In a nutshell, there ends up being only one listener on the desired port and the incoming requests are shared among the various clustered processes.

As to whether you could benefit from more processes than you have cores, the answer is that it depends on what type of benefit you are looking for. If you have a properly written server with all async I/O, then adding more processes than cores will likely not improve your overall throughput (as measured by requests per second that your server could process).

But, if you have any CPU-heavy processing in your requests, having a few more processes may provide a bit fairer scheduling between simultaneous requests because the OS will "share" the CPU among each of your processes. This will likely slightly decrease overall throughput (because of the added overhead of task switching the CPU between processes), but it may make request processing more even when there are multiple requests to be processed together.

If your requests don't have much CPU-heavy processing and are really just waiting for I/O most of the time, then there's probably no benefit to adding more processes than cores.

So, it really depends what you want to optimize for and what your situation is.

Compatriot answered 10/8, 2016 at 4:53 Comment(1)
This cleared some of my confusions. If I create a child process to handle CPU bound task then, that process will also have one thread for running event loop and four worker thread. Am I right?Sleuth

© 2022 - 2025 — McMap. All rights reserved.