How to delete keys matching a pattern in Redis Cluster

一世执手 提交于 2019-12-06 13:56:10

问题


I have tried method in this question, but it does not work since I'm working in cluster mode, and redis told me:

(error) CROSSSLOT Keys in request don't hash to the same slot


回答1:


Answers for that question try to remove multiple keys in a single DEL. However, keys matching the given pattern might NOT locate in the same slot, and Redis Cluster DOES NOT support multiple-key command if these keys don't belong to the same slot. That's why you get the error message.

In order to fix this problem, you need to DEL these keys one-by-one:

redis-cli --scan --pattern "foo*" |xargs -L 1 redis-cli del

The -L option for xargs command specifies the number of keys to delete. You need to specify this option as 1.

In order to remove all keys matching the pattern, you also need to run the above command for every master nodes in your cluster.

NOTE

  1. With this command, you have to delete these keys one-by-one, and that might be very slow. You need to consider re-designing your database, and use hash-tags to make keys matching the pattern belong to the same slot. So that you can remove these keys in a single DEL.

  2. Either SCAN or KEYS command are inefficient, especially, KEYS should not be used in production. You need to consider building an index for these keys.



来源:https://stackoverflow.com/questions/53716223/how-to-delete-keys-matching-a-pattern-in-redis-cluster

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!