📜 ⬆️ ⬇️

Clustering a nodejs web server using node-clusterize-cli

For the last six months, I have been developing a fairly large web application, under the hood of which NodeJS is roaring and smoking. When it came to production deployments, I wondered: “why don't I use several threads with the application instance?”.

Having realized the cluster, I saw that the productivity from its use increased by 1.5 times , which is not very bad, given the small amount of effort spent. But I decided not to dwell on this, and make a convenient CLI for working with a cluster in order to untie the code that is responsible for starting the cluster from a specific application. In addition, I really wanted to demonize the cluster so that it hung in silence in the process, raised fallen workers, wrote to the logs, and did not distract anyone anymore.

This is how node-clusterize-cli appeared - a command line utility that does not affect application code. Based on the original “cluster” NodeJS module.

')
Work mechanism

The module is simple - to start, it starts the master process, in which the number of workers you need (by default, two processes per core) starts, and then untie the master process from the CLI interface and leave it running like a daemon. In addition, the master process listens to error messages that may occur in workers, and automatically restarts fallen comrades. For each request to be processed by a random process, the module from the NodeJS API called “cluster” is responsible.

Using

In general, everything is trivial:

#    $ npm i -g node-clusterize-cli #   $ clusterize --app ./app.js --workers 32 --log ./cluster.log 


Also, for convenience, there is a method for getting a list of running clusters, and a method for killing them:

 # ,  `ps -eo pid,comm | grep "clusterize master"` $ clusterize list 53416 app.js #   `kill -9 CLUSTER_PID` $ clusterize kill 53416 


More details can be found in the hint - clusterize -h and readme on GitHub.

repository on github
Ab load test logs

Source: https://habr.com/ru/post/208914/


All Articles