Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If nodejs is multithreaded why should i use cluster module to utilize multicore cpu?

if nodejs is multithreaded see this article and
threads are managed by OS which can do it in the same core or in another core in multicore cpu see this question then nodejs will automatically utilize multicore cpu ,
so why should i use cluster.fork to make different process of node to utilize multicore as shown in this example at node docs


i know that multiprocess have the advantage that when one process fall there still another process to respond to requests unlike in threads , i need to know if multicore can be utilized by just spawning process for each core or it's an OS task that i can't control

like image 708
Basemm Avatar asked Dec 06 '22 12:12

Basemm


1 Answers

It depends.

Work that happens asynchronously and by Node itself, such as IO operations, is multithreaded. Your JavaScript application runs in a single thread.

In my opinion, the only time you need to fire off multiple processes, is if the vast majority of your work is done in straight JavaScript. Node was designed behind the fact that this is rarely the case, and is built for applications that primarily block on disk and network.

So, if you have a typical Node application where your JavaScript isn't the bulk of the work, then firing off multiple processes will not help you utilize multiple CPUs/cores.

However, if you have a special application where you do lots of work in your main loop, then multiple processes may be for you.

The easiest way to know is to monitor CPU utilization while your application runs. You will have to decide on a per-application basis what is best.

like image 181
Brad Avatar answered Dec 11 '22 12:12

Brad