If you’re building a production-grade Node.js application, you’ve probably heard of PM2 — a powerful process manager that helps you manage and keep your application alive forever.

But did you know PM2 supports multi-threading using cluster mode?

In this article, I’ll walk you through the difference between Cluster mode and Fork mode in PM2, and how to use them with a real example using ecosystem.config.js.

🧠 What is PM2?

PM2 is a production process manager for Node.js applications. It handles:

  • Process management
  • Load balancing
  • Automatic restarts
  • Monitoring and logging

You can install it globally using:

sudo npm install -g pm2

Check the version to ensure it’s installed:

pm2 --version

🔁 Fork Mode (Single-threaded)

Fork mode is the default mode in PM2. It runs a single instance of your app per pm2 process. This is useful for simple apps or scripts that don’t require multithreading or load balancing.

Example config:

module.exports = {
  apps: [
    {
      name: "my_app",            // Replace with your desired app name 
      script: "npm",
      args: "run dev",
      env: {
        NODE_ENV: "production"
      }
    }
  ]
};

Start it with:

pm2 start ecosystem.config.js

⚡ Cluster Mode (Multi-threaded / Load-balanced)

If your app is CPU-bound or needs to handle multiple requests efficiently, cluster mode is your friend. It uses the cluster module internally to spin up multiple instances (forks) of your app — one for each CPU core.

This brings:

  • Better performance
  • Automatic load balancing
  • Multi-core CPU usage

For Cluster mode, you can install it globally using:

sudo npm install -g @socket.io/pm2

Example config:

module.exports = {
    apps: [
        {
            name: "my_app", 
            script: "index.js", // Your app’s entry point
            instances: "max",   // Automatically uses all CPU cores
            exec_mode: "cluster", 
            env: {
                NODE_ENV: "production"
            }
        }
    ]
};

Start it with:

pm2 start ecosystem.config.js

This will run your app in cluster mode, utilizing all CPU cores efficiently.

PM2

✅ When to Use What?

|Mode           |Use When...                                                |
|---------------|-----------------------------------------------------------|
|Fork Mode      |Simpler apps, background jobs, or single-threaded logic    |
|Cluster Mode   |You want to scale across all CPU cores and handle more load|

🧪 Final Steps: Useful PM2 Commands

  • Check app status: pm2 status
  • View logs: pm2 logs
  • Restart app: pm2 restart all
  • Stop app: pm2 stop my_app
  • Save process list for auto-start on reboot: pm2 save
  • Start automatically on server reboot: pm2 startup
  • Follow the command after pm2 startup it gives: sudo env PATH=$PATH:/home/ubuntu/.nvm/versions/node/vXX.X.X/bin pm2 startup systemd -u ubuntu --hp /home/ubuntu)

Conclusion

Using PM2 with cluster mode can significantly boost your app’s performance by utilizing all available CPU cores. It’s an easy way to scale your Node.js app without switching to more complex solutions.

Let me know in the comments how you're using PM2, or if you’ve tried out both modes!