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.
--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.
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
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.
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.