Node Clustering
How to run node in a cluster
![Node Clustering](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fsds0zpye%2Fproduction%2Fbabb361a6cabc6f0e6c340c6538523d3ad02f9a2-1200x734.png%3Ffit%3Dmax%26auto%3Dformat&w=96&q=75)
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();
}