PM2 - Production Manager for Node.js

Miquel Canal

Tuesday 1, September 2020
  • Software Engineering
  • JavaScript
  • System Administration

What is PM2?

PM2 is a process manager for node.js applications. It helps to manage and maintain production application running online 24/7. A powerful tool that will facilitate common system administration tasks for node apps. Its auto reloading process will ensure applications run forever without any downtime.

Node.js app reload using PM2

PM2 exposes reload capabilities which allow to reload an application without any downtime. As opposed to restart, which kills and restarts the process, reload achieves a 0-second-downtime reload.

PM2 allows to reload an application based on a memory limit. The service has a 30 seconds interval that checks if a memory threshold has been reached and reloads the app accordantly.

The --watch parameter can also be passed in when the application is initialised. This can be useful to reload the application automatically whenever there is a change on the current directory or its subdirectories.

PM2’s Ecosystem

The use of ecosystems in PM2 allows to define app configurations such as environment variables or log files based on a process file. An ecosystem can be easily generated using a single command pm2 ecosystem.

After an ecosystem has been created, PM2 can start up applications using configurations stored on ecosystem files. This is particularly useful for micro-service applications running on development or production modes.

PM2 load-balancing

PM2: Node.js load balancing

One of the key powerful features of PM2 is its built-in node.js application load balancer. When a node.js application gets started in cluster mode, PM2 is going to automatically scale its processes across the available CPUs. This is accomplished without any code change.

Running an application in cluster mode is going to increase the performance and reliability of your app. PM2 load balancing relies on Node.js’ cluster mode.

It is important to keep in mind that applications running behind a load balancer should be stateless apps. There shouldn’t be data stored on the process as PM2 is going to run in multiple CPUs.


Domain Driven Design (DDD)

Domain Driven Design (DDD)

What is Domain Driven Design and why it is important to define a clear terminology? This article covers the key aspects of developing software following best practices of Domain Driven Design.

Code Reusability in Software Programming

Code Reusability in Software Programming

What is code reusability? This article reviews the concepts around code reuse and how to apply them to software development.

The 12-Factor methodology

The 12-Factor methodology

The twelve-factor app is a methodology used to build software-as-a-service apps that are easy to scale up without significant changes to tooling or architecture.

This site uses cookies to ensure a great experience. By continue navigating through the site you accept the storage of these cookies.