Wynn Netherland changelog.com/posts

cluster: Plugin-enabled multi-core server manager for Node.JS

It’s fun to watch the Node.js ecosystem grow so rapidly. As Node matures, the community continues to build out tools around the project to make it easier to deploy more robust web applications.

The latest is Cluster from LearnBoost, an easy way to run a Node.js application across multiple cores. With support for both the 0.2.x and 0.4.x flavors of Node, Cluster offers an impressive list of features:

  • Hot restarts with zero downtime
  • Hard and graceful shutdown options
  • Workers
    • commit suicide when master dies
    • are spawned one per cpu by default
  • Plugins including the following out of the box
    • cli: provides a command-line interface for your cluster
    • debug: verbose debugging information
    • logger: master / worker logs
    • pidfiles: writes master / worker pidfiles
    • reload: reloads workers when files change
    • repl: perform real-time administration
    • stats: adds real-time statistics to the repl plugin

As expected, Cluster can be installed via npm:

npm install cluster

To crank up your server in a cluster, create your Node.js server as normal and start with cluster instead:

var cluster = require('cluster'), http = require('http');

var server = http.createServer(function(req, res){
  console.log('%s %s', req.method, req.url);
  var body = 'Hello World';
  res.writeHead(200, { 'Content-Length': body.length });
  res.end(body);
});

cluster(server)
  .use(cluster.logger('logs'))
  .use(cluster.stats())
  .use(cluster.pidfiles('pids'))
  .use(cluster.cli())
  .use(cluster.repl(8888))
  .listen(3000);

Got an idea for a plugin? Get forkin’.

[Source on GitHub] [Project home page]


Discussion

Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00