Node Clustering

How to run node in a cluster

Node Clustering
import cluster from "cluster";
import os from "os";

const osCPUs = os.cpus().length;
const totalCPUs = IS_PRODUCTION ? osCPUs : 1;

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

  cluster.on("exit", () => {
    // Fork new worker if one dies
    cluster.fork();
  });

  if (!IS_PRODUCTION && CRONS_ENABLED) {
    // Start the syncs locally using cron jobs. In production we will use the
    // cron.yaml so that we can load balance the requests and ensure we only
    // run the sync once instead of on all vm nodes
    setTimeout(() => startSyncs(), 2000);
  }
} else {
  startServer();
}