Jump Consistent Hashing Library for Erlang/OTP
Erlang C Makefile
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
c_src
doc
perf
src
.gitignore
.travis.yml
LICENSE
Makefile
README.md
rebar.config
rebar.lock

README.md

jch-erl Build Status

Jump Consistent Hashing Library

NIF wrapper for Jump Consistent Hash algorithm by John Lamping and Eric Veach developed at Google, Inc. Paper: "A Fast, Minimal Memory, Consistent Hash Algorithm.

This library have 2 implementations: the xorshift64* pseudo-random number generator and the linear congruential random generator as in the paper. First one is reasonably fast but, more importantly, memory efficient. Second one is compatible with implementations in other languages.

Performance results (via Travis CI) for

Usage

Two functions jch:ch/2 and jch:ch/3 are offered. Simply pass in a 64 bit long (or less) integer key argument followed by the desired bucket or continuum partition size. The function returns the partition allocated for the key. Optional 3'rd argument is atom orig to use compatible algorithm or atom xorshift64 to use more eficient algorithm.

Performance is very stable as bucket size increases and distribrution across the ring is stable (standard deviation for a reasonable sample size is typically <5% relative to the mean.

Example

% ERL_LIBS=deps erl +sfwi 1 +scl false -pa ebin
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V5.10.3  (abort with ^G)
1> jch:ch(1,128).
29
2> jch:ch(13,128).
121
3> jch:ch(13,128).
121
4> jch:ch(13, 128, orig).
56
5> jch:ch(trunc(math:pow(2,64))-1,128).
78
6> jch:ch(trunc(math:pow(2,64)),128). %% off by 1 mate
** exception error: bad argument
     in function  jch:ch/2
        called as jch:ch(18446744073709551616,128)
7> %% off by 1 mate
7>

Enjoy!