7 ms·
PM2: Production Process Manager with a Built-In Load Balancer
- kszyh 3y agoDespite a few shortcomings, such as the lack of automatic scaling, it is definitely more stable than, nginx unit, which we had a lot of problems with, due to segfaults and very "tricky" nodejs support.
- arcza 3y agoI like PM2 and it's successfully been keeping my APIs alive quite well, but how does the future look given the popularity of Kubernetes? What is the overlap like and what's not overlapping?
- 4pkjai 3y agoI think Kubernetes is unnecessary for the vast majority of deployments. Simple apps run perfectly fine with things like PM2, Monit and init.d
- occz 3y agoBe that as it may - if you already have Kubernetes up and running, what would be the additional value of having both PM2 running alongside Kubernetes? Fwiw, last time I asked myself this question my conclusion was that there was no additional value, and we opted to not use PM2. This was a long time ago however, so take it with a grain of salt.
- 4pkjai 3y agoI agree with you on that
- deleted 3y ago[deleted]
- sublinear 3y agoPM2 is great if you have an app composed of multiple services that need to share an environment. You may want to reduce communication overhead between these node processes or avoid involving kubernetes to share resources. Implementation details would drive this. Also, kubernetes cluster workers can fail. How do you want your workload to behave in that case? When your container is killed, should these services fail together? Do you want a better separation of concerns between your app dev team and your devops/middleware team (think disaster recovery and cluster maintenance)? Where does the app dev end and the devops begin? For other languages such as Java or C++, it would be out of the question to let these details matter to anyone but app devs. Their entire apps are in one docker image. Kubernetes should only manage instances of the app and the obvious dependencies that should be externalized (redis, memcached, postgres, mysql, etc.) In my experience it's really a question of failure modes in your architecture and squeezing out small gains in performance that matter at scale.
- keepamovin 3y agoI use PM2 with BrowserBox and it's been great. Reliable, scalable, performant. PM2 has been with us since the early days. We've never looked for an alternative.
- pkstn 3y agoPM2 was really nice until I moved to using Docker Swarm Mode which is even better in so many ways!
- cocochanel 3y agoFor some reason I thought Docker Swarm was abandoned. From the Docker docs, > Docker Swarm mode is built into the Docker Engine. Do not confuse Docker Swarm mode with Docker Classic Swarm which is no longer actively developed.
- azophy_2 3y agothe change from docker classic swarm into current docker swarm happened years ago. from what I can tell, usage-wise there are not much difference between the two. however the sad thing is currently swarm is held by other company (mirantis) after being sold by docker inc. and the new company doesn't seem that invested in improving swarm. you could say its in life support now save for a few community contribution more explanation by dockerswarm.rocks website: https://dockerswarm.rocks/swarm-or-kubernetes/ https://dockerswarm.rocks/swarm-or-kubernetes/
- victorbjorklund 3y agoJust used PM2 on a recent project for deploying a nodeapp on a single server. Very easy to use and just works.
- SSchick 3y agoTangentially: Writing this at 1:30am, might not be very insightful. We actually are phasing out the use of pm2 in our org since we are already running within kubernetes. And running pm2 within k8s seemed redundant (and is likely a relict of previous deployment strategies). It also made certain debugging tasks trickier as we very rarely like to attach debuggers to our staging environment and the cluster/fork behavior of pm2 made it hard to effectively target indivual executions/processes. Further we had some issues running nextjs after upgrading to the latest version within pm2 when using custom configuration files (we did not end up finding the root cause but decided to drop pm2, least path of resistance etc). One other thing that also gave us headaches was the fact that pm2 allows specifying different env variable values for different environments but no other configuration (such as cluster count etc.) We were successful entirely replacing it with docker-compose configs locally that are ultimately closer to how our services are deployed in production. Overall I think pm2 is suitable for local dev and prototyping small to medium complexity services.
- ilaksh 3y agoIt seems strange that you would even consider using pm2 still if you have all of that other stuff. I use it to avoid the complexity of those things since my operational needs are usually very modest.
- hosh 3y agoAt work, we have kubernetes and pm2 running on a pet server outside of kubernetes. I cannot wait to get rid of pm2 and the pet server. For smaller projects, I'd rather just work directly with systemd or better yet, use the podman-systemd integrations.
- Aldipower 3y agoI am running a 18 node SOA since 4 years with pm2 and I am very happy. Very reliable. No need for the complexity of Kubernetes or even Mesosphere DCOS back in the day. I do not even consider to change anything.
- 0x6c6f6c 3y agoAll on a single host, or is this spread across multiple servers? Curious about how people run it with the latter approach if they do
- Aldipower 3y agoActually on 3 hosts, but only the primary is at the edge, one other is a hot-standby at another geographic location and the third is for incremental backup purposes via ZFS. The database sync connection are tunneled via Wireguard. Failover to the hot-standby is done via DNS (ClouDNS). Also I run the setup in a Linux Container (LXC). Deployment is done via SCP and Tar. :-D This setup is very reliable to me and at the other hand cheap!
- andrewstuart 3y agoIs systemd capable of doing this? I thought process managers like supervisor and pm and the others had all been superseded by systemd, unless you’re in a non systemd distro of course.
- Kharacternyk 3y agoI don't think systemd has a built-in load balancer. Also, running multiple copies of the same service isn't typical PID-1 stuff, so maybe doing that with systemd involves some friction.
- stephenr 3y agoRunning multiple "copies" of a service is very much in systemd's wheelhouse; it's called templated units.
- Kharacternyk 3y agoI know about templates, but I don't believe they are designed for fungible copies. You can run a bunch of my-app@a-random-port.service, but it's not the same UX as `pm2 start my-app -i number-of-copies`.
- stephenr 3y agoThey work very well for "worker" processes. The default ssh package on Debian uses systemd template units for the user process; I've used them extensively for job queue workers, in combination with max execution time and a separate timed job to monitor queue load (essentially automatic scale up/down of queue workers). Realistically if your prod environment relies on you manually running `pm2... ` or whatever, you're doing something wrong. User Experience should have about 0% impact on how your service daemons run.
- roryrjb 3y agoI created a Node.js binding for sd-notify to cover at least some of the use case here: https://github.com/systemd/node-sd-notify https://github.com/systemd/node-sd-notify
- contextnavidad 3y agoHad good experiences with PM2, however be cognizant of it's copy left license. We have found it redundant with the likes of Kubernetes.
- byyll 3y ago> be cognizant of it's copy left license why?
- contextnavidad 3y agoSome organisations forbid copy left licenses
- BearScale 3y agoI set up my first nodejs backend project on an ec2 instance with pm2 almost 9 years ago. I've also been toying with an instant deploy pipeline that takes advantage of its watch and restart.
- edwinjm 3y agoPM2 is very good and I really wonder why it's still somewhat unknown. People complaining that Node.js only use one CPU core, or benchmarks with low scores for Node.js. There are solutions for this! PM2 is one of them and is very good. Use it.
- nesarkvechnep 3y agoJust spawn more processes. Great solution! /s
- sublinear 3y agoWould you rather scale with more containers? That's heavier. Properly written node apps are bound by I/O throughput. CPU and memory cost for additional processes is minimal, but latency is significantly decreased with a worker pool.
- jitl 3y ago> Properly written node apps are bound by I/O throughput Does that mean that node apps shouldn’t do any computation on data, just move bytes between a file and a network socket? If I need my program to think, I wrote it wrong or in the wrong language? I would rather have shared memory multithreading be a usable feature in my program’s runtime. SharedArrayBuffer exists but 98% of existing code uses objects and arrays so can’t be easily shared between processes without copying, and the cost of copying objects with structuredClone puts a huge optimization barrier in between most code and parallel processing.
- sublinear 3y agoNo, just write your javascript to do more I/O than compute. Apps should do app things not low level tasks that slow things down. If you need to compute something complicated, write it in a compiled language and make it a node module. Many popular npm libraries are exactly this.
- 3y ago
- nsonha 3y agoCould some one please explain to me if this is still gonna be useful if we already have load balancing on the API gateway?
- kszyh 3y agoNo, it won't. It's basically just a NodeJS application server. On the plus side, you get a dashboard with statistics like latency, memory usages etc. But from your description it seems that it will be a redundant solution.
- nsonha 3y agohow does a normal load balancer make sure the nodejs server utilizes all cores?
- lioeters 3y agoPM2 has worked very well in my experience. I have about a dozen services (APIs and full-stack applications) across a number of servers, remote and local. Mostly Node, these days trying with Bun. Some of them have been running for years without a hitch. It's been totally reliable. I'm sure there are alternatives, lighter-weight and closer to the metal, but I haven't had the need. PM2 does the job.
- mynegation 3y agoIt may well be that I did not learn to use it properly, but - to provide a counter opinion - pm2 left a sour taste with me. Tried to use it few years back and by default it does not rotate the logs, did not handle machine reboots well, and at times I was confused what is running and what is not. I settled on docker for small projects and kubernetes for larger ones and these things made more sense to me. I admit it could be a matter of habit.
- sublinear 3y agoMy experience starting out with PM2 was the same. You need a plugin for the log rotation, saving the default environment variables is confusing, and stale processes cause weird behavior. You may already have your own in-app log rotation before adding PM2 so may want to avoid printing to stdout/stderr. For usage with docker you want pm2-runtime.
- zarzavat 3y agoI came to the opposite conclusion for the same reasons. Docker/kubernetes is fine for things that make money, but for small personal projects it is far too much complexity and I dumped it. pm2 gets the job done with less effort overall.
- dartos 3y agoI’ve found pm2 more complicated than docker-compose for local/self hosted small projects. Why did you prefer pm2?
- __s 3y agopm2 gets past involving containers I get to rsync my code up to a server, & pm2 restart it. The server process runs like any other, no container issues to consider, just a json file saying how I'd run it myself if I wanted to tbf I'd rather replace pm2 with systemd, since I'm using it to run a Rust program, but if I was using node their cluster mode looks neat
- zarzavat 3y ago
- teaearlgraycold 3y agoBe warned. It’s AGPL. I’ve never been able to reach the developers to get commercial licensing.
- kszyh 3y agoIt has a paid versions: plus and enterprise. But actually, I couldn't find any information on their site about what the support covers.
- diamondfist25 3y agoI used pm2 to run my adhoc Python jobs/bot. Tried with Django and celery and docker, ended up being a pain in the ass since I have to modify on the fly. Pm2 is neat, but the monitor solution is pricey.
- TZubiri 3y agoIt's just something that's not needed. You can come up with a Unix solution by using bash, piping to logs, daemonizing with &. And as a bonus, it works for any program, not just with node stuff. Remember, use the tools available in your foundation before adding more blocks to the pyramid
- iTokio 3y agoIs there a robust generic solution already existing? To launch multiples processes, load balance and automatically restart if one crash..
- flemhans 3y agoMaybe systemd? I just always find the unit files so cumbersome to deal with (actually I feed pm2 to ecosystem files to ChatGPT and let it deal with the conversion)
- erichocean 3y agoDoes anyone have an alternative to pm2 that's reliable? That's my biggest issue with it: it gets slower and slower to do commands like `pm2 list` over time, and eventually, pm2 itself crashes—taking everything it runs with it. Also, `pm2 flush` doesn't actually truncate all logs, just the logs of whatever processes pm2 happens to be running at that moment. I workaround this by killing pm2 entirely and `rm -Rf .pm2` once a month or so. (This is all on Linux BTW.)
- koshergweilo 3y agoI've had a lot of luck with dokku. It uses docker so it's more heavyweight, but it's super simple and scales really well
- josegonzalez 3y agoDokku maintainer here. Dokku itself doesn't run anything in resident (there is a small go binary that listens to the docker event stream and restarts apps as necessary but thats it), so there shouldn't be overhead there. On the Docker side, there is a _slight_ amount of overhead in cpu/networking, but its so negligible that its sometimes difficult to benchmark. While I can't really make changes to Docker to alleviate issues, I'd be interested in knowing what parts of Docker are heavyweight so we can better surface any such information to Dokku users.
- lmeyerov 3y agoIt's a fascinating project, and esp for from before docker+k8s normalized and nodejs was reinventing the universe Nowadays, docker compose or k8s ecosystems do a lot of this stuff in a more team-friendly & ecosystem-supported manner. I always liked pm2 bc the dynamic API layers... except docker & k8s also have APIs, and you get all sorts of extras with them by being a layer below I wonder if there is a name for this waller garden phenomena. The pm2 team built a wonderful universe and it keeps improving afaict, but tech ecosystems also have been improving around them and at faster & bigger levels...
- mvkel 3y agoI've experienced some strange behavior with pm2 on my projects where a reboot would completely wipe the configs and I'd have to rebuild the processes (even after a pm2 save) If it happens again I'll likely look into doing it the old fashioned way with systemd. Skill issue? Most certainly. It still sucks to think I've figured it out (to the point of testing with manual reboots) only to one day have nothing running and have no clue why.
- andrewmcwatters 3y agoI don't understand why people think k8s is a good replacement for pm2. The resource requirements to run containers over standard processes is tremendously different in practice. Can you make Kubernetes deployments easier than this? cd myapp # Ignore multer. NODE_ENV=production PORT=80 HTTPS_PORT=443 pm2 start bin/www --name myapp --watch --ignore-watch="\.git node_modules uploads" pm2 startup pm2 save
- xs83 3y agoPM2 fits the job perfectly when you want to start managed processes from the CLI without a config like you would with something SystemD or SupervisorD). It has some nice interfaces for observability. Its more lightweight than spinning up Docker containers for each process
- lyu07282 3y agoHorrible piece of software, it uses a stateful(!) configuration. The daemon updates it's state at runtime and there is absolutely no guarantee it will properly restore that state when it restarts. It's extremely unreliable/flaky, you really don't want that from such a critical service.
- hinkley 3y agoIs pm2 claiming that the cluster module is a load balancer? Because I don’t really consider round robin to be a full load balancer. Maybe 25 years ago but not today. The node cluster module needs a way to provide your own balancing algorithm. Then I wouldn’t feel the need to put a reverse proxy in front of it.
- pier25 3y agoThankfully since I started hosting my Node apps on Fly.io I don't have to care about PM2.
- RadiozRadioz 3y agoI used this to run an entire startup for 4 years (complex data needs, not CRUD). It was a stopgap that was good enough to stay. I always wanted rid of it, but there were always more pressing problems. Used the load balance feature extensively, which worked but starts to falter when you get to big scale. It's not a stable foundation, it's a thing that's been hacked around enough to the point where the problems are mostly gone. If I did it all over again, I'd pick something proper like systemd or k8s. I would say this is more a surprisingly capable dev tool than a real service manager. Take a look at the code. E.g. https://github.com/Unitech/pm2/blob/master/lib/API.js https://github.com/Unitech/pm2/blob/master/lib/API.js I don't want this thing running my production environment if I can help it.