Using PM2 in Production: Advanced Cluster Management

When we’re in development, PM2 is typically used sparingly—mostly for configuring and testing clusters periodically to ensure they remain functional after any changes. However, PM2 truly shines in production environments with live clusters. Let’s explore some of the advanced capabilities that PM2 offers, which would be challenging to replicate using Node.js’s built-in cluster functionality.
Starting Fresh with PM2
Deleting Running Processes
To remove any running processes from the list tracked by PM2, use the pm2 delete
command:
pm2 delete server
This clears all running processes. Your cluster is now reset.
Starting the Server
You can start your server using the following command:
pm2 start server.js --log logs.txt -i max
server.js
: The name of your main JavaScript file.--log logs.txt
: Specifies the name of the file to which logs will be sent.-i max
: Ensures the number of instances matches the number of logical CPU cores on your machine.
Once started, you’ll see information about each process, including the total number of instances.
Detailed Process Information
For more granular details about a specific process, use the pm2 show
command with the process ID:
pm2 show 0
You’ll get metrics such as:
Memory usage.
Node.js event loop latency and wait times.
Path to the logs and script.
Duration for which the process has been running.
Managing Individual Processes
PM2 allows you to manage individual processes directly:
Stopping a Process
If you detect an issue with a specific process, you can stop it temporarily:
pm2 stop 4
Here, 4
is the process ID.
Restarting a Process
When ready, restart the stopped process:
pm2 start 4
This process management flexibility ensures minimal disruption to the cluster.
Viewing Logs
PM2 automatically creates log files for all server activity. For example, the logs.txt
file includes logs for each process and records every restart event. This centralized logging simplifies debugging and monitoring.
Live Monitoring with PM2
To access a real-time dashboard of your processes, use:
pm2 monit
This command provides a live overview of:
Memory usage.
CPU usage.
Status of each server process.
Simulating Server Load
You can test the dashboard’s responsiveness by making a request to a CPU-intensive endpoint in your application. For example:
Refresh the timer endpoint (e.g.,
http://localhost:3000/timer
).Observe CPU usage spike for the corresponding process in the dashboard.
Once the task completes, the CPU usage will return to normal.
Zero-Downtime Restarts (Preview)
In an upcoming lesson, we’ll explore how to use PM2’s zero-downtime restart feature. This powerful capability ensures that updates can be deployed without disrupting active requests.
Key Takeaways
PM2 simplifies cluster management in production environments by offering advanced features like detailed process metrics, individual process control, and centralized logging.
Its live monitoring capabilities allow you to track the performance of your application in real-time.
Upcoming features like zero-downtime restarts make PM2 an invaluable tool for ensuring seamless production deployments.
Stay tuned for the next lesson on zero-downtime restarts—a crucial feature for maintaining high availability in production environments!
Subscribe to my newsletter
Read articles from Muhammad Sufiyan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Muhammad Sufiyan
Muhammad Sufiyan
As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021. With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences. In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS. I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.