My Sites


Monday, September 19, 2016

Nodejs Developer - Production Best Practises : Demystifying how clustering works ( CPU Threads vs Cores vs Sockets vs Processes)

First thing first. Every application needs to be optimized and perform fast. When it's come to Nodejs, you will come across many best practices to optimize your Nodejs application with tremendous performance and reliability; especially in the production environment.
Most of the Nodejs applications use Express as the Nodejs web framework. And there, they have specifically mentioned a lot of production best practices covering performance aspects and reliability aspects. Basically, the things to do in your code and environment/setup which will ultimately improve your application performance. Below article will guide through those best practices.
https://expressjs.com/en/advanced/best-practice-performance.html

But what I want to emphasize in my article is one fact among that list; which is 'Run your app in a cluster'. Basically, a single instance of Node.js runs in a single thread. To take advantage of multi-core systems  you will sometimes want to launch a cluster of Node.js processes to handle the load. Following is the note provided by them.
'Clustering is made possible with Node’s cluster module. This enables a master process to spawn worker processes and distribute incoming connections among the workers. However, rather than using this module directly, it’s far better to use one of the many tools out there that does it for you automatically; for example node-pm or cluster-service.'

Anyway, here what I want to talk about is the Node's cluster module. How it works with the CPU. Of course, you can copy paste the code given in the documentation and it will surely work as a charm. But what is actually happening inside the CPU with regards to this. Let's have a look in depth.

First We'll look how CPU (Central processing unit) works. Here mainly we talk about # of threads and # of cores. In following snapshot, in the left hand side it shows my local machine CPU information (Intel(R) Core(TM) i7-4600M CPU @ 2.90GHz) and in the right side it shows the CPU information of a AWS C1.xlarge server (Intel(R) Xeon(R) CPU E5-2651 v2 @ 1.80GHz).

CPU information can be displayed by following Linux commands.
cat /proc/cpuinfo
lscpu


Here CPU(s) means # of threads eventually logical cores. So in this case, my cluster enabled Node.js application will run in 4 processes in my local machine and will run 8 processes in the AWS server.

And here we can find Core(s) per socket and No. of sockets. In my local machine, the total No. of cores are 2 which is Dual core (Actual hardware). And in AWS server, there is only 1 core (Actual hardware). And from Thread(s) per core will give you the idea of about the relationship between threads and cores.

A core is the physical hardware that works on the thread. In general a processor can only work on one thread per core, CPUs with hyper-threading enabled can work on up to two threads per core. In this case, my local machine is hyper-threading enabled.







This i7 CPU has only 2 cores, but can handle 2x2=4 threads , and hence appears as a quad core to the OS.(4 logical cores).

Hope this will give you an understanding about, how CPU behaves and how clustering relates with the CPU. Cheers!!! :)