Skip to main content
Deno 2 is finally here πŸŽ‰οΈ
Learn more

JCH - Jump Consistent Hash Library for Javascript in Deno.

A Fast, Minimal Memory, Consistent Hash Algorithm.

We present jump consistent hash, a fast, minimal memory, consistent hash algorithm that can be expressed in about 5 lines of code. In comparison to the algorithm of Karger et al., jump consistent hash requires no storage, is faster, and does a better job of evenly dividing the key space among the buckets and of evenly dividing the workload when the number of buckets changes. Its main limitation is that the buckets must be numbered sequentially, which makes it more suitable for data storage applications than for distributed web caching.

Paper: https://arxiv.org/abs/1406.2294

Usage

const a = ch(286293355577, 500000) // support TS type Number. Since all JS numbers are 64 bits floating number.
const b = ch("127.0.0.1", 1073741824) // currently not support type String yet. Maybe can port outputs to crc32 or bkdr in the future.

or check example code located at ./ex/01.ts.

The algorithm works by using a hash of the key as the seed for a random number generator. It then uses the random numbers to β€œjump forward” in the list of buckets until it falls off the end. The last bucket it lands in is the result. The paper has a more complete explanation of how it works and a derivation of this optimized loop.

While evenly divide keys into buckets uniformly, we can change total bucket amount(add/remove buckets) but still remain the old key values as possible.

You may see results like this:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (iter idx) β”‚ Key β”‚ Values β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚          0 β”‚   1 β”‚   7208 β”‚
β”‚          1 β”‚   2 β”‚   7198 β”‚
β”‚          2 β”‚   9 β”‚   7158 β”‚
β”‚          3 β”‚   8 β”‚   7352 β”‚
β”‚          4 β”‚   0 β”‚   7156 β”‚
β”‚          5 β”‚   7 β”‚   7257 β”‚
β”‚          6 β”‚   5 β”‚   7217 β”‚
β”‚          7 β”‚   3 β”‚   7103 β”‚
β”‚          8 β”‚   6 β”‚   7199 β”‚
β”‚          9 β”‚   4 β”‚   7152 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜
buckets after add two nodes:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (iter idx) β”‚ Key β”‚ Values β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚          0 β”‚   1 β”‚   6045 β”‚
β”‚          1 β”‚   2 β”‚   5946 β”‚
β”‚          2 β”‚   9 β”‚   5997 β”‚
β”‚          3 β”‚   8 β”‚   6137 β”‚
β”‚          4 β”‚   0 β”‚   5918 β”‚
β”‚          5 β”‚   7 β”‚   6055 β”‚
β”‚          6 β”‚   5 β”‚   6124 β”‚
β”‚          7 β”‚   3 β”‚   5875 β”‚
β”‚          8 β”‚   6 β”‚   5968 β”‚
β”‚          9 β”‚   4 β”‚   5893 β”‚
β”‚         10 β”‚  10 β”‚   6057 β”‚
β”‚         11 β”‚  11 β”‚   6139 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Jump Hash addresses the two disadvantages of ring hashes: it has no memory overhead and virtually perfect key distribution. (The standard deviation of buckets is 0.000000764%, giving a 99% confidence interval of 0.99999998 to1.00000002).

Jump Hash is also fast. The loop executes O(ln n) times, faster by a constant amount than the O(log n) binary search for Ring Hash, and made faster even still by the fact that the computation is done entirely in a few registers and doesn’t pay the overhead of cache misses.