Implementation of connect store using node's native cluster messaging
Latest commit cd6b18e Jan 27, 2017 @crandmck crandmck committed with sam-github Update with API documentation

Connect Session Store for Cluster

Build Status NPM version


Strong-cluster-connect-store is an implementation of connect session store using node's native cluster messaging. It provides an easy way for using sessions in connect/express based applications running in a node cluster.


  • Supports both Connect and Express.
  • No dependencies on external services.
  • Module is shipped without connect, it will use your version of connect or express.
  • Covered by unit-tests.

See also API documentation.


$ npm install strong-cluster-connect-store


Configuration - connect

var connect = require('connect');
var ClusterStore = require('strong-cluster-connect-store')(connect);

var app = connect();
  .use(connect.session({ store: new ClusterStore(), secret: 'keyboard cat' }));

Configuration - express

var express = require('express');
var ClusterStore = require('strong-cluster-connect-store')(express);

var app = express();
  .use(express.session({ store: new ClusterStore(), secret: 'keyboard cat' }));

Setting up the master process

The store requires that a shared-state server is running in the master process. The server is initialized automatically when you require() this module from the master. In the case that your master and workers have separate source files, you must explicitly require this module in your master source file. Optionally, you can call setup() to make it more obvious why you are loading a module that is not used anywhere else.

// master.js

var cluster = require('cluster');
// etc.


// configure your cluster
// fork the workers
// etc.

Using Strong Cluster Connect Store

The following example assumes you have set up Strong Cluster Connect Store for Express and have run the steps above.

'use strict';
var express = require('express');
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var ClusterStore = require('strong-cluster-connect-store')(express.session);

if (cluster.isMaster) {
  // The cluster master executes this code


  // Create a worker for each CPU
  for (var i=0; i<numCPUs; i++) {

  cluster.on('online', function(worker) {
    console.log('Worker ' + + ' is online.');

  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + + ' died with signal',signal);
} else {
  // The cluster workers execute this code

  var app = express();

    { store: new ClusterStore(), secret: 'super-cool' }

  app.get('/hello', function(req, res) {
    var msg;
    if (req.session.visited)
      msg = {msg: 'Hello again from worker '};
      msg = {msg: 'Hello from worker '};

    req.session.visited = '1';
    res.json(200, msg);