Node.JS built in cluster or PM2 clustering?

It actually depends on how your Node application works. If your application is stateless then it is easy to use pm2 cluster mode as it does not require much effort (or no effort) in code changes. But if your application uses local data, sessions or using sockets then it is recommended to use Node.js inbuilt cluster module and start your application normally using pm2.

My Node application is using sockets and MQTT so I can’t directly use pm2 cluster mode (pm2 start app.js -i max) as same node application will run on every CPU and it was creating multiple socket connection with the client. So I have to manage Clusters and Workers manually using Node cluster and have to use sticky-sessions and socket.io-redis like node packages to setup proper communication flow between all workers. And then starting my node app using simply pm2 start app.js

Below are some links which can be helpful.

  • PM2 Clustur mode
  • PM2 Recommendation Note
  • Node Cluster

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)