Node.js Web Server in Practice: Using PM2 Cluster Mode to Boost API QPS
Preface
pm2 is a Node.js process management tool. https://pm2.keymetrics.io/
For details, see a previous article: https://blog.csdn.net/uikoo9/article/details/79018750
This article introduces pm2’s cluster mode,
and uses pm2’s cluster mode to boost Node.js API QPS.
autocannon
The load testing tool used is autocannon.
For details, see: https://blog.insistime.com/nodejs-autocannon
Test code available at: https://github.com/uikoo9/qiao-z-autocannon
The test framework is qiao-z: https://qiao-z.vincentqiao.com/#/
Load Testing the API
Download the Code
https://github.com/uikoo9/qiao-z-autocannon
Start the Service
# Install
npm i
# Start
npm run qz
Load Test
Using the res.send API in qiao-z as an example:
autocannon -p 1 -c 10 -d 10 http://localhost:9999/res-send
Load test result: 66791.28

pm2 cluster
Start the above service using pm2 cluster mode.
First, start with 2 instances:
pm2 start qz.js -i 2
Load test result: 100192

Similarly, using pm2 cluster mode to start from 2 instances up to the maximum number of instances,
running load tests and recording results sequentially:

The trend shows: 2 instances yield the highest QPS, decreasing from 2 to 8

Conclusion
1. Using pm2 cluster mode to start multiple Node.js instances on a single machine improves API QPS
2. From a single Node.js instance to 2 pm2 cluster instances, QPS improves by about 50%
3. Increasing pm2 cluster instances does not further increase QPS
4. Point #3 is mainly due to the overhead of inter-instance communication in pm2 cluster (or Node.js cluster)
5. Best practices should be determined by testing on the actual server