introduce

An instance of a single Node.js program runs only on a single thread and therefore cannot take full advantage of a CPU’s multi-core system. Sometimes you may need to start a cluster of Node.js processes to take advantage of each CPU core on your local computer or production server.

This is especially important when dealing with APIS or EXPRESsJs-based HTTP servers.

Fortunately, Node.js has a core module called Cluster, which helps us run Node.js programs on all the cores of the CPU.

In this article, we will implement an ExpressJS HTTP server in Node.js and create a unique instance on each CPU kernel. As a result, the throughput of the HTTP server is greatly increased as each of its CPU core instances provides the possible number of concurrent requests.

Directory creation Express HTTP server runs the server on multiple CPU coresCopy the code

Create an Express HTTP server

The first thing we need to do is get the HTTP server up and running. If you already have an ExpressJS server available, you can skip to the next part: running the server on a multi-core CPU.

We’ll use ExpressJS to quickly create an efficient and simple server. If the NPM package is not already installed, you can install it using the following command:

$ npm install --save express
Copy the code

Then add the following code to your Node.js file:

const express = require("express")
const PORT = process.env.PORT || 5000
const app = express()
app.listen(PORT, function () {
  console.log(`Express server listening on port ${PORT}`)
})
Copy the code

First, we require() the previously installed Express NPM package.

Then, we create a PORT variable, which can be the current value of process.env.port or 5000. You then create an express instance with the Express () method and store it in the APP variable.

Add app.listen() to start the Express program and tell it to listen for the PORT we specify.

When you run the code from the command line, you should see something similar output to the console:

Output:
Express server listening on port 5000
Copy the code

Very good! Now let’s start the Express HTTP server.

Run the server on multiple CPU cores

In this section, we’ll run the Express server on multiple CPU cores!

To help us achieve this goal, we will use the Node.js modules OS and Cluster. The OS module is used to detect how many CPU cores the system has, and the Cluster module is used to create multiple sub-processes that our HTTP server can run in parallel.

Since these are core modules, there is no need to install any NPM packages, and we can require() them into our code.

I’ll provide you with the full code and explain it later, so if you’re confused, don’t worry.

Here’s the complete code:

const express = require("express") const os = require("os") const cluster = require("cluster") const PORT = process.env.PORT || 5000 const clusterWorkerSize = os.cpus().length if (clusterWorkerSize > 1) { if (cluster.isMaster) {  for (let i=0; i < clusterWorkerSize; i++) { cluster.fork() } cluster.on("exit", function(worker) { console.log("Worker", worker.id, " has exitted.") }) } else { const app = express() app.listen(PORT, function () { console.log(`Express server listening on port ${PORT} and worker ${process.pid}`) }) } } else { const app = express() app.listen(PORT, function () { console.log(`Express server listening on port ${PORT} with the single worker ${process.pid}`) }) }Copy the code

There’s a lot going on in the code, so let’s explain every part of it.

The first is the Require () Express package and the two core modules of Node.js, OS and Cluster.

Next, create a PORT variable and assign it the current process.env.port number or a value of 5000. We will use it later at startup.

We then create a variable named clusterWorkerSize to represent the number of cpus on the system. This number can be obtained using the os.cpus().length method. More information about the os.cpus() method can be found in the Node.js documentation.

We create an if… Else statement to check if the CPU has multiple cores with the clusterWorkerSize value. If the number of cpus is greater than 1, we continue to create the cluster. But if there is only one CPU core on the computer where the code is running, start the Express program the way you did in the first step of this tutorial.

Assuming our machine has multiple CPU cores, create another if… Else statement to check whether the statement is the first process that has been run in the cluster. Use the cluster.ismaster () method to check whether true or false is returned.

If it is the first process to run, we will use cluster.fork() to generate a new worker process for each CPU core on the computer. We also added an event listener that will output a message when the worker process exits so that we know when there is a problem or surprise.

It is worth noting that the main process is used to listen for ports on the HTTP server and balance the load of all requests between worker processes.

Once all worker processes have been generated, we will create a new instance of the program on each worker process we create. If your computer has two CPU cores, two instances of this program will be created.

When you run the program, you should see the following on the console:

The Output:  Express server listening on port 5000 and worker 10801 Express server listening on port 5000 and worker 10802 Express server listening on port 5000 and worker 10803 Express server listening on port 5000 and worker 10804Copy the code

The output will vary depending on the number of CPU cores.

Now you have an HTTP server that can run on a multi-core CPU!

conclusion

The Cluster module makes it easy to create child processes, giving Node.js much needed functionality to use all of the functionality provided by the CPU. It also does a lot of work behind the scenes to communicate between the main and worker processes.

After reading this article, you should now know how to use this module to run the Express HTTP server on multiple CPU cores. Armed with this knowledge, you’ll be able to better manage and extend your application.