ariko stephen
ariko stephen

Reputation: 87

Performance of NodeJS Server In cluster VS non-cluster modes for a Hello World server

I ran performance tests on a simple Node.js "Hello World" server in cluster mode (using PM2) and non-cluster mode on my 10-core ubuntu machine. I ran the tests using Apache benchmark tool with concurrency levels of 10, 100, 1000 and 10, 000 . The total number of requests was kept at 100, 000 for all concurrency levels. The figures in cluster and non cluster columns are for total time taken in seconds to complete all the 100, 000 requests at different concurrency levels. The performance results show that non-cluster mode performed at least 1.5X better than cluster mode at all concurrency levels.

Performance table at different concurrency levels

|  Concurrency Level  | Cluster Mode (s) | Non-Cluster Mode(s) |
|---------------------|------------------|---------------------|
|          10         |         20.1     |         6.4         |
|          100        |         17.4     |         7.0         |
|          1000       |         16.3     |         9.1         |
|          10000      |         27.3     |         18.1        |

My Expectation

Cluster mode should perform close to 10X faster since it distributes traffic across the 10 CPU cores unlike non cluster mode with one event loop on a single core handling all requests. As seen in the performance table, the actual results are very far from expectation

Has anyone else experienced similar results? Any insights into why cluster mode is performing worse?

Why is cluster mode used for node server in production?

Server Code

//server.js
import http from "http"

const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "text/plain" });
  res.end("Hello, World!");
});

server.listen(3000, () => {
  console.log("Server running on port 3000");
});

Run Server

$ pm2 start server.js -i 10  (cluster mode)
$ node server.js             (non-cluster mode)

Performance Test Commands

$ ab -n 100000 -c 10    http://localhost:3000/
$ ab -n 100000 -c 100   http://localhost:3000/
$ ab -n 100000 -c 1000  http://localhost:3000/
$ ab -n 100000 -c 10000 http://localhost:3000/

Upvotes: 2

Views: 63

Answers (0)

Related Questions