Multiprocessing in NodeJS

Clayton
3 min readMay 9, 2023

--

Photo by Gabriel Heinzer on Unsplash

As you already know, nodejs by default runs application in single process mode. Although the ability of nodejs in high concurrency processing is strong and impressive, you can still try to implement it in multiprocessing way, since multiprocessing can leverage the multi-cpu platform ability to the fullest.

Disadvantages of single process

Our previous development of node was only single-threaded. Although single-threaded is simple, it has many disadvantages:

  • Node is a single process. Once an error occurs in the code and the process is closed, the entire service will collapse.
  • Because it is a single process, the ability to process requests is limited, and only one process can be processed before the next one can be processed, which cannot take advantage of multi-core CPUs.
  • A single process cannot achieve hot updates. When the file is modified, if you want to apply the modification, you must restart the entire service to do so. In the process of restarting, it is unable to accept any requests

Well, having said these shortcomings, how to solve it, the answer is: multi-process!

Advantages of multiprocessing

  • Once an error occurs in the code, at most it will affect the shutdown of one process, and other processes will still run normally without causing the entire service to go down
  • One process processes one request at the same time, and if there are multiple processes, multiple requests can be processed at the same time. Increase the concurrency of services
  • It is also very simple to implement hot updates. When node detects that the file is updated, it will stop in turn and start a new process. For new processes, new files are read. The whole process is insensitive to the client, and the only perception is the change of the page content.

Simple http Server with single process

//index.js
const http = require("http");

http.createServer((req, res) => {
res.end("hello world");
}).listen(8089, () => {
console.log("listen 8089");
});

Created a simple http service, local access http://localhost:8089, the page will be displayedhello world

Nodejs cluster

cluster is a core module of Node.js that provides a simple way to create multi-process applications. When using the cluster module, a Node.js application can be divided into multiple processes, each running independently but sharing the server port, thus improving the application's concurrency and reliability.

The main methods of the cluster module include:

  • cluster.fork(): creates a new process, copying all settings and objects of the current process, and starts a new Node.js instance to run the same code.
  • cluster.isMaster: a Boolean value that determines whether the current process is the master process. If it is the master process, it can create child processes using the fork() method.
  • cluster.isWorker: a Boolean value that determines whether the current process is a worker process. If it is a worker process, it can execute specific business code.

Using the cluster module can implement load balancing and fault tolerance processing for multi-process applications. For example, client requests can be distributed to multiple worker processes for processing, thus improving the application's concurrency and reliability. At the same time, the cluster module can also be used to implement hot reloading and automatic restart of the application.

Multi-process Http Server in action

Next, we start to add multi-process function to the simple server with the help of cluster.

//index.js

const cluster = require("cluster");
const http = require("http");
const numCPUs = require("os").cpus().length;

if (cluster.isMaster) {
console.log("master: ");
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
http.createServer((req, res) => {
res.end("hello world");
}).listen(8088, () => {
console.log("listen 8088");
});
}

The multi-process function mainly relies on cluster.fork() incluster to create child processes. And the number of child processes created is determined by the number of CPU cores.
There is another condition in the code cluster.isMaster, if it is the main process, follow the logic of the main process; if it is a sub-process, follow the logic of the sub-process.
The logic of the main process is mainly to create the logic of the sub-process The sub-process is mainly to create an HTTP server

Summary

As you can see, multiprocessing implemention in nodejs is quite simple and easy to comprehend. In case of high performance situations, you may just try it with the help of cluster.

--

--