Search code examples
redis

Does Redis rehash the keys?


Does Redis rehash the key under the hood?

i.e. do I need to hash my key before writing it to Redis?

for example:

Normally I would do: redis.put(rehash(key), value)

Is it really necessary?


Solution

  • Redis uses the CRC16 algorithm to map keys to hash slots. There are 16384 hash slots in Redis Cluster, and to compute what is the hash slot of a given key, we simply take the CRC16 of the key modulo 16384.

    There is no need for rehashing the key before writing to Redis, Redis does it for you.

    In general, a hash function maps keys to small integers (buckets). An ideal hash function maps the keys to the integers in a random-like manner, so that bucket values are evenly distributed even if there are regularities in the input data.

    CRC16 can ensure an evenly load on the nodes concerning the amount of keys i.e. a uniform distrubution.