DEV Community

Smit Vaghasiya
Smit Vaghasiya

Posted on

2 1 2 1 1

Scaling Your Node.js App with PM2: Cluster Mode vs Fork Mode

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
Enter fullscreen mode Exit fullscreen mode

Check the version to ensure it’s installed:

pm2 --version
Enter fullscreen mode Exit fullscreen mode

🔁 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"
      }
    }
  ]
};
Enter fullscreen mode Exit fullscreen mode

Start it with:

pm2 start ecosystem.config.js
Enter fullscreen mode Exit fullscreen mode

⚡ 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
Enter fullscreen mode Exit fullscreen mode

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"
            }
        }
    ]
};
Enter fullscreen mode Exit fullscreen mode

Start it with:

pm2 start ecosystem.config.js
Enter fullscreen mode Exit fullscreen mode

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|
Enter fullscreen mode Exit fullscreen mode

🧪 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!

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay