📈 Scaling Node.js on each X, Y and Z axis using Node.js Native Modules, PM2, AWS , Load Balancers, AutoScaling, Nginx, AWS Cloudfront
cluster
os
spawn
Some techniques are implemented using stack other than node.js, but the concept is more important to comprehend and implement for better performance of the application
const cluster = require("cluster"); // Cluster Native Module
const cpus = require("os").cpus().length; // Number of CPUs
console.log("Number of CPUs available - ", cpus)
const http = require("http");
const cluster = require("cluster"); // Cluster Native Module
const cpus = require("os").cpus().length; // Number of CPUs
const port = process.env.PORT || 3000;
if (cluster.isMaster) {
// Checking whether the current cluser is master or not.Boolean value is returned
console.log("Master CPU - ", process.pid);
for (var i = 0; i < cpus; i++) {
cluster.fork(); // Forking a new process from the cluster
}
cluster.on("exit", (worker) => {
console.log("Worker Process has died - " + process.pid); // Process that exited/died.
console.log("Process Remaining - " + Object.keys(cluster.workers).length); // Prints the number of running workers at any instance
console.log("Starting New Working");
console.log("Process Remaining - " + Object.keys(cluster.workers).length);
});
} else {
http
.createServer((req, res) => {
message = `Running Process: ${process.pid}`;
res.end(message);
// For Testing Purpose. Uncomment the following code to kill process by sending GET request to '/kill'
// if (req.url === "/kill") {
// process.exit();
// } else {
// console.log(process.pid);
// }
})
.listen(port);
}
const http = require("http");
const cluster = require("cluster"); // Cluster Native Module
const cpus = require("os").cpus().length; // Number of CPUs
const port = process.env.PORT || 3000;
if (cluster.isMaster) {
// Checking whether the current cluser is master or not.Boolean value is returned
console.log("Master CPU - ", process.pid);
for (var i = 0; i < cpus; i++) {
cluster.fork(); // Forking a new process from the cluster
}
cluster.on("exit", (worker) => {
console.log("Worker Process has died - " + process.pid); // Process that exited/died.
console.log("Process Remaining - " + Object.keys(cluster.workers).length); // Prints the number of running workers at any instance
console.log("Starting New Working");
cluster.fork(); // Creating a new process again after the previous process exited so that max number of cpus are utilised.
console.log("Process Remaining - " + Object.keys(cluster.workers).length);
});
} else {
http
.createServer((req, res) => {
message = `Running Process: ${process.pid}`;
res.end(message);
// For Testing Purpose. Uncomment the following code to kill process by sending GET request to '/kill'
// if (req.url === "/kill") {
// process.exit();
// } else {
// console.log(process.pid);
// }
})
.listen(port);
}