Sticky sessions for Node.js clustering done responsibly
what is it?
This module provides sticky sessions for Node.js at the cluster level, routing requests to the underlying workers by producing hashes very quickly.
I built upon the idea by adding support for IPv6, and attempted to streamline the implementation as much as possible for the
lipstick consumers so that only minimal changes to your application are needed.
- Node.js >=
npm install lipstick --save
lipstick, your master has to communicate with their workers effectively.
Here's a ready to use production-grade
cluster.js file. It will listen on port
PORT as defined in your environment variables. It will use
app.js as your worker process and spawn
os.cpus().length workers, or
2 of them, whichever is bigger. It will also route messages to the workers based on a hash of their IP address, applying the stickiness.
The API for your
cluster.js module is detailed below.
lipstick(appfile?, options?, done?)
appfile defaults to
./app.js and will be used as the worker process. Options are detailed below.
||The port your application listens on|
||The amount of workers your cluster should spawn|
You'll have to make a slight modification in your
app.js workers. Just change the following line:
lipstick equivalent shown below:
require('lipstick').listen(app, port, cb);
This will allow
lipstick to patch your worker processes.
node app will work as usual.