Home >Web Front-end >JS Tutorial >Master-Worker Pattern in NodeJS: Explained
Node.js, a JavaScript-based development platform, empowers developers to build server-side applications using JavaScript. Its single-threaded, event-driven architecture is a key strength, efficiently managing numerous concurrent requests without the overhead of multiple threads or processes.
Despite its advantages, Node.js's single-threaded nature presents limitations:
The solution? Node.js's Master-Worker Pattern (also known as Cluster Mode). This distributed system design uses a master process overseeing multiple worker processes. The master manages and monitors the workers, while workers handle individual tasks.
Node.js leverages the cluster
module for Master-Worker implementation. This module simplifies the creation of multiple child processes and provides mechanisms for control and inter-process communication. Here's a breakdown:
cluster
module and identify whether the current process is the master or a worker.cluster.fork()
to spawn worker processes and attach event listeners to monitor their status and messages.process.send()
.This basic example demonstrates the cluster
module:
<code class="language-javascript">const cluster = require('cluster'); const http = require('http'); const numCPUs = require('os').cpus().length; if (cluster.isMaster) { console.log(`Master ${process.pid} is running`); // Spawn workers. for (let i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', (worker, code, signal) => { console.log(`Worker ${worker.process.pid} died`); }); } else { // Workers share a TCP connection (e.g., HTTP server). http .createServer((req, res) => { res.writeHead(200); res.end('Hello from Worker!'); }) .listen(8000); console.log(`Worker ${process.pid} started`); }</code>
The master process determines if it's the master (using cluster.isMaster
). If so, it creates workers equal to the CPU core count. Each worker is an independent process with its own memory and V8 instance. Workers establish an HTTP server and listen for requests. The exit
event handles worker crashes, enabling the master to restart them.
Enhance scalability by using Nginx as a reverse proxy and load balancer to distribute requests across multiple Node.js processes.
Nginx Configuration Example:
<code class="language-nginx">http { upstream node_cluster { server 127.0.0.1:8000; server 127.0.0.1:8001; server 127.0.0.1:8002; # ... more Node.js processes } server { listen 80; location / { proxy_pass http://node_cluster; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } }</code>
Nginx evenly distributes requests among the Node.js processes listed in node_cluster
.
In summary, Node.js's Master-Worker Pattern, implemented using the cluster
module, offers a straightforward yet powerful method for building multi-process applications, improving performance and reliability.
Leapcell is a next-generation serverless platform for web hosting, asynchronous tasks, and Redis:
Multi-Language Compatibility
Unlimited Free Project Deployment
Exceptional Cost-Effectiveness
Streamlined Developer Workflow
Effortless Scalability and High Performance
Learn more in the Documentation!
Follow us on X: @LeapcellHQ
Read more on our blog
The above is the detailed content of Master-Worker Pattern in NodeJS: Explained. For more information, please follow other related articles on the PHP Chinese website!