TQ
dev.com

Blog about software development

Subscribe

High performance JavaScript web service using Cluster

01 Aug 2016 - by 'Maurits van der Schee'

You can't start a discussion nowadays about high traffic web services or somebody will bring up NodeJS and it marvelous performance. Skeptic as I am I test these claims before I believe them. The following code runs pretty fast on my machine, but only uses one core of my CPU:

const http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200);
  res.end('hello world\n');
}).listen(8000);

I ran the following Apache Bench command to test the performance:

ab -n 200000 -c 100 http://localhost:8000/

It actually performs pretty good at 15k rps using one CPU core at 100%. This is half the rps compared to Go, mod_php or Jetty, but those all use multiple cores and for one core this measurement is pretty impressive. So much of the claim is true (on one core).

NodeJS Cluster for multi core

Okay, now let's apply clustering to use all cores/CPUs:

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', function (worker, code, signal) {
    console.log('worker ${worker.process.pid} died');
  });
} else {
  // Workers can share any TCP connection
  // In this case it is an HTTP server
  http.createServer(function (req, res) {
    res.writeHead(200);
    res.end('hello world\n');
  }).listen(8000);
}

I must be doing something wrong as there was no performance improvement on my machine. On the contrary: total performance even went down from 15k rps to 12k rps using multiple CPU cores!

Advantages of using NodeJS

Although it may not be as fast as Java or Go, NodeJS does still have an impressive set of advantages to be your favorite choice:

  1. No compilation: NodeJS is a scripting language.
  2. Popular language that can be used in front-end and back-end alike.
  3. Easy to install, easy to run and easy to deploy, great for quick prototyping.

But if you want to build a mature high performance micro-service I would still choose a compiled language, because even if you would be able to achieve the same request rate in a 'hello world' example, then you actual business logic will be heavier in JavaScript than in a compiled language (such as Go).