Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out largest string value size for keys in Redis database

Tags:

python

redis

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

It looks like SCAN is the way to iterate through the keys, but I'm still working out how to put this to use to get the sizes of the values and store a maximum as I go.

like image 337
B Robster Avatar asked Nov 11 '14 17:11

B Robster


2 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 a sample of the summery 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 a complete output example here

like image 182
Ofir Luzon Avatar answered Sep 22 '22 02:09

Ofir Luzon


Here is a solution which uses redis's 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 run faster than the Python code you provide, Ben Roberts, particularly because the Lua script uses STRLEN over GET + Python's len.

like image 40
Tim Cooper Avatar answered Sep 25 '22 02:09

Tim Cooper