Backbone mod to enable Redis as store
Last updated 7 years ago by benjie .
Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install backbone-redis-store 
SYNC missed versions from official npm registry.

Work in Progress

This is a work in progress, it's not been thoroughly tested in development, let alone production. You have been warned.


There are npm modules that serve the backbone/redis storage problem, but they seem to include or similar transports to synchronize to the browser. This is great if you need that functionality, but I simply what a backbone.js redis store that neither knows about nor cares about the client.


backbone-redis-store is intended to be a simple (yet powerful) bridge between backbone.js and redis.

It is written in CoffeeScript, so you'll need CoffeeScript to run it.


  • MIT license
  • Written in CoffeeScript
  • Overrides Backbone.sync to check for redisStore and take appropriate action (should revert back to old Backbone.sync if no redisStore is found)
  • Stores models to redis (as values to a keyed hash)
  • Auto-increment ids (optional, can be overridden)
  • Can enforce unique values for arbitrary keys (optional)
  • Indexing (currently only indexes unique keys)
  • Find model by unique key (asynchronously loads if not already loaded)
  • Sets (e.g. foreign key lookups)


Stores the model as JSON encoded data into a redis hash named options.key, where the id of the model is the key to the hash.

Auto-increment counter is stored as next|{options.key}

Unique indexes are stored as a normal redis key: unique|{options.key}:{field}|{value.toLowerCase()} with value: They are not stored in a hash so that we can avoid the use of WATCH and instead use MSETNX to check and set the unique keys.

Why avoid WATCH?

Simply because we're sharing one redis connection across all clients (probably) - we don't want various other WATCHs to conflict, or an UNWATCH to cancel our watch. An alternate solution would be to use a separate redis connection per transaction - this may be implemented in future.



To use it, you would:

# Create a new redisClient (npm install redis)
redisClient = require('redis').createClient()

# Require backbone (npm install backbone)
Backbone = require 'backbone'

# Import backbone-redis-store
RedisStore = require './backbone-redis-store'

# Implement RedisStore's Backbone.sync method
RedisStore.infect Backbone

# Define yourself a new model
class MyModel extends Backbone.Model
  username: null
  uniqueNumber: null
  somethingElse: null

# Define a collection, setting a `redisStore` property
class MyModelCollection extends Backbone.Collection
  redisStore: new RedisStore
    key: 'mymodel'
    redisClient: redisClient
    unique: ['username','uniqueNumber']
  model: MyModel

# Create a new instance of this collection and populate it
myModelCollection = new MyModelCollection()

Note: I would not recommend doing the last line for large stores - I would lazily fetch data as you need it using myModelCollection.getByUnique or similar.

RedisStore options

new RedisStore takes the following options:

  • key - the redis key under which to store the model data
  • redisClient - the connection to redis to use for storage
  • unique - an array of columns of the model that should be treated as unique indexes. An empty array is perfectly valid.


Backbone.Collection.getByUnique(key, value, options)

  • key - the model key to look for
  • value - the value of said key to look for
  • options - accepts success and error callbacks like many Backbone methods


I intend to add the following features in time:

  • Non-unique indexes
  • Redis pub/sub notification of updates (allows a cluster of NodeJS/Backbone instances to stay in sync)
  • Better error handling
  • Package as an npm module
  • Redis connection pooling (maybe)

If you fancy contributing, please get in touch! :)

Current Tags

  • 0.1.0                                ...           latest (7 years ago)

1 Versions

  • 0.1.0                                ...           7 years ago
Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (0)

Copyright 2014 - 2017 © |