scaling-nodejs

📈 Scaling Node.js on each X, Y and Z axis using Node.js Native Modules, PM2, AWS , Load Balancers, AutoScaling, Nginx, AWS Cloudfront

View the Project on GitHub realabbas/scaling-nodejs

Scaling Node.js Guide Github Ali Abbas @realabbas

Visitors

Scaling Types

Node.js Native Modules

Resources and techniques

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

Blogs

Videos

Example Code

Cluster

Check number of CPUs
const cluster = require("cluster"); // Cluster Native Module
const cpus = require("os").cpus().length; // Number of CPUs

console.log("Number of CPUs available - ", cpus)

Full Clustering Example
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);
}
Zero - Downtime Example
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);
}