user1619355
user1619355

Reputation: 459

Implement Consistent Hash Algorithm in Java

I am trying to implement a consistent hash based algorithm in java using the following reference for sharding keys to redis -

Stanford Theory on CH

I am trying to understand the best way to generate the hascode for a node and a key. Currently I am using the DigestUtils to generate the hash as follows & adding the returned value to the ring/circle -

private BigInteger hash(String key) {
    return new BigInteger(DigestUtils.md5Hex(key.getBytes()), 16);
}

I wanted to know if this approach sounds correct.

Upvotes: 0

Views: 2431

Answers (2)

Nikita Koksharov
Nikita Koksharov

Reputation: 10803

Redisson uses highwayhash algorithm for sharded Map and Set objects. This algorithm has better distribution characteristic.

Here is a usage example with reference implementation:

long[] KEY = {0x9e3779b97f4a7c15L, 0xf39cc0605cedc834L, 0x1082276bf3a27251L, 0xf86c6a11d0c18e95L};

byte[] data = new byte[] {1, 2, 3, 4, 5};

// getting 128 bits hashing
long[] hash128 = HighwayHash.hash128(data, 0, data.length, key);
// getting 256 bits hashing
long[] hash256 = HighwayHash.hash256(data, 0, data.length, key);

Upvotes: 1

Tague Griffith
Tague Griffith

Reputation: 4173

If you look at the source code spymemcached client for Memcache, you can see how that client implemented the Ketama consistent hashing algorithm. Focus on the following files:

While not for Redis specifically, the principles are the same.

Upvotes: 1

Related Questions