According to this page Go vs Node.js, Node.js is not showing to be taking full advantage of CPU cores when running cpu-intensive code.
If I use virtualization and simply add more Node.js instances, will I achieve the same performance as Go? I suppose there still will be overheads and one won't be able to achieve the same performance.
Multiple processes will do. For 4 cpus/threads you need 4 Node.js processes to make use of them. That requires a workload that can be split between processes though.
Node.js provides the Cluster module to distribute socket connections between multiple worker processes which may help in some workloads, but I doubt this would help any of the benchmark workloads.
You need to install Cluster module of nodejs in order to take full advantage of CPU cores when running cpu-intensive code.
Node.js is indeed a single threaded process. However, you can make use of clustering to spawn multiple workers on multiple cores.
If you are not using PM2 for spawning the app, please consider using it. PM2 makes spawning workers on multiple cores super easy.
pm2 start app.js -i max
This command will spawn workers on each available core.
Also, note that if you are using session/sockets then you might face some problems because of clustering.
Related
I am doing some research for a home project and I'm looking into the Cluster Module and Worker Threads.
I know the difference between Cluster and Worker Threads.
My question is:
In NodeJS is it possible to use Clustering and Worker Threads at the same time?
I'm guessing you're thinking of the worker threads module when you're referring to "worker threads". I'm making a clear distinction since NodeJS runtime comes with 4 worker threads by default. cluster (module) and worker_threads shouldn't have any problems working in parallel as clustering provides you with multiple independent NodeJS instances, as in, multiple NodeJS processes which have their own threads as stated before. Spawning more worker threads using the above mentioned worker_threads module spawns more threads which aren't independent (they can and do share memory), which can be good if you're doing some crunching, but they all run under a single Node process.
Processes can communicate using IPC, and Node worker threads can talk using the MessagePort class from the same module.
Therefore, yes, you can do that and my best guess is that, on the top of the "supervision tree" of sorts you spawn a couple of Node processes (using the cluster module) to distribute the load if you have them acting as servers (no clue about your use case), and then for each process you can use the worker_threads module to spawn additional threads if needed (to speed up some heavy processing etc).
Using pm2 with node.js app, 2 core machine.
pm2 start app.js -i 4
Will it help the performance, or won't help at all ?
Thanks !
It won't help performance, in fact it will be harmful. It's recommended to spawn N-1 workers, N being the amount of CPU cores.
You can issue: pm2 start app.js -i -1 for that. Given that you only have 2 cores, this will only use one, so you won't take advantage of clustering.
If you want to try using 2 cores in your case, you should run your own benchmarks, but make sure that your machine is not doing much work outside of Node.js, otherwise it will be better to just use 1 core.
If you use more workers than CPU cores, the processes will start competing for resources, which will downgrade the performance of the application.
"Node.js is limited to a single thread". how the nodeJS will react when we are deploying in Multi-Core systems? will it boost the performance?
The JavaScript running in the Node.js V8 engine is single-threaded, but the underlying libuv multi-platform support library is multi-threaded and those threads will be distributed across the CPU cores by the operating system according to it's scheduling algorithm, so with your JavaScript application running asynchronously (and single-threaded) at the top level, you still benefit from multi-core under the covers.
As others have mentioned, the Node.js Cluster module is an excellent way to exploit multi-core for concurrency at the application (JavaScript V8) level, and since Express is cluster aware, you can have multiple worker processes executing concurrent server logic, without needing a unique listening port for each process. Impressive.
As others have mentioned, you will need Redis or equivalent to share data among the cluster worker processes. You will also want a logging facility that is cluster aware, so the cluster master and all worker processes can log to a single shared log file. The Node log4node module is a good choice here, and it works with logrotate.
Typical web examples show using the runtime detected number of cores as the number of cluster worker processes to fork, but I prefer to make that a configuration option in a config.yaml file so I can tune the number of worker processes running the main JavaScript application as needed.
Nodejs runs in one thread, but you can start multiple nodejs processes.
If you are, for example, building web server you can route every request to one of nodejs processes.
Edit: As hereandnow78 and vkurchatkin suggested, maybe the best way to use power of multi core system would be to use nodejs cluster module
cluster module is the solution.
But u need to know that, node.js cluster is, it invokes child process. It means each process cannot share the data.
To share data, u need to use Redis or other IMDG to share the data across the cluster nodes.
I have an application that runs long-executing processes. To make it faster, I do simple sharding of data and want to run them in parallel, simply by .fork() 2 instances of same application.
I'm having 2 Cores machine there and want to make sure that 2 Cores are utilized and first instance is running on first core, second on second core.
I know about cluster module, but it seems not relevant in this case, since I don't need HTTP services running and load-balancing between them. Just workers (mean, they dont need to communicate with each other, send messages or whatever - they just do HTTP requests and store data to database).
Is there possible at all to control which CPU core would node.js process take? How to monitor that on Mac/Linux?
Cluster module is exactly what you need : http://nodejs.org/api/cluster.html
You want this, which is a specific part of the cluster API:
cluster.setupMaster([settings])
https://nodejs.org/api/cluster.html#cluster_cluster_setupmaster_settings
I also wrote a module to do this, and there are several out there:
https://www.npmjs.com/package/poolio
my module works well, but the API is not that straightforward, so I would recommend using the core module
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
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.
Node is not multi-threaded from the point of developer's view. Threads are used in a very different way than they are used by for example Apache's worker mpm.
I believe this answer will clear things up.