0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.