A consistent hashringcycle for sharding your dataz, with 100% more blue glow and 50% less Wagner.
npm install light-cycle
var Lightcycle = ;var cycle =seed: 0xdeadbeefsize: 50;// Create entries in the hash ring for each of our redis caches, using redis:host:port as// their unique ids in the ring.for var i = 0; i < myRedisInstanceslength; i++var redisShard = myRedisInstancesi;cycle;// Now we have something to cache in one of our shards.var dataToStore =id: '3421'data: 'This is very important data that must be cached in our redises.'timestamp: Date;// Where shall we store this?var whichRedis = cycle;whichRedis;
Resources are any object or identifier you wish to store. You can store an open database connection, a resource identifier, or something else. This module does not attempt to inspect or use the resource. It just implements a sharding scheme based on the resource's id.
Construct a cycle.
Settings may include the following fields:
seed: seed for the hash function; must be a positive integer; defaults to
size: expected number of resources you'll be storing; defaults to 128
replicas: number of replicas to store in the cycle for each resource; defaults to
If you want your light-cycle to behave identically to other invocations, pass the same hash seed.
Add a resource to the cycle. This will create replicas entries in the underlying data structure. The
id parameter must be a string.
Remove the resource with the given id from the cycle. This removes all replica entries.
Given the id of some data you wish to locate, return the resource where it should reside.
id may be a string or a buffer.
Resize the cycle to accomodate the current number of entries plus some padding.
This is called automatically if the number of entries added exceeds the size option passed in at configuration. Rebalance is not automatically called when resources are removed.
Hash keys are cached, so rebalancing shouldn't be too slow, but to avoid thrash rebalancing pads out the size and the replica count by
Lightcycle.REPLICAS_PAD respectively. For best results, choose a size setting at start that can accomodate the number of resources you intend to use.
Wikipedia is informative.
This page is another good introduction to consistent hash rings.
And there's always the original paper.