Giter VIP home page Giter VIP logo

scaling-nodejs's Introduction

Scaling Node.js Guide Github Ali Abbas @realabbas

Visitors

Scaling Types

  • X Axis
  • Y Axis
  • Z Axis

Node.js Native Modules

  • cluster
  • os
  • spawn

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)

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);
}

scaling-nodejs's People

Contributors

realabbas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.