How to use Cluster to manage a Node.js application
masterCluster manages workers (spawning one per CPU by default) and provides features like zero-downtime restarts, graceful shutdowns, and worker resuscitation.
To use Cluster, you typically pass your application logic (as a module or a path) to the cluster() function, chain plugins using .use(), and then call .listen(port).
Best Practice: Instead of requiring your app in the master process, pass the path to your application file to the cluster() function. This prevents the master process from unnecessarily creating database connections, as the file will only be require()ed within the workers.
var cluster = require('cluster');
// Passing the path to the app file is recommended to avoid
// unnecessary database connections in the master process.
cluster('./app')
.use(cluster.logger('logs'))
.use(cluster.stats())
.use(cluster.pidfiles('pids'))
.use(cluster.cli())
.use(cluster.repl(8888))
.listen(3000);