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.
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 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:
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
starting with Redis 3.4: