Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.JS built in cluster or PM2 clustering?

Which one is better?

I have activated Nodejs clustering mode with workers but now I discovered PM2 that does the same thing. I'm using keymetrics to see the stats from my webserver and I have noticed that when I launch my NodeJS node (with a built in cluster) without using PM2 cluster feature, Keymetrics reports 20/30MB of Ram used.

If I deactivate clustering (inside node) and I switch on PM2 cluster, keymetrics reports about 300MB of Ram usage.

Now, which method is better and why with a built in cluster keymetrics reports only 30MB of ram usage?

like image 598
leodev Avatar asked Nov 29 '15 10:11

leodev


People also ask

Does PM2 use cluster?

PM2 internally uses the Node. js cluster module, but everything including the edge cases is handled for us and we don't even have to touch our existing code to get this working.

Why we use PM2 in node js?

PM2 allows you to configure several different strategies for how your Node. js application should restart. By default, it restarts your application if it exits or crashes to minimize the impact to your customers in production while the source of the crash is investigated.

What is node js cluster?

Node. js runs single threaded programming, which is very memory efficient, but to take advantage of computers multi-core systems, the Cluster module allows you to easily create child processes that each runs on their own single thread, to handle the load.

What is cluster in PM2?

The cluster mode allows networked Node. js applications (http(s)/tcp/udp server) to be scaled across all CPUs available, without any code modifications. This greatly increases the performance and reliability of your applications, depending on the number of CPUs available.


2 Answers

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
like image 118
Yashrajsinh Jadeja Avatar answered Sep 16 '22 15:09

Yashrajsinh Jadeja


I use PM2. There are a number of reasons it is better.

  1. Unlike using core's clustering, your code needs little to no modification to use PM2. Clustering logic doesn't belong in every app we ever build.
  2. It scales from the command line. I can simply run pm2 scale my-app +1 to add another worker in realtime after deployment.
  3. You should already be using PM2 anyway to keep the process alive. So clustering comes for free.

I cannot reproduce anything close to your 300MB number. In fact, I recently had a leaky app that I had to use --max-memory-restart on and even in that situation memory usage usually stayed below 100MB. Though it wouldn't surprise me in the slightest if PM2's clustering used more memory, simply because it does a lot for you out-of-the-box.

My suggestion would be to not prematurely optimize. Use PM2 until you genuinely need to squeeze every drop of memory / performance out of your systems (definitely not before you have lots of traffic). At that point you can figure out what the bare minimum is you need from clustering and can re-implement just those parts yourself.

Resources

  • Clustering walkthrough: https://keymetrics.io/2015/03/26/pm2-clustering-made-easy/
  • PM2 tutorial: https://futurestud.io/tutorials/pm2-cluster-mode-and-zero-downtime-restarts
like image 33
Seth Holladay Avatar answered Sep 20 '22 15:09

Seth Holladay