Search code examples
node.jsexpresssails.jsnode-cluster

Is it possible to run sails.js on a node cluster?


I am currently running an express server using the node js vanilla cluster setup as demonstrated over here:

http://rowanmanning.com/posts/node-cluster-and-express/

I'd like to move the server over to sails.js and I am wondering if anyone knows how to configure sails to support the node cluster (no proxies, just simple cluster).

TX,

Sean.


Solution

  • First thing - if You want to use sessions, You need to use session store. Otherwise session will be not shared between instances of Your app.

    Then, The easiest way is to use something like PM2, which can be found here: https://github.com/Unitech/pm2

    You dont need to do changes in Your app.js files - it should be written as standard non-clustered sails app. PM2 will do the job.

    Just start the app with pm2 start app.js -i x where x is number of instances or use pm2 start app.js -i max which will start instances that are equal to numbers of processors, or processor threads.

    PM2 is great and very stable, it has many features to run it smoothly in producation, however it has some flaws in development. If You will ever have a problem with "port already in use" after stopping or even deleting app that was using it - You will have to use pm2 kill which will kill ALL Your apps. Other than that - its just great - with some additional monitoring tools.