Find out the largest string value for keys in a Redis database

I need to look at our Redis cache and see what the size of our largest stored value is. I am familiar with Python or can use redis-cli . Is there a way to iterate over all the keys in the database so that I can check the size of each value?

SCAN seems to be a way of iterating over the keys, but I'm still working on how to use this to get the sizes of the values ​​and keep the maximum when I go.

+6
source share
3 answers

Since you mentioned redis-cli as an option, it has a build it function that does pretty much what you ask for (and much more).

redis-cli --bigkeys

 # Scanning the entire keyspace to find biggest keys as well as # average sizes per key type. You can use -i 0.1 to sleep 0.1 sec # per 100 SCAN commands (not usually needed). 

Here is an example output:

 Sampled 343799 keys in the keyspace! Total key length in bytes is 9556361 (avg len 27.80) Biggest string found '530f8dc7c7b3b:39:string:87929' has 500 bytes Biggest list found '530f8d5a17b26:9:list:11211' has 500 items Biggest set found '530f8da856e1e:75:set:65939' has 500 members Biggest hash found '530f8d619a0af:86:hash:16911' has 500 fields Biggest zset found '530f8d4a9ce31:45:zset:315' has 500 members 68559 strings with 17136672 bytes (19.94% of keys, avg size 249.96) 68986 lists with 17326343 items (20.07% of keys, avg size 251.16) 68803 sets with 17236635 members (20.01% of keys, avg size 250.52) 68622 hashs with 17272144 fields (19.96% of keys, avg size 251.70) 68829 zsets with 17241902 members (20.02% of keys, avg size 250.50) 

You can view the full output example here

+7
source

Here is a solution that uses redis built-in scripting capabilities :

 local longest local cursor = "0" repeat local ret = redis.call("scan", cursor) cursor = ret[1] for _, key in ipairs(ret[2]) do local length = redis.pcall("strlen", key) if type(length) == "number" then if longest == nil or length > longest then longest = length end end end until cursor == "0" return longest 

This should work faster than the Python code you provide, Ben Roberts, especially because the Lua script uses STRLEN over GET + Python len .

+3
source

I think I understood the main idea using the redis-py library

 import redis r= redis.StrictRedis(...) max_len = 0 for k in r.scan_iter(): try: val_len = r.strlen(k) except: continue if val_len > max_len: max_len = val_len print max_len 
0
source

Source: https://habr.com/ru/post/978035/


All Articles