DEV Community

Discussion on: How to Use Consistent Hashing in a System Design Interview?

Collapse
 
maximgatilin profile image
Maxim

Thanks for the article! I have one question - you mentioned that md5 is used to transform key into hash and in your example key is transformed into number in 0-100 range. But how? E.g “CA” string is transformed into “ 3e8d115eb4b32b9e9479f387dbe14ee1” hash in reality, not to 79 like in your example

Collapse
 
arslan_ah profile image
Arslan Ahmad

That's a good question.

We have to map the hash to the available key range; which can be done using the module operator. For example, if the consistent hash ring has a key range of a 32-bit integer, we can calculate the actual position on the ring like:

md5-hash("CA") modulo 2^32 => gives us position on the ring

The object is then assigned to the next server that appears on the ring in clockwise order.

Alternately, we can also map the hash to a key range representing degrees of a complete circle i.e., 0-360 to find an object's place on the ring:

md5-hash("CA") modulo 360

Hope I was able to answer your question.

Collapse
 
maximgatilin profile image
Maxim

Got it, thanks for the answer, I didn’t know we can convert hash into integer