clusterdb

In-memory persistence for Node.js clustering across workers.

npm install clusterdb
2 downloads in the last week
4 downloads in the last month

clusterdb

In-memory persistence for Node.js clustering across workers.

Usage

Master

cluster = require 'cluster'
cpus = require('os').cpus().length

Require the master cdb instance.

cdb = require('../').master

For the 0.0.1 release, creating a data model is required. You then reference these keys elsewhere.

cdb.init
  users: { }

Create some children (giggles).

if cluster.isMaster

  i = 0 
  cluster.setupMaster 
    exec: './child.litcoffee'

  while i < cpus
    proc = cluster.fork()
    i++

  cluster.on 'fork', (worker) -> 

    console.log "Forked server worker #{worker.process.pid}"

Let ClusterDB know about the child.

    cdb.addChild worker

  cluster.on 'exit', (worker, code, signal) -> console.log "Server worker died #{worker.process.pid}"

Child

Require and init.

cdb = require('../').child

cdb.init()

Create a dummy user.

user = 
  id: process.pid 
  name: "JohnQ #{process.pid}"

All actions are async.

upsert

cdb.upsert 'users', user, () -> 

fetch

cdb.fetch 'users', user.id, (user) -> 

destroy

cdb.destroy 'users', user.id, () ->

Sanity

For the 0.0.1 release, callbacks are overwritten:

cdb.fetch 'users', user.id, (user) -> # Won't return.
cdb.fetch 'users', user.id, (user) -> # Won't return.
cdb.fetch 'users', user.id, (user) -> # Returns.
npm loves you