Node.js Cluster module - Multiprocessing or Parallel Processing? - node.js

I built a Node.js application that is built up of smaller components (children worker processes) that all perform different tasks, “in parallel”. When the main program runs, it spawns the workers and they begin their work. I used Node.js’s cluster module to accomplish this.
Is this an example of multiprocessing or parallel processing, and why?

Clustering is better to say a load balancer rather than parallel processing.
Node JS is single threaded, it means if you have 4 cores, it will use one regardless of available cores, that's fork mode, Clustering uses one node thread on all available cores, that's an optimized way of doing things and load balancing.

Related

Differences between Node.js Thread Pool and Clustering Node.js

Recently I've been researching about Node.js core architecture, but still I don't understand some concepts.
So assume that I have 6 core CPU and it's an Intel CPU, so actually I have 12 logical processors for threads for any execution in my computer that needs CPU, on the other hand Node.js and all of it's core components such as libuv and V8 engine are all running in a single thread.
Now for I/O tasks that consumes resources like disk for example, or other inner C/C++ node.js modules, the libuv have thread pool with maximum size of 4, This means that libuv can request OS for do 4 tasks in parallel (exactly at same time). Now in this situation OS do the work of context switching for node.js threads because they have higher priority and complete its tasks.
This is what I learned about Node.js event loop.
Now I want to know how clustering node.js app (forking and run multiple processers) is going to improve the entire application performance.
If I run 12 clusters from my node app (cause I have 12 logical CPU processors) and each node.js process have 4 running I/O async jobs, that means that we have 48 threads but only 12 of them can run at the time and the other tasks must wait.
So is it not better to just increase libuv thread pool size to 11 (and 1 for node.js main thread) and just have one node.js process ? instead of clustering them ?
What I'm missing here ? can someone explain how clustering can increase performance ?
and what about clustering and increase libuv thread pool size together ?

Where is master process located in cluster architecture of nodejs app?

Let's say, I've got a 4-core CPU, which basically means, my computer could run 4 processes at the same time; no more no less.
Now let's look at cluster module of nodejs; there are
1 master process
4 worker processes
Can I say, each worker process is to be assigned to each core of the CPU?
And if it is, then where is its master process located?
The master process is fired as a standard single-thread process (or service) so it "is assigned to" and lives on one of your CPU cores (or threads, depending on the case).
Each worker process is then spawned (forked) as described in the docs:
https://nodejs.org/api/cluster.html
Just so you know, Node now supports the use of worker_threads to execute JavaScript in parallel on multiple threads, so it's technically not true anymore that Node is limited by single-thread execution:
https://nodejs.org/api/worker_threads.html

Can we use Node.js cluster using single core processor?

I have a node.js application which receives multiple requests concurrently from clients. If this application is running on a server with a multi-core processor, we can reap the advantages of Node.js cluster to scale-up the application by creating multiple workers which can actually be executed in parallel on a muti-core processor to manage the load.
With Node.js being single-threaded and if my application is running on a server having a single CPU, can we use cluster to scale-up the application?
Intel cores usually have hyper-threading, allowing two threads per core at (slightly?) slower performance. Even with a single core, you should be able to reap those benefits.

standalone node scripts needs clustering?

I have node standalone workers for data processing.
Depend upon my server capability, I will be running multiple instances of worker
so my question is, do I need to include clustering in my worker,
because clustering main task is to make use of multiple cores of cpu but it holds true only if we are serving http requests
But in my case if I am running 4 instances and then each instance will follow clustering, so I think its not recommended to use clustering for standalone scripts
The built-in cluster module would be useful in this case, but certainly not necessary. If you have some queue that each instance can pull from without conflict, then you're probably already running as well as you can.
If you want to manage/broker the work for the child workers, then you can use the cluster module to start sub processes and hand out the tasks from your master process.

How to make sure that nodejs cluster assigns processes to different cores?

When utilizing multi cores via Node.js' cluster module is it guaranteed that each forked node worker is assigned to a different core?
If it's not guaranteed is there any way to control or manage it and eventually guarantee that all end up in different cores? Or the OS' scheduler distributes them evenly?
A while ago I did some tests with cluster module which you can check in this post that I wrote. Looking at the system monitor screenshots it is this pretty straightforward to understand what happens under the hood (with and without cluster module).
It is indeed up to the OS to distribute processes over the cores. You could obtain the pid of a child process and use an external utility to set the CPU affinity of that process. That would, of course, not be very portable.

Resources