visit
At Stream, we build a lot of showcase and example applications to show off the awesome features that our service has to offer. For nearly all of our applications, we host them on an instance — usually or . While maintaining your codebase and keeping it relevant is difficult, we’ve found that the most challenging aspect when it comes to maintaining an application is keeping it alive and running. Additionally, with Node.js being the core language for most of our backend APIs, scaling a single threaded process can be hard to do; that’s where comes in and why we enjoy using it so much. There are many process managers out there, most notably , , and good ol’ . And then there is PM2, with over 60 million downloads and 25k GitHub stars (and rising!). We like PM2 because, simply put, it’s easy to use and makes managing a production environment seamless.
PM2 is a battle tested, production ready runtime and process manager for Node.js applications. It comes with a built-in , as well, which makes scaling applications even easier. Best of all, it works on Linux, Windows, and macOS. With a configuration file (process.json), you specify what processes you want to run and how many you’d like to scale to. When starting PM2, you specify the process.json file and PM2 takes care of the rest. (More on process files in a bit 😉) What all of this means is that PM2 allows you to keep your Node.js applications alive forever, and to reload them with zero downtime when you have updates to your application or server.
If you’re on macOS, installing is as simple as running yarn add global pm2. If you’re on Linux, Windows, or using a Docker container (yes, it supports Docker as well), follow the instructions .
If you’re curious what it should look like, here is an example of our process_prod.json file for Winds, our open-source RSS & Podcast application: As you can see, we’re running several processes, and PM2 handles them without any issues, automatically using the Node.js Cluster API to spawn multiple processes.Once it’s started, your app is forever alive, auto-restarting after crashes and machine restarts — all with one simple command:
pm2 startup
No matter how many applications you’re running, PM2 has a suite of commands that allow you to manage their respective states. Below are a few of our favorite commands (in no particular order):
Running the command pm2 monit will return a rich set of data around your application’s health. For example, you’ll see CPU utilization, memory usage, requests/minute, and more!
PM2 has built-in log management. It aggregates log data from all of your applications and writes it to a single source for viewing. You can even tail the logs in real-time to see what’s going on behind the scenes with your application. Log Management from PM2 comes with log rotation as well, which is important, especially if you’re application is outputting verbose logs on a frequent basis. There are three commands that I use often, and you should too:
pm2 install pm2-logrotate
More information on Log Management can be found . If you’re finding that your instance is filling up with logs often, think about using a centralized logging service such as , , or .Thank you for reading and happy coding!