NodeJS & Multi-core CPU
What’s multi-core?
You have a highway. Highways have speed limit and highways have lanes.
On Highway 75, you have a speed limit of 80, but it’s an 8 lane highway.
On Highway 15, you have a speed limit of 140, but it has only 2 lanes.
If you have only one fast car, you’re better off on Highway 75.
If you have a bunch of trucks, you are better off on Highway 90, because you can have more trucks going at the same time.
Most games, GUI bases programs want a “higher speed limit” — with only 2 cars on the highway at the same time.
A lot of data pipeline, productivity software prefer the opposite: having 8+ trucks on the road at the same time
It’s about picking the right tool for the job. Sometimes more & slower is better; sometimes fewer and faster is better.
NodeJS on multi-core
Node.js absolutely does scale on multi-core machines.
- For big heavy compute tasks like data processing, Node.js can fire up child processes or send messages to additional worker processes. In this design, you’d have one thread managing the flow of events and N processes doing heavy compute tasks and chewing up the other 15 CPUs
- For Node.js, having N workers compete on a single socket is an extremely reasonable solution.
Shared Ports: nginx or loadbalancer (port 80) --> Node_workers x N (sharing port 3000 w/ Cluster)
Measure separate core usage per process
mpstat reports the per-core CPU usage for all processes in the machine:
dstat allows us to input the CPU core id for which we need the CPU usage:
In linux, we can find core id from cpuinfo file