Clustering in NodeJs - Performance Optimization - Part II

Clustering is meticulously explained using the process manager. PM2 is chosen for simplifying implementation. NodeJs native clustering module concept is covered in Part-I

Danish Siddiq
tajawal
4 min readSep 30, 2018

--

Sometimes simple things look complicated

What is a Process Manager?

Process manager was introduced to bring easiness in effectively managing processes on a server and taking server resources benefit without an interruption from an application code.

Why?

I advised before reading about the process manager, you must read Part-I about clustering module in NodeJs to know the purpose of process managers.

Process manager will take responsibility for managing applications on different processor cores without being worrying about a master and worker headache. It helps to keep application independent of cores and processes management.

Pros:

  1. Abating code complexity of cores management.
  2. Session management.
  3. Multiple applications management on a server.
  4. Cores utilization responsibility delegated to process manager.
  5. Inter-Process Communication IPC complexity removed.

Which?

There are many process managers available but in the current article, we will explore PM2. Since it is simple, easy and provides features like logging, monitoring, and much more. Later we will compare the performance of PM2 with clustering module for same lines of code as used in the previous example.

How to install:

npm install PM2 -g

How to use:

The best thing about PM2 is easiness. Some of the basic commands for using PM2 are following one:

To Start:

The following command will start an application. As of now consider that we have applications defined in a script for PM2. Script file example is shared after commands introductions. In case if you are intolerant to jump to an example then directly start reading script example.

pm2 start file-name.js

To Stop an Application/Process:

pm2 stop pid or App-name

To Restart an Application/Process:

Restart will kill and start an application

pm2 restart pid or App-name

To Reload an Application/Process:

As opposed to restart, which kills and restarts the process, reload achieves a 0-second-downtime reload.

pm2 reload pid or App-name

To Delete an Application/Process:

pm2 delete pid or App-name

To effect “All” Applications/Processes:

“All” attribute will affect every process and application.

pm2 reload all

To Monitor:

pm2 monit

To View Logs:

pm2 logs

Above mentioned commands can be used directly in CLI to start an application but preferred approach is to set up an ecosystem file and control applications from a script file.

Eco System File:

I am using same code base as used in the previous article so a performance comparison can be developed with PM2 and node “Clustering” Module.

I created two applications with the same code base but listening requests on different ports in code while setting up the server by express. Following script will be used for managing these applications from PM2 using ecosystem file.

Ecosystem.config.js

Script Dissection:

  1. “apps” attribute is used for defining multiple applications on the server. Two apps are defined with their entry point file path is defined in script attribute.
  2. “exec_mode” defines application running mode. It can have two types either “fork” or “cluster” mode. Fork mode is helpful when utilizing pm2 for PHP and python. Cluster mode is defined for node applications which will eventually access NodeJs cluster module on a lower level. It is great for zero-configuration based process management.
  3. “instances” as obvious from the name, PM2 will take care of forking process on multiple instances but listening on the same port. “Clustering” application is defining the maximum number of possible CPU’s available while “Clustering_Replica” application is asking for only two instances.
  4. PM2 also helps to manage application scripts in different environments. Other attributes for staging and production are only added to give a quick idea.

Terminal Output:

Running above script produces the following result showing processes ids along with further relevant information including memory and CPU usage.

Performance Metrics — Clustering vs PM2:

Apache benchmark is used for measuring an application performance, helpful in load testing. Example tested using two approaches, setting up a server with “cluster” module and second with “PM2”. Results were almost the same, a marginal difference which is negligible. Look out the statistics:

Request time in milliseconds

Conclusion:

  1. PM2 is as good as using the cluster module in an application, therefore it is better to use process manager to reduce code complexity.
  2. Multiple applications management is also possible through centralized ecosystem file.
  3. The PM2 dashboard helps in runtime application monitoring.

“Life is really simple, but we insist on making it complicated.”
Confucius

Source Code:

Relevant file is ecosystem.config.js in code repository.

--

--