
If you are a NodeJS application developer, you are developing it in a single-threaded/single-process-based manner by default. If you deploy it on a multi-CPU/core-based server, you are only under-utilizing it. Sure, we can do better, right? Nodejs also provides some facilities to improve its default single-thread nature through its cluster module. Sure, you will have to know how to code that way. But, wouldn’t it be nice if you tell it to run your application in the multi-cluster way, and it just works in that way, without manually coding a bunch of lines?
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 precisely that: making 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 an 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 that needed to be multi-process-based. I also found an existing npm module named “multi-node,” but it wasn’t maintained anymore and was not compatible with the latest Node.js versions. I wasn’t able to find anything else closely similar to its alternatives.
Thus, we implemented our multi-process mechanism with the help of the Hipache source code. Later, I thought of having it as a separate module as a multi-node, so I created its npm package and open-sourced it on GitHub.
Why use this library:
Based on the reputation of the earlier-mentioned multi-node library, it’s obvious that developers liked the idea of having the multi-process handling logic separated and using it as a ready solution instead of implementing it individually for all applications. So, I hope this can help you as an alternative to that library.
This library provides a very easy-to-use interface with two simple functions, requiring almost no additional effort to migrate an existing application to multi-process mode.
Additionally, to have your application in multi-clustered mode, you will have to design and develop it as stateless, which is very beneficial for scaling. It will work seamlessly even if you deploy your application in different geographical locations.
Installing:
It is very easy to install the package as a simple command below:
$npm install clustered-node --save
Code language: PHP (php)
The above command
How To Use This Multi:
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 the proper host/port where it should listen and a number of processes to this module’s ‘listen’ method. So, you don’t have to use the server object’s internal ‘listen’ method. Example:
var server = http.createServer(someCallbackMethod);
require("clustered-node").listen({port:1337, workers:3}, server);
Code language: JavaScript (javascript)
Multi-Process App: if your application is a non-server app, like a crawler, test tool, etc, then you can 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);
Code language: JavaScript (javascript)
Things To Consider Before Migrate:
Though, as shown above, migrating your existing app to a multi-process system is very easy and efficient, there is something you should remember and be careful about your application before you migrate to make sure that the migration goes seamlessly without any issues:
- Your Application Is Stateless: You must ensure that your application is stateless, which means it doesn’t store session data. If it is, migrate the mechanism to use a db/cache server to do that. Also, don’t use a global variable(constants are OK) inside the entry-point method if you use 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 processes on a single CPU core, they are in the end, sharing the same CPU schedule and won’t scale up the overall performance; instead, they could be worse(using OS scheduling for context switching)
Possible Improvements/Enhancements In Future:
This project is in a very early stage, and I already have several things in mind for improvement on this project:
- Unit Tests: I plan to add unit tests that automatically test each unit’s functionality using mocha and chai.
- Support other use cases: I also plan to support other use cases. For example, you can migrate your existing app without even touching it, having the multi-process implementation in a separate file and passing the app’s main JS file as a 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 welcome to contribute and provide feedback on this project. To submit suggestions or bug reports, use the GitHub issue tracker for clustered-node.
Pull requests are also very much welcome. This is just an early-stage implementation, and I believe it can be enhanced/improved in many ways other than what I mentioned above. If you are interested in working on any such parts, feel free to start discussing them on GitHub or contact me.
Let me know if I have missed anything or if you like to know about it. Happy coding!
Discover more from CODESAMPLEZ.COM
Subscribe to get the latest posts sent to your email.
Leave a Reply