0

My question is, now that I need a load balancer something else, or this Worker that I create according to the CPUs Core size works perfectly well in the node app.

const express = require("express");

const cluster = require("cluster");
const totalCPUs = require("os").cpus().length;

const PORT = process.env.PORT || 3000;

if (cluster.isMaster) {
  console.log(`Number of CPUs is ${totalCPUs}`);

  // Fork workers.
  for (let i = 0; i < totalCPUs; i++) {
    cluster.fork();
  }

  cluster.on("exit", (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
    console.log("Let's fork another worker!");
    cluster.fork();
  });
} else {
  const app = express();

  app.use(express.json());
  app.use(express.urlencoded({ extended: false }));

  // Route Calling
  const vehicleRoute = require("./routes/vehicle");

  app.use("/api", vehicleRoute);

  app.listen(PORT, () => {
    console.log(`[${process.pid}]App listening on port ${PORT}`);
  });
}
Smit Gajera
  • 1,001
  • 1
  • 8
  • 26
  • 1
    I'm not seeing a question here. What is the problem you're having that you're solving with "a load balancer something or whatever, or this Worker that I create"? – AKX Oct 27 '21 at 04:23
  • [@AKX] (https://stackoverflow.com/users/51685/akx), Right now I'm scaling my node application this worker installation works fine, or do I need to define or set-up more load balancer like Nginx or else? – Smit Gajera Oct 27 '21 at 04:38
  • I'm a little confused here node clustering is perfect to scale our application or need any other tool? – Smit Gajera Oct 27 '21 at 04:39
  • No one else but you can answer that – you will need to measure it with your expected load, and then see if it holds up. – AKX Oct 27 '21 at 05:09

0 Answers0