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:

  1. Refresh the timer endpoint (e.g., http://localhost:3000/timer).

  2. Observe CPU usage spike for the corresponding process in the dashboard.

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