Skip to content

jhurt/consistent-hashring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

consistent-hashring

A simple, pure JS implementation of consistent hashing.

Build status

BuildStatus

Install

npm install consistent-hashring

Basic Usage

Create a new hashring

const ConsistentHashRing = require('consistent-hashring');
let hashring = new ConsistentHashRing();

Add some nodes

for (let i = 0; i < 10; i++) {
  hashring.addNode('node_' + i);
}

Distribute some keys among the nodes

const nodeForKeyA = hashring.getNode('A');
const nodeForKeyB = hashring.getNode('B');

Remove some nodes, only keys associated with the removed nodes need to be re-hashed:

hashring.removeNode('node_50');
hashring.removeNode('node_90');

Advanced Usage

Weight the nodes

By default, 100 replicas of each node are distributed around the ring. You can change this by passing a replicaCount as the second parameter of addNode:

hashring.addNode('nodeA', 50); //half the default weight
hashring.addNode('nodeB', 200); //twice the default weight

Specify the hashing algorithm

By default, a ConsistentHashRing uses Gary Court's murmurhash3_32_gc implementation for hashing. You can modify test/test.js and run npm run test to see how well your hash function distributes. You can inject your own hashing algorithm when instantiating a ConsistentHashRing

const myGreatHashFunction = function(str) {
  //return some hash of str
};

let hashring = new ConsistentHashRing(myGreatHashFunction);

License

MIT

About

A simple, pure JS implementation of consistent hashing.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published