Running NodeJS on all CPU cores in order to achieve maximum hardware potential

Viewed 787

Hardware:

I'm running Ubuntu 20.04 virtual Machine on VMware Workstation with 4 core CPU and 6GB of RAM

Issue:

I've been searching for how to boost my NodeJS server and one of the solutions was to utilize all CPU cores of the processor which I'm doing right now.

So this is my entry file called server.js:

const express = require("express");

const app = express();

app.use("/", async (res, req) => {
  req.status(200).send('success');
});

app.listen(4300);

With help of pm2 I'm spawning the maximum number of clusters pm2 start server.js -i max and I'm achieving this log:

enter image description here

With help of autocannon I'm targeting this server to kind of 'DDoS' to monitor how good it was to handle multiple concurrent requests.

Here is the code:

const cannon = require('autocannon')

const localHost = 'http://localhost:4300'

async function startMockUp() {
    let result = await cannon({
      url: localHost, // target url
      connections: 10000,   
      pipelining: 1,
      duration: 10 // seconds
    })

    console.log('request', result.requests)
}

startMockUp()

and here is the output for the first run:

enter image description here

Now I'm trying to make the same thing but with only one cluster running:

enter image description here

And getting this result:

enter image description here

So as you can see I'm not achieving x4 speed up running 4 concurrent clusters in addition to that single cluster was able to handle a bit more of requests.

Why is that happening?

Am I doing something wrong?

Is there a workaround to achieve better results?

1 Answers

Look into using cluster to fork off the processes. This is the standard method to use multiple instances of node on a machine. My thought is that with the way you are starting the processes, only one of the instances is actually handling requests.

Related