Bulk Delete key example in Redis database

Source: Internet
Author: User
Tags delete key redis system log redis server


Redis Server went away

When viewing the system log file, it is found that the error is thrown every day:

PHP Fatal Error:
Uncaught exception ' redisexception ' with message ' Redis server went away '

The script that throws the problem is a statistical script, need to read the previous day data and warehousing, initially thought to be redis read too frequently caused, but the data to the test machine after the execution of the script found that the situation does not occur, careful debugging found a manual execution when a line of code is not executed, if the execution of the line is very slow. The line code is:

$Redis->delete ($Redis->keys ($pre _key_del. ' * '));

See the manual with the appropriate tips:

KEYS are very fast, but using it in a large database can still cause performance problems if you need to get from a dataset
To find a specific key, you'd better replace it with a REDIS set structure (set).
Login Redis through info view, memory using more than 25G, and key also has 144 million ... There is a large number of keys in the Reids that are useless and have not set an expiration time. Set an expiration time, it is necessary to do something.

used_memory_human:24.72g
db0:keys=144856453,expires=25357

The implementation of the keys prefix* through the tester causes Redis to die, and other connections are not connected. So positioning to the problem appears on the keys command, also as the manual says the performance problem.

How do I remove unused key?

Most keys are regular, have a specific prefix, need to get a specific prefix key and then delete, the internet has such a command:

Redis-cli-a redis-pwd-n 0 Keys "preffix*" | Xargs redis-cli-p 6379-a redis-pwd-n 0 del

The test machine executes the keys "preffix-1*" time is probably more than 40 s, this means Redis to stop the 40s+, and the prefix is set by the day, this kind of operation needs many times, because the business reason, does not allow such operation, minute all is the money ~
Finally, the idea is to first from the test machine to meet the conditions of the key to the text, the previous statement through the cat text to get. Such as:

Redis-cli-p 6380-a redis-pwd Keys "preffix-1*" >/home/keys_redis/preffix-1

The key on the production environment is then deleted by this data.


cat/home/keys_redis/preffix-1 | Xargs redis-cli-a redis-pwd-n 0 del

Delete the speed is very fast, memory consumption is also very fast, feel like how much consumption. After execution, the number of keys has been reduced 95%+, and memory has dropped from 25G to 2G. But one index raised the ———— Mem_fragmentation_ratio, before and after the memory contrast:

# Memory processing before
used_memory:26839186032
used_memory_human:25.00g
used_memory_rss:23518339072
used_ memory_peak:26963439000
used_memory_peak_human:25.11g
used_memory_lua:31744
mem_fragmentation_ ratio:0.88
mem_allocator:jemalloc-3.2.0
# Memory after processing
used_memory:2399386704
Used_memory_human : 2.23G
used_memory_rss:4621533184
used_memory_peak:26963439000
used_memory_peak_human:25.11g
used_memory_lua:31744
mem_fragmentation_ratio:1.93
mem_allocator:jemalloc-3.2.0

Mem_fragmentation_ratio problems may also need to be optimized, from the Redis This problem can be seen, set cache when we also need to take into account the cache maintenance problem, whether this set cache expiration time, key naming way how to manage, You can't just cram the data in.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.