If you are a NodeJS application developer, by default you are developing it in single threaded/single process based. If you are deploying it on a multi-cpu/core based server, then you are only under-utilizing it. Sure, we can do better, right? Nodejs also provide some facility to improve its default single-thread nature by its cluster module. Sure you will have to know first how to code in that way. But, wouldn’t it be nice if you just tell it to run your application in multi-cluster way and it just work in that way, without coding bunch of lines manually?
The ‘clustered-node’ Library:
I am happy to introduce you to a new nodejs open source library that I have created recently, which will facilitate the exactly that, creating your nodejs server/application in multi-process/clustered mode in a super easy way, without knowing much inner details of how clustering works. The library is available as npm package and the source is available on github:
NPM Package: https://www.npmjs.org/package/clustered-node
Source On Github: https://github.com/ranacseruet/clustered-node
Background:
I was working on a small proxy server at my job which was needed to be multi-process based. I also found an already existing npm module named “multi-node“, but turned out it wasn’t maintained anymore and not compatible with latest nodejs versions. Didn’t able to find out anything else closely similar as its alternatives.
Thus, with help of the source code of hipache, we implemented our own multi-process mechanism. Later on, the thought came to my mind the have it as a separate module as multi-node and thus created its npm package and open sourced it on github.
Why using this library:
From the reputation from the earlier mentioned multi-node library, its obvious that, developers liked the idea of having the multi-process handling logic separated and use it as a ready solution, instead of implementing it individually for all applications. So, as an alternative of that library, this can help you, I hope.
This library provides very easy to use interface with two simple function that requires almost no additional effort to migration an existing application to multi-process mode.
Additionally, to have your application in multi-clustered mode, you will have to design and develop your application as stateless, which is very beneficial when it comes to the question of scaling. Even if you deploy your application on different geographical location, it will work seamlessly.
Installing:
It is very easy to install the package as a simple command below:
$npm install clustered-node --save
The above command
How To Use:
Currently this library offers two different use cases:
- Multi-Process Server: If your application is a nodejs server you can easily pass the server object with proper host/port where it should listen to and number of processes to this module’s ‘listen’ method. So, you don’t have to use server object’s internal ‘listen’ method. Example:
var server = http.createServer(someCallbackMethod); require("clustered-node").listen({port:1337, workers:3}, server);
- Multi-Process App: if your application is a non-server app, like a crawler, test-tool etc, then you can just pass your entry point method as the parameter, along with the number of processes to this module’s ‘run’ method. Which will be in turn called for every process created. Example:
function runMyApp() { //start your application coding starts here } require("clustered-node").run(config, runServer);
Things To Consider Before Migrate:
Though as shown above, migrating your existing app to multi-process based is very easy and efficient, there is something you should remember and careful about your application before you actually do the migration to make sure that the migration goes seamlessly without any issue:
- Your Application Is Stateless: You will have to make sure that your application is stateless, which means it doesn’t store session data itself. If it is, migration the mechanism to use db/cache server to do that. Also, don’t use global variable(constants are OK) inside the entry-point method if you are using the ‘run’ method.
- Number Of Process Is Reasonable: You shouldn’t just put any number as the number of processes to be run. You should keep it synced with the number of CPUs/Cores your server is running on and use that number. For example, if you are running two process on a single cpu core, they are at the end sharing same CPU schedule and won’t actually scale up the overall performance, instead could be worse(using OS scheduling for context switching)
Possible Improvements/Enhancements In Future:
This project is on very early stage and I already several things in mind for improvement on this project:
- Unit Tests: I am planning to add unit tests to test each unit of functionality automated way with use of mocha and chai.
- Support other use cases: I am also planning to support other use cases like, you will be able to migrate your existing app without even touching it, having the multi-process implementation on a separate file and pass the app main js file as parameter. This will give developers the peace of mind and flexibility to switch back and forth from/to single/multi-thread mode instantly anytime.
Your contribution/Feedback:
You are more than welcome to contribute provide feedback on this project. Just use the github issue tracker for clustered-node for suggestions/bug reports.
Pull requests are also very much welcome as well. This is just an early stage implementation and I believe can be enhanced/improved in lot of ways other than what I mentioned above. If you are interested to work on any such parts, feel free to start discussing on github or contact me.
Let me know if I have missed anything or you like to know about. Happy coding!
Discover more from CodeSamplez.com
Subscribe to get the latest posts sent to your email.
Leave a Reply