Harnessing Multi-Threading in Node.js: A Guide to Clustering and Parallel Processing
As we all know the javascript is singly threaded programming language , and it does not provide functions of multithreaded languages like java or .NET .
Quoted from Building API with Node.js "A single-thread architecture works with only the main thread instance for each initiated process. If you are used to programming in multithread architectures, like Java or .NET to use the concurrency processing, unfortunately that won’t be possible with Node.js. However, you can work with parallel processing using multiple processes"
There are 2 option available to us
Using async and await method . this is one of the main resource of Node.js . this works as nonblocking I/O .
clustering :-
In Node.js you can create N-1 processes to work with parallel processing, as the main process (or master process) works on balancing the load among the other processes (or slave process). If your server has multiple cores, applying this technique will optimize the usage of the processing
Let me explaing by giving example :-
const cluster = require('cluster'); const http = require('http'); const numCPUs = require('os').cpus().length; if (cluster.isMaster) { // Fork workers equal to the number of CPU cores for (let i = 0; i < numCPUs; i++) { cluster.fork(); } // Listen for worker exit event and fork a new worker if one exits cluster.on('exit', (worker, code, signal) => { console.log(`Worker ${worker.process.pid} died`); cluster.fork(); }); } else { // Worker processes will be here const express = require('express'); const app = express(); // Define a route app.get('/', (req, res) => { res.send('Hello from Worker ' + cluster.worker.id); }); // Start the server const PORT = process.env.PORT || 3005; app.listen(PORT, () => { console.log(`Worker ${cluster.worker.id} running on port ${PORT}`); }); }
There clustering is done and master worker is busy or exited then another fork is done and process is carried out making using of clustering
Clustering also provides load balancing.
This works efficiently only if your VM has a good number of cores.
In this example:
We import the
cluster
,http
, andos
modules from Node.js.We check if the current process is the master process using
cluster.isMaster
. If it is the master process, we fork multiple worker processes equal to the number of CPU cores available on the system usingcluster.fork()
.If we are using imports,
cluster.isMaster
has been deprecated.- We need to use
cluster.isPrimary
.
- We need to use
Each worker process handles incoming HTTP requests using a simple HTTP server. Each worker listens on port
3000
.If a worker process dies for any reason (
'exit'
event), the master process forks a new worker process to replace it.- This is especially useful when a worker process encounters an error in a large backend, affecting the speed of API calls. This easily creates a new worker process.
Each worker responds to HTTP requests with a simple message indicating its worker ID.
When you run this code, you'll have multiple Node.js processes running simultaneously, each capable of handling HTTP requests independently. The master process is responsible for load balancing among the worker processes, distributing incoming requests among them for efficient parallel processing.