Skip to content

movableink/node-hash-ring

 
 

Repository files navigation

node-hash-ring - Consistent Hashing C++ Add-on for node.js


See this blog post for background on the algorithm.

Installation

Via npm:

npm install @movable/hash_ring

The native addon is compiled on install via node-gyp (invoked automatically), so a C++ toolchain is required.

From source:

git clone https://github.com/movableink/node-hash-ring.git
cd node-hash-ring
npm install

Example

const { HashRing } = require("@movable/hash_ring");

// Create a cluster of 3 servers weighted so that 127.0.0.2:8080 stores twice as many
// keys as 127.0.0.1:8080, and 127.0.0.3:8080 stores 4x as many keys as 127.0.0.1:8080
// and 2x as many keys as 127.0.0.2:8080
const ring = new HashRing({ "127.0.0.1:8080": 1, "127.0.0.2:8080": 2, "127.0.0.3:8080": 4 });

console.log(ring.getNode("users:102")); // Should be 127.0.0.3:8080
console.log(ring.getBuckets());         // ['127.0.0.1:8080', '127.0.0.2:8080', '127.0.0.3:8080']

An optional second argument controls the number of replica points per bucket (precision):

const ring = new HashRing({ a: 5, b: 3 }, 500);

TypeScript definitions are bundled (index.d.ts).

Tests

Tests run with mocha:

npm test

License

MIT License


Author

Brian Noguchi

Maintained by Movable Ink.

About

A Consistent Hashing C++ add-on for node.js

Resources

Stars

0 stars

Watchers

4 watching

Forks

Packages

 
 
 

Contributors

Languages

  • C++ 67.3%
  • JavaScript 17.7%
  • C 11.5%
  • Python 3.2%
  • Other 0.3%