Nyxynyx
Nyxynyx

Reputation: 63687

Is This Single Node.JS App Using Multiple Cores?

On an Ubuntu 18.04 system running only 1 Node.js script that ingests a data feed, htop shows that both CPU cores are being utilized.

With 1 node script running: enter image description here

With NO node.js script running: enter image description here

This Node.js script has multiple event listeners that receives data, does some data processing and sends them to a remote database server.

foo.on('msg', msg => { setImmediate(() => do_work(msg)) } );

Question: Why does it appear as if both CPU cores are being utilized rather equally by Node, despite Node.js being single threaded? CPU load splits observed are 60%/40% and 50%/50%

Is it actually utilizing both CPU cores? Or simply switching between them really quickly all the time, but only really utilizing 1 core at any one time? In other words, such a scenario will cause the system to choke when the CPU workload is above 1 core's worth but lesser than 2 core's.

Basically, I like to know whether a single-core system will suffice for this work load. Thank you!

Upvotes: 7

Views: 2506

Answers (2)

Xaqron
Xaqron

Reputation: 30867

The fact that multiple node instances are running (while you ran the script once) means this is neither an internal node mechanism nor using worker threads by script to utilize CPU.

Either the script or one of the libraries used by it, is trying to utilize CPU via the old fashion forking (child process) way which results in isolated (no shared memory) instances of node (multiple processes as in your case) which communicate via IPC.

Upvotes: 3

Bergi
Bergi

Reputation: 664970

Even though only a single JS function (per environment)1 will execute at any given time, on one core, that doesn't mean

1: you mention that you're running only a single script, which means there is only one realm of JS objects. Another possibility for a node.js app to run on multiple threads is to spawn workers, each of which has its own JS environment and communicates via events.

Upvotes: 7

Related Questions