在不影响性能的情况下,怎么快速批量删除redis数据?
黄舟
黄舟 2017-04-21 11:15:52
0
4
735

业务场景:
redis数据切换到Oracle,取消持久化,redis只做缓存

具体需求:
从redis从库里获得了400w个无过期时间的hashkey,需要在主库中将其删除

矛盾点:
1.如果直接批量删除会导致redis拥塞,影响正常业务
2.如果每删除一个key,sleep50ms,不会影响业务,但是根据经验要跑两天

最终问题:
在不影响性能的情况下,怎么快速批量删除redis数据?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(4)
Peter_Zhu

Redis deletes keys very quickly, even 4 million keys are not a problem. I suspect the bottleneck is actually the network.

If you delete keys one by one, each time you send a command, the client will wait for a reply from redis, which wastes a lot of network bandwidth.

You can try using pipelining/transactions. Without saturating the network bandwidth on the redis side, the command is sent at the maximum speed and then executed at once.

If this doesn’t work, you can write a Lua script to identify and delete useless keys, and send them to redis for execution with eval. This should not block the network.

If this still affects the business. . The ultimate solution is to create a master/slave, delete the key on the slave, and use the master to handle the request. After deleting the key, promote the slave to master, and then transfer the request to it.

迷茫

Try the eval command of redis.

For example, delete all KEYs starting with old-fashioned:

eval "redis.call('del', unpack(redis.call('keys','old-fashioned:*')))" 0

If a single deletion consumes a lot of performance, you can consider deleting in batches.

伊谢尔伦

In my humble opinion, you can delete it at 2 o'clock in the middle of the night. The longest lag time is probably 1 minute. There should not be many people using it at this time, unless it is a 24-hour traffic website like Baidu Taobao! No need to thank me, my name is Lei Feng

巴扎黑
$ redis-cli --scan --pattern 'user:*' | xargs -L 1000 redis-cli del

starting with Redis 3.4:

$ redis-cli --scan --pattern 'user:*' | xargs -L 1000 redis-cli unlink
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!