Improving Node.js Application Performance With Clustering

Dinesh Rawat
3 min readApr 5, 2023
Take Advantage of Node.js Cluster and Child Processes with PM2

Let’s say you have a restaurant and you are expecting a lot of customers for dinner. You have four chefs in your kitchen who are responsible for preparing all the dishes. Each chef has a different specialty and can cook a different type of dish. However, they can only prepare one dish at a time.

Now, if you get a large number of orders, your chefs may not be able to keep up with the demand. To address this issue, you can divide your kitchen into four separate stations, with one chef working at each station. This way, each chef can focus on preparing a smaller set of dishes and can work more efficiently.

Similarly, in PM2’s Cluster mode, you can divide your Node.js application into multiple instances, with each instance running on its own CPU core. This way, each instance can handle a smaller set of incoming requests, and can work more efficiently, resulting in better performance and faster response times.

On the other hand, if you are expecting fewer customers, you may not need all four chefs working at once. In this case, you can have one chef in the kitchen, and the other three on standby, ready to help if needed.

Similarly, in PM2’s Fork mode, you can run a single instance of your Node.js application, which can be sufficient for smaller applications that don’t require a lot of processing power. PM2 can still manage and monitor the single instance, providing benefits like automatic restarts in case of crashes and log management.

In summary, just as dividing your kitchen into multiple stations allows your chefs to work more efficiently and handle more orders, PM2’s Cluster mode allows your Node.js application to handle more requests and work more efficiently. Conversely, just as having one chef in the kitchen can be sufficient for smaller orders, PM2’s Fork mode can be sufficient for smaller applications that don’t require a lot of processing power.

Fork vs Cluster Mode

To start a Node.js script using PM2 in cluster mode, you can use the following command:

pm2 start app.js -i <number of instances> --name <app name> 
-e <error log file> -o <output log file>

Here’s what each parameter does:

  • app.js: This is the name of your Node.js script.
  • -i <number of instances>: This specifies the number of instances you want to run in cluster mode. You can set this to the number of CPU cores on your server or to a specific number of your choosing.
  • — name <app name>: This sets the name of the application that will be used by PM2 to manage the process.
  • -e <error log file>: This sets the name of the error log file for the application.
  • -o <output log file>: This sets the name of the output log file for the application.

For example, to start a Node.js script named server.js with 4 instances in cluster mode, you could use the following command:

pm2 start server.js -i 4 --name myApp -e err.log -o out.log

To start a Node.js script using PM2 in fork mode, you can use the following command:

pm2 start app.js --name <app name> 
-e <error log file> -o <output log file>

Here’s what each parameter does:

  • app.js: This is the name of your Node.js script.
  • — name <app name>: This sets the name of the application that will be used by PM2 to manage the process.
  • -e <error log file>: This sets the name of the error log file for the application.
  • -o <output log file>: This sets the name of the output log file for the application.

For example, to start a Node.js script named server.js in fork mode, you could use the following command:

pm2 start server.js --name myApp -e err.log -o out.log

I hope this helps!

--

--

Dinesh Rawat

Seasoned software engineer, Content creator, Helping teams achieve their goals. https://www.linkedin.com/in/dinesh-rawat/