During your daily life managing Redis clusters, you might encounter the issue where a shard in your cluster grabs the RDB file meant for single instances or other shards during startup. So the node will have data meant for other shards. For example, when scanning the cluster for a particular key, it shows up on 2 nodes
|
1 2 3 4 5 |
redis-cli -c -p 30001 --scan --pattern "515" "515" redis-cli -c -p 30003 --scan --pattern "515" "515" |
In Redis 8.6 onward (and 8.0 for Valkey), this issue will automatically resolve by the server deleting keys that do not belong to its owned slots, and you will get the following messages in the log file:
|
1 |
I have keys for slot <slot-number>, but the slot is assigned to another node. Deleting keys in the slot. |
But in older versions, the keys will not be deleted (instead, the affected slots are set to IMPORTING state), which will create operational headaches (duplicated keys still consume memory and create confusion in keyspace management). This article will list out a few ways in which you can solve the problem.
If you still have the RDB file meant for the shard, solving the issue is simple. Just shut down the instance with SHUTDOWN NOSAVE, point the configs (dir, dbfilename, and/or appenddirname if you use AOF) to the correct RDB file (or copy the RDB file to the correct directory), and then start the instance again. Problem solved!
If you did not detect the issue right away and applications have written new data into the shard, restarting the node with the correct RDB file will no longer work. Since we now have data mismatch between the two RDBs.
The good news is that your application won’t get stale data held by the wrong node. Since the cluster still has the correct data about hash slot ownership, when applications read/write to keys in the affected slot, they will still get redirected to the correct owner.
|
1 2 3 4 5 |
redis-cli -c -p 30001 r1:30001> SET 515 new-value-for-key-515 -> Redirected to slot [15879] located at r1:30003 OK |
So now we can reclaim the memory by deleting the errant keys
We can find the errant keys by listing out how many slots are affected by the wrong RDB load. Simply run redis-cli –cluster check, and we will see a warning about slots in IMPORTING state.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
redis-cli --cluster check r1:30001 r1:30001 (c6227b55...) -> 1000 keys | 5461 slots | 0 slaves. r1:30002 (5f38d97e...) -> 326 keys | 5462 slots | 0 slaves. r1:30003 (a99e6438...) -> 324 keys | 5461 slots | 0 slaves. [OK] 1660 keys in 3 masters. 0.10 keys per slot on average. >>> Performing Cluster Check (using node r1:30001) M: c6227b556fd2672da600b0bb25f471699ee02546 r1:30001 slots:[0-5460] (5461 slots) master M: 5f38d97efd63db106957dc1c3170796b3e390f17 r1:30002 slots:[5461-10922] (5462 slots) master M: a99e6438abc408db1af7da9745c07e40d469e1e6 r1:30003 slots:[10923-16383] (5461 slots) master [OK] All nodes agree about slots configuration. >>> Check for open slots... [WARNING] Node r1:30001 has slots in importing state 5462,5491,5521,5538,5547,5556,... |
After we get the affected slots, run CLUSTER GETKEYSINSLOT to find how many keys they have
|
1 2 3 4 5 |
r1:30001> CLUSTER COUNTKEYSINSLOT 5871 (integer) 1 r1:30001> CLUSTER GETKEYSINSLOT 5871 1 1) "884" |
So, in this example, slot 5871 has only one key named 884.
A normal DEL will not work in this case because the cluster still has the correct hash-slot ownership information. So simply sending DEL will get you redirected to the correct node and delete the good key.
Instead, we need to flag the connection with -ASK first, which, as stated in the documentation, will not get us a redirection
ASK means to send only the next query to the specified node.
We can test this out by GETting key 515 (which we SET a new value for earlier)
|
1 2 3 4 5 6 7 8 9 10 11 |
redis-cli -c -p 30001 r1:30001> get 515 -> Redirected to slot [15879] located at r1:30003 "new-value-for-key-515" # connect to the instance on port 30001 again redis-cli -c -p 30001 r1:30001> ASKING OK r1:30001> GET 515 "515" |
The downside to this is that since -ASK only affects the next query, we will have to set it again after completing one command
|
1 2 3 4 5 6 7 8 9 |
r1:30001> ASKING OK r1:30001> GET 515 "515" r1:30001> GET 515 -> Redirected to slot [15879] located at r1:30003 "new-value-for-key-515" |
So, you can delete the errant key by running ASKING followed by DEL command
|
1 2 3 4 5 |
r1:30001> ASKING OK r1:30001> DEL 515 (integer) 1 |
Loading the wrong RDB file into a Redis Cluster node can leave keys from other shards behind, wasting memory and causing operational headaches. While Redis 8.6 and later automatically clean up these misplaced keys, older versions require manual intervention. If the correct RDB file is still available and no new data has been written, restoring it is the simplest solution. Otherwise, you can identify the affected slots using redis-cli –cluster check, locate the misplaced keys with CLUSTER COUNTKEYSINSLOT and CLUSTER GETKEYSINSLOT, and safely remove them using ASKING followed by DEL. Most importantly, always verify that you are deleting keys from the affected node rather than their rightful owner to avoid accidental data loss.
Resources
RELATED POSTS