- [Instructor] Node.js runs in a single process,
and basically in one thread.
This means that it does not fully
utilize all CPUs on a host.
To remedy this, Node.js provides a cluster module,
that will spin up chart processes.
Under the hood, Node.js then
uses interprocess communication to distribute the load
between the chart processes, round robin.
Node cluster is not the only way to achieve this.
For example, if you use the process manager PM2,
as we will do later, it can be
configured to scale up to all CPUs as well.
Still, to understand the basic principle behind clustering,
we will now implement this from scratch using Node cluster.
Before we optimize this, let's run a little load test
against our current application.
So, on the console, I'm using a patch bench,
and we want to concur and see if 10,
and I'm running one hundred request against the website.
This takes a little bit, and we
see that we have a time per request
of around 39 milliseconds.
Now, let's see what we can gain by using the cluster module.
I'm in bin www, cluster is core module of Node.js,
so, I don't have to insert anything in here.
I'm just requiring cluster, and it also
wants to require the os which is also core module that
gives us access to some operating system information.
From this os module, I can now query the number of CPUs.
Const numCPUs equals os dot cpus parenthesis, dot length.
You will see in the setting, while we are doing that,
I'm scrolling down all the way to line 46,
and after this line, so, after the instance
of the server was created,
I now add the logic for the clustering.
And here, at first, say if cluster is master,
so, that's the case when we are the master process.
That's the process that starts first.
I will do a log of info,
master, dollar,
process, dot, pid, is running.
So, we will get the process id of the master process,
and then I will use a for loop
to create workers for each free CPU.
So, I lower than num CPUs,
because one CPUs should be dedicated
to the master process as well.
One, so for each CPU we will do a cluster
with fork which creates a worker.
We also want to handle errors here,
so, cluster on exit, so if something goes wrong,
this event gets the, just that worker process, and there,
I copy over that log line from line 49.
But this time it's a log fatal,
and it's worker process,
worker process pid, just died.
And in this case, I want to fork a new worker
because otherwise, we would maybe run out of workers,
if there was some temporary problem,
which lets the workers die.
Next, we will have to add the code to run when we
are the chart process, and in this chart process,
we will now do this db connect, and the listening part.
So, that's the main application code.
Everything else can stay the same.
And let's now restart the application,
and see what happens, and we
see the log message that indicates,
that all workers have spun up.
Now, let's run a load test again.
So, before we had around 39 milliseconds response time.
Now, let's do that again, and we
see that it's now 24 millisecond,
which is a pretty good improvement.