using nodejs cluster in production? - node.js

i was reading nodejs cluster to run multiple instances of node appication
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
}
else {
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
console.log(`Worker ${process.pid} started`);
}
is using this approach good in a production environment.
where the number of requests is more

You can rather use pm2/strongloop for managing this.Because all these modules are production ready and really easy to manage.
I personally feel pm2 is awesome to manage node processes.
Use following link to know more about pm2
http://pm2.keymetrics.io/

Yes you can use cluster for as long as you are calculating your cpu length
const numCPUs = require('os').cpus().length;
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
The cluster won't create processes more than that of the cpu length
But if clusters are not manage properly it will slow down the entire application process

Related

How can a run a middleware function a single time when I am using Cluster in Node.js

I am using clusters in my node.js server and I have a middlewere function that is saving weekly some data to db. The issue is that I am using three workers and this function is executed three times and it saves 3 documents in the DB. How can I avoid this and still use clusters?
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// execute the one time function
// Fork 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 can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
console.log(`Worker ${process.pid} started`);
}
you can do that by executing it inside the if statement or where your master code executes. this way you can just run it once.
https://nodejs.org/api/cluster.html
Your nodejs program can tell whether it's the master with cluster.isMaster().
Your master can update your weekly data. Or, if all your workers have to generate their own data, they can do that, then the master can read it all and create just one document.

Can node run in multiple threads?

Nodejs is asynchronous. But single threaded. When a synchronous workload is executed, the event loop is blocked.
Can we make node multi threaded to increase performance?
You can look into cluster mode in recent versions of Node.js.
Quoting the example from the above page for reference:
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork 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 can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
console.log(`Worker ${process.pid} started`);
}
This code starts a number of workers, and you see a clear separation between master and worker code.
These processes can communicate by sending messages.

Nodejs cluster forking not working on Windows; how do you enable the round-robin scheduling?

Similar questions have been asked, as to why a nodejs cluster forking only delegates to one worker on a windows machine. We know the answer is that the RR algorithm is disabled on windows, but so then, how can you enable it?
I have a hint:
1) https://stackoverflow.com/a/43971478/8804567
The Docs say "
cluster.schedulingPolicy can also be set through the NODE_CLUSTER_SCHED_POLICY environment variable. Valid values are 'rr' and 'none' "
I'm wondering where exactly do I make this configuration.
The code:
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
// Workers sharing same TCP connection
http.createServer((req, res) => {
console.log(`worker ${process.pid} working`);
res.writeHead(200);
res.end('hello world\n');
}).listen(2019);
console.log(`Worker ${process.pid} started`);
}
Any help, guidance or comments in relevancy are very much appreciated, seriously!
Thanks community!
Ok, so it was right in my face, but I was confused as to how to do it.
I added this line of code & now when I run it & load test it I can see a proper delegation.
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
cluster.schedulingPolicy = cluster.SCHED_RR;
...

Nodejs cluster: why blocking master will block workers too?

As you can see, in master(cluster.isMaster) while loop is blocking master, but why worker is also blocked?
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
const sleep = require('sleep');
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
// background jobs start
while(true) {
console.log(123)
sleep.sleep(1) // do_background_jobs()
}
// background jobs end
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
console.log(`Worker ${process.pid} started`);
}
Here is logs:
Master 45476 is running
Worker 45479 started
Worker 45483 started
Worker 45482 started
Worker 45478 started
But netstat shows 8000 is closed. of cource curl fails.

Node's Cluster Fork

I don't understand how clusters work in Node.
The snippet below is example code from Node's docs.
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork 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 can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
console.log(`Worker ${process.pid} started`);
}
I'd like to know exactly what happens at cluster.fork(). Does it copy over any variables to the forked processes? If I declared an object before forking, would both threads access it? Is it possible to assign tasks to a thread manually or does Node have to do it?

Resources