← Back to Articles
Documentation2026-11-02 • 8 min read

PM2 Production Mastery: Memory, Clusters & Health Metrics

Written by Siji AI (MooLux Tech Team)

# Production Process Management with PM2: Monitoring Memory Restarts, Clusters, and Health Metrics

In modern DevOps and GNU/Linux environments, reliable process supervision is non-negotiable. PM2 (Process Manager 2) has become the industry standard for node-based and containerized workflows, offering robust clustering, memory guardrails, and enterprise-grade observability. This guide breaks down how to leverage PM2 for high-availability deployments across MooLux infrastructure and beyond.

Why PM2 Dominates Production Workflows

Unlike traditional init systems, PM2 is engineered for modern application lifecycles. It handles zero-downtime reloads, automated log rotation, and ecosystem configuration out of the box. For MooLux GNU/Linux and MooLang backends, PM2 bridges the gap between developer convenience and production resilience, eliminating the need for brittle shell scripts.

Mastering Cluster Mode for Horizontal Scaling

PM2’s cluster mode leverages Node.js child_process.fork() to spawn multiple worker processes across all available CPU cores. This eliminates the single-thread event loop bottleneck and provides automatic OS-level load balancing.

bashMooLux Terminal
# Start workers equal to available CPU cores
pm2 start app.js -i max

# Dynamically scale workers up or down
pm2 scale api-worker +2
pm2 scale api-worker -1

Architectural Tip: Always pair cluster mode with sticky sessions if your application handles WebSockets or persistent user connections. Configure max_memory_restart at the process level to prevent memory leaks from degrading cluster performance.

Proactive Memory Monitoring & Automatic Restarts

Memory leaks are the silent killer of long-running services. PM2 tracks RSS and heap usage in real-time, triggering graceful restarts before Out-Of-Memory (OOM) errors crash your process or trigger kernel-level intervention.

Configure limits via CLI or ecosystem.config.js:

javascriptMooLux Terminal
module.exports = {
  apps: [{
    name: "prod-api",
    script: "server.js",
    max_memory_restart: "500M",
    autorestart: true,
    watch: false,
    log_date_format: "YYYY-MM-DD HH:mm:ss Z"
  }]
};

Performance Tip: Set max_memory_restart slightly below your container’s memory limit (e.g., 450M for a 512M Docker limit). This prevents the Linux OOM Killer from bypassing PM2’s graceful shutdown hooks, ensuring clean process teardowns.

Health Checks & Real-Time Metrics Dashboard

Production visibility requires more than pm2 status. Enable PM2’s built-in monitoring endpoint to integrate with Prometheus, Grafana, or custom alerting systems.

bashMooLux Terminal
# Start with built-in monitoring enabled
pm2 start ecosystem.config.js --watch --monitoring

# Launch interactive CLI dashboard
pm2 monit

Access the HTTP dashboard at http://:9615. You can also query metrics programmatically for CI/CD pipelines or alerting scripts:

bashMooLux Terminal
# Extract specific memory usage in bytes
pm2 describe prod-api | jq '.monit.memory_usage'

# Watch CPU/Memory live in terminal
watch -n 2 'pm2 monit --json' | jq '.monit.cpu_usage'

Best Practice: Implement custom health checks using the --health-check-url flag or wrap your app in a lightweight middleware returning 200 OK on port 8080. PM2 will automatically restart the process if the endpoint fails to respond within the timeout window.

Best Practices for Containerized & Bare-Metal Deployments

| Deployment Scenario | PM2 Strategy | Expected Outcome | |---|---|---| | Docker / Kubernetes | Disable native clustering (-i 0). Use --pm2-runtime for faster boot. | Lets orchestrator handle scaling & load balancing | | Bare Metal / VPS | Use -i max. Enable log rotation (max_logs: "30D"). | Full CPU utilization & disk space management | | MooLang / Edge Nodes | Pin memory limits, disable watch, set kill_timeout: 3000 | Rapid failover & minimal edge latency |

Regardless of infrastructure, always persist your process tree using pm2 save and generate a systemd/boot script with pm2 startup. This guarantees your supervisor survives kernel panics and system reboots.

Conclusion

Mastering PM2 transforms fragile development scripts into resilient production architectures. By combining cluster scaling, memory guardrails, and health monitoring, you eliminate entire categories of downtime. Deploy smarter, monitor deeper, and keep your MooLux infrastructure running at peak efficiency.