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 ?
Related
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
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.
I have 2 node.js programs that communicate with each other through a socket. I originally intended on running them on separate linux machines, but realized it would be much simpler to have them both on one multi-core machine, each allocated a specific subset of the total CPUs. For example, I have a machine with 8 cores, I want to run program_A on 2 cores as 1 process (using node's fork module), and program_B on the remaining 6 cores, also as a single process forked over several cores. Is there any way to do this with node.js? I'm aware of taskset to set CPU affinity, but I'm not quite sure if that would work for this scenario.
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.
Can Couchdb saturate all cores on a multi-core machine for read/write operations like MongoDB ( in MOngoDB all core used only for read operations ) or not ?
Yes. Like any other erlang application couchdb utilizes all the cores available in your machine.
Here is a screen shot for my couch beam process. As you can see it is happily running on both the cores (I have a two core machine)
Couchdb however spwans many process and not all of them are multicore. For example a couchjs process that is spawned for building views is single core. However there is a couchjs process for every design_doc which essentially means that you can build multiple views together and they will be distributed across the cores.
To answer your question both read and write are multicore operations but view building is not.