• Stars
    star
    349
  • Rank 121,528 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

hashring is a consistent hashing algorithm for Node.js that is compatible with libketama and python's hash_ring package

HashRing

The HashRing module provides consistent hashing that is compatible with the original libketama library that was developed at last.fm. In addition to beeing compatible with libketama it's also compatible with the hash_ring module for Python. See the compatiblity section of the API for more details on this.

Build status

BuildStatus

Installation

The advised installation of module is done through the Node package manager (npm).

npm install hashring --save

The --save parameter tells npm that it should automatically add the module to the dependencies field in your package.json.

Usage

var HashRing = require('hashring');

The HashRing constructor is designed to handle different argument types as a consistent hash ring can be use for different use cases. You can supply the constructor with:

String

A single server, possible, but pointless in most cases if you only use one server, then done use the HashRing at all, it only adds overhead.

var ring = new HashRing('127.0.0.1:11211');

Array

Multiple servers for the HashRing.

var ring = new HashRing(['127.0.0.1:11211', '127.0.0.2:11211']);

Object

An Object where the keys of the Object are the servers and the value can be a Number and it will be seen as weight for server. The value can also be an Object. Where the key can be a weight or a vnode.

Weights or vnodes are used to give servers a bigger distribution in the HashRing. For example you have 3 servers where you want to distribute your keys over but not all servers are equal in capacity as 2 of those machines have 200mb of memory and the other has 3.2 gig of memory. The last server is substantially bigger and there for should receive a greater distrubtion in the ring.

For a rule of thumb use the amount of memory as weight:

var HashRing = require('hashring');

var ring = new HashRing({
  '127.0.0.1:11211': 200,
  '127.0.0.2:11211': { weight: 200 }, // same as above
  '127.0.0.3:11211': 3200
});

If you want create a server with multiple vnodes (virtual nodes):

var HashRing = require('hashring');

var ring = new HashRing({
  '127.0.0.1:11211': { vnodes: 50 },
  '127.0.0.2:11211': { vnodes: 200 },
  '127.0.0.3:11211': { vnodes: 100 }
});

Algorithm

With the second argument you can configure the algorithm that is used to hash the keys. It defaults to md5 and can only contain values that are accepted in Node's crypto API. Alternatively you can supply it with a function for a custom hasher. But do note that the hashValue will be calculated on the result.

Options

  • vnode count The amount of virtual nodes per server, defaults to 40 as this generates 160 points per server as used by ketama hashing.
  • compatiblity Allows you to force a compatibility mode of the HashRing. It default to ketama hash rings but if you are coming from a python world you might want compatibility with the hash_ring module. There's a small diff between hash_ring and ketama and that's the amount of replica's of a server. Ketama uses 4 and hash_ring uses 3. Set this to hash_ring if you want to use 3.
  • replicas The amount of replicas per server. Defaults to 4.
  • max cache size We use a simple LRU cache inside the module to speed up frequent key lookups, you can customize the amount of keys that need to be cached. It defaults to 5000.
  • default port The default port number which will removed from the server address to provide ketama compatibility.
'use strict';

// require the module, it returns a HashRing constructor
var HashRing = require('hashring');

// Setup hash rings with your servers, in this example I just assume that all
// servers are equal, and we want to bump the cache size to 10.000 items.
var ring = new HashRing([
    '127.0.0.1',
    '127.0.0.2',
    '127.0.0.3',
    '127.0.0.4'
  ], 'md5', {
    'max cache size': 10000
  });

// Now we are going to get some a server for a key
ring.get('foo bar banana'); // returns 127.0.0.x

// Or if you might want to do some replication scheme and store/fetch data from
// multiple servers
ring.range('foo bar banana', 2).forEach(function forEach(server) {
  console.log(server); // do stuff with your server
});

// Add or remove a new a server to the ring, they accept the same arguments as
// the constructor
ring.add('127.0.0.7').remove('127.0.0.1');

API's Table of Contents

HashRing.continuum()

Generates the continuum of server a.k.a as the Hash Ring based on their weights and virtual nodes assigned.


HashRing.get(key)

Find the correct node for which the key is closest to the point after what the given key hashes to.

  • key String, Random key that needs to be searched in the hash ring

returns: The matching server address.


HashRing.range(key, size, unique)

Returns a range of servers. Could be useful for replication.

  • key String, Random key that needs to be searched in the hash ring
  • size Number, Amount items to be returned (maximum). Defaults to the amount of servers that are in the hashring.
  • unique Boolean, Don't return duplicate servers. Defaults to true.

returns: The array of servers that we found.


HashRing.swap(*from, to)

Hotswap identical servers with each other. This doesn't require the cache to be completely nuked and the hash ring distribution to be re-calculated.

Please note that removing the server and adding a new server could potentially create a different distribution.

  • from String, The server that needs to be replaced
  • to String. The server that replaces the server

HashRing.add(server)

Add a new server to ring without having to re-initialize the hashring. It accepts the same arguments as you can use in the constructor.

  • server Server that need to be added to the ring.

HashRing.remove(server)

Remove a server from the hash ring.

  • server Server that need to be removed from the ring.

HashRing.has(server)

Checks if a given server exists in the hash ring.

  • server Server for whose existence we're checking.

HashRing.reset()

Reset the HashRing and clean up it's references.


HashRing.end()

Resets the HashRing and closes the ring.


HashRing.find(hashValue) (private)

Finds the correct position of the given hashValue in the hashring.

  • hashValue Number, The hashValue from the HashRing#hashValue method.

returns: Index of the value in the ring.


HashRing.hash(key) (private)

Generates the hash for the key.

  • key String, Random key that needs to be hashed.

returns: The hashed valued.


HashRing.digest(key) (private)

Digest hash so we can make a numeric representation from the hash. So it can be fed in to our hashValue.

  • key String, Random key that needs to be hashed.

returns: An array of charCodeAt(0) converted chars.


HashRing.hashValue(key) (private)

Get the hashed value of the given key, it does the digesting, hashing yo.

  • key String, Random key that needs to be hashed.

returns: The hash value of the key.


HashRing.points(servers)

Returns the points per server.

  • servers Optional server that you want to filter for

returns: A Object with server -> Array of points mapping

Upgrading from 0.0.x to 1.x.x

The 0.0.x releases had some serious flaws that causes it to be incompatible with the 1.0.0 release. These flaws are the reason that 1.0.0 got released. They are not backwards compatible as they change the way that keys are hashed. The following incompatible changes have been made for the sake of consistency:

  • Only accepts hashers that are build in to node (for now). As it can only guarantee proper hashing of values.
  • The replace function was actually doing swaps of keys, so it's original functionality has been renamed to swap. The replace API is now removing the given server and adds it again. As this causes the servers to be properly re-hashed.
  • The module now requires a C++ compiler to be installed on your server as hashing the value requires support for 64bit bitshifting and JavaScript as a language only supports 32bit bitshifting.
  • It adds 4 replica's instead 3 for the servers. This is how libketama originally did it, if you want to have 3 replica's in the hash ring you can set the compatibility option to hash_ring.
  • The API's have be renamed, deprecation notices are added in to place and they will be removed in the next minor version bump (1.1.0)
  • Added human readable configuration options instead of camelcase. This increases readability of the module.
  • CRC32 was removed as crypto engine because it was too unstable.

More Repositories

1

memcached

A fully featured Memcached client build on top of Node.js. Build with scaling in mind so it will support Memcached clusters and consistent hashing.
JavaScript
1,313
star
2

useragent

Useragent parser for Node.js, ported from browserscope.org
JavaScript
886
star
3

versions

Versions, A small module for creating a flexible CDN application
JavaScript
204
star
4

licensing

Discover the license footprint of your application
JavaScript
87
star
5

jackpot

Jackpot, tcp connection pooling for Node.js
JavaScript
75
star
6

creditcard

Creditcard number parsing, validation and information extraction
JavaScript
60
star
7

npmjs

An alternative npm registry client that I like.
JavaScript
50
star
8

argh

argh is a extremely light weight option/argument/process.argv parser for Node.js. It only parses options, nothing more than that.
JavaScript
45
star
9

FlashPolicyFileServer

A simple Flash Policy File Server for node
JavaScript
39
star
10

diagnostics

Tools for debugging your node.js modules
JavaScript
34
star
11

load

Load JavaScript files that do not use the bloat module patterns
JavaScript
24
star
12

cluster.exception

Exception handler for cluster
JavaScript
23
star
13

kuler

Color your terminal using CSS/hex color codes
JavaScript
21
star
14

realtimeplatform.github.com

TBA
21
star
15

licenses

Retrieve accurate license information for a given npm package.
JavaScript
21
star
16

fs.notify

File change notification that doesn't suck hairy monkey balls and just works.
JavaScript
19
star
17

canihaz

canihaz, lazy installing node.js modules
JavaScript
19
star
18

expirable

Expirable cache for node.js, FUCKING AWESOME YO!
JavaScript
18
star
19

mana

Mana allows api-oriented clients greater stability and high availability in demanding distributed battles and boss fights.
JavaScript
16
star
20

fingerprinting

Produce a unique string for any given resource, commonly used in cache busting practices.
JavaScript
11
star
21

nodejsconfit-2011

Demo's from nodejsconf 2011
JavaScript
11
star
22

find-package-json

Find package.json files in parent directories using an iterator based API. Keep searching till you find the one you're looking for or when you run out of folders to search in.
JavaScript
11
star
23

renderme

Render README files that people use on Github
JavaScript
10
star
24

one-time

Run the supplied function exactly one time (once).
JavaScript
9
star
25

.files

The . files, vim & build commands etc
Vim Script
9
star
26

shrinkwrap

npm shrinkwrap parsing, generation and comparison
JavaScript
9
star
27

text-hex

Create a hash from a string of text and transforms it to a color
JavaScript
8
star
28

env-variable

Cross platform environment variables with process.env, window.name, location.hash and localStorage fallbacks
JavaScript
7
star
29

commenting

Wrap content in a comment that is valid for a given file type.
JavaScript
7
star
30

node-bisection

Bisection algorithm for node.js
JavaScript
7
star
31

fn.name

Extract the name from a function.
JavaScript
7
star
32

senpai

Notice me senpai! A graceful degrading notification system for browsers.
JavaScript
7
star
33

memcached-stream

High performance streaming memcached protocol parser for Node.js. Code name: flaming-octo-bear
JavaScript
6
star
34

node-algorithms

A series of algorithms ported to JavaScript to be used on Node.js
JavaScript
5
star
35

koekje

A sessionStorage interface for cookies
JavaScript
5
star
36

uni

uni[versal] command line
JavaScript
5
star
37

ansidown

Convert a subset of markdown do a somewhat ANSI compatible output
JavaScript
5
star
38

failover

cluster-fuck management
JavaScript
4
star
39

Spry-Configurator

Spry files configurator and combinator. Version 2.0 is currently running on config-spry-it.com
JavaScript
4
star
40

gjallarhorn

Child process management without blasting your server in to pieces.
JavaScript
3
star
41

setup-env

Automated, feature detected, environment setup for your test suite
JavaScript
3
star
42

jQuery-scrollector

Navigate to elements using hashtag based CSS3 selectors,
JavaScript
3
star
43

odoyle

Micro Rules Engine powered by a simple JSON structure
JavaScript
3
star
44

connected

Simple wrapper to ensure that your `server.listen` calls receive an err like all regular callback signatures.
JavaScript
3
star
45

woohoo

WooHoo is Celebration as a Service.
JavaScript
3
star
46

YUNO-UPDATE

Y U NO update your browser
JavaScript
3
star
47

enabled

Check if a certain flag is enabled.
JavaScript
3
star
48

colorspace

Generate HEX colors for a given namespace.
JavaScript
3
star
49

mono-repos

Highly opinionated set of utilities to orchestrate and administrate mono repositories.
JavaScript
3
star
50

assign

Map/Reduce promise like returned API -- Really not way to properly describe this module..
JavaScript
3
star
51

queueback

Callback queue system for optimizing your HTTP requests
JavaScript
3
star
52

require-poisoning

Cache-Poisoning applied to Node.js require statements so you inject your own modules
JavaScript
3
star
53

rip-out

Rip out properties from an object that you don't want to have included in the cloned result.
JavaScript
2
star
54

setHeader

Protect headers from being overridden before they are written to response.
JavaScript
2
star
55

Outside

Go Outside to chat
JavaScript
2
star
56

storage-engine

EventEmitter abstraction on top the React-Native AsyncStorage API
JavaScript
2
star
57

package-link

npm link that doesn't suck
JavaScript
2
star
58

shrubbery

Shrubbery is small Map() backed plugin registry
JavaScript
2
star
59

treason

Cheating on React's JSX by using JSON
JavaScript
2
star
60

booting

Async Initialization Process
JavaScript
2
star
61

darknode

HTTP/Docker interface for Darkweb/yolo
JavaScript
2
star
62

connection-parse

Simple TCP connection string parser
JavaScript
2
star
63

enu

Extract enumerable and non-enumerable properties from an object.
JavaScript
2
star
64

node-browserscope

Browerscope.org results based on user agent queries
JavaScript
2
star
65

dollars

The "write less, do more, save dollars" library
JavaScript
2
star
66

timbers

Shiver me timbers! A tool to map short CLI argument to long CLI arguments
JavaScript
1
star
67

debug.css

debug.css
1
star
68

truth

Data is the only source of truth.
JavaScript
1
star
69

sparklet

Sparklet is a sparkline pagelet for BigPipe
JavaScript
1
star
70

window.name

A module which exposes sessionStorage API for storing data in the window.name
JavaScript
1
star
71

parser

Redis based IRC protocol parser based on lua scripting
Lua
1
star
72

platform-specific

Platform specific information for isomorphic applications
JavaScript
1
star
73

nock-knock

Advanced router handling for nock
JavaScript
1
star
74

dotfig

Configuration extraction from dot files or package.json fields
JavaScript
1
star
75

Spry-UI-ProgressBar

Progress bar widget for the Spry Framework
JavaScript
1
star
76

orbited

JavaScript
1
star
77

zoidberg

Flow control? Why not zoidberg
JavaScript
1
star
78

all-the-things

Random development of all the things, which should not be published yet, but does need to be tested
JavaScript
1
star
79

Spry-YQL-DataSet

YQL for Spry Data Sets
JavaScript
1
star
80

send-help

Generate developer friendly output of a help command based on a given object structure.
JavaScript
1
star