Rehashing existing keys into different slots in Redis

729 views
Skip to first unread message

shuva.jyo...@gmail.com

unread,
Oct 9, 2018, 1:04:14 AM10/9/18
to Redis DB
Hello devs/users

I am a newbie with Redis. I have a situation in which all the keys have been hashed to the same slot. Is there any way that i could 
rename the keys and rehash them again, so as to distribute them across slots.

I know i could use rename to rename keys but i guess that would still keep the slot-assignment unchanged.

thanks in advance

Shuva

Юрий Соколов

unread,
Oct 9, 2018, 1:15:08 AM10/9/18
to redi...@googlegroups.com
 Is there any way ...  to distribute them across slots ... still keep the slot-assignment unchanged.

It seems to me you have answered yourself.

вт, 9 окт. 2018 г., 8:04 <shuva.jyo...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

hva...@gmail.com

unread,
Oct 9, 2018, 3:35:14 AM10/9/18
to Redis DB
Are you using the sharded Redis Cluster or the non-sharded Redis?

If you're using Redis Cluster, then it sounds like you're looking for a way to control whether your keys are kept together on the same master/slaves or spread among the masters/slaves.  One way in the Redis documentation is this:  https://redis.io/topics/cluster-spec#keys-hash-tags

Shuva Kar

unread,
Oct 9, 2018, 6:13:36 AM10/9/18
to redi...@googlegroups.com, hva...@gmail.com
I am using sharded Redis Cluster.  All my keys are currently being hashed to the same slot. If i do rename the keys, will they be re-hashed to different slots ?
Br,shuva


Raunak Bhansali

unread,
Oct 9, 2018, 7:01:49 AM10/9/18
to redi...@googlegroups.com, hva...@gmail.com
Hashes are always consistent but you can play around with selecting key pattern to use to generate the hash.
There are two ways : 
1. Default -> The entire key is used to generate a hash using the hashing algo.
2. Parantheses based hashing - > Redis looks for pattern inside '{}' braces and only the selected string pattern is used to generate the hash. ex. {123}name, {123}email, {124}name in this case 1st and second will be mapped to the same hash slot where as 124 will be mapped to different.

Theres another level where slots are assigned to the nodes in master slave pattern. 
There are commands by which you can manually reassign the slots to the nodes.

But afterall same keys will be assigned to the same slot. (as number of hashes and hashing algo is fixed in the redis build)
--
Raunak Bhansali

Software Developer
email-sig-logo.png

Shuva Kar

unread,
Oct 9, 2018, 7:57:17 AM10/9/18
to redi...@googlegroups.com, hva...@gmail.com
Correct Raunak,
But if i decide to rename {123}name and {123}email to 123-name and 123-email, will they still be hashed to the old slot or a new one ?

Br,shuva

Raunak Bhansali

unread,
Oct 9, 2018, 9:11:43 AM10/9/18
to redi...@googlegroups.com
New one. Because the hash key is changing. 

Sripathi Krishnan

unread,
Oct 9, 2018, 11:10:00 PM10/9/18
to redi...@googlegroups.com
As far as I know, rename command will not work. Rename is a multi-key command, and Redis cluster expects all multi key commands to belong to the same hash slot. In other words, you can use rename only if the old and new key belong to the same hash slot - defeating the purpose. 

Instead you can use the dump and restore commands in a loop. Run a for loop from 0 through 16383. For each slot, call cluster getkeysinslot. Then dump each key, and call restore with the modified key name. 

If you use cluster getkeysinslot, you get the advantage of running dump command in a pipeline. You could simplify and just scan the entire keyspace as well. 
Reply all
Reply to author
Forward
0 new messages