Amir Popovich
Amir Popovich

Reputation: 29846

How can I find the size of a redis topic (subset of keys)?

My redis keys are ordered using all kind of topics (e.g. Topic:SubTopic:Key).

I would like to find the byte size of a section Topic:* or a Topic:SubTopic:*.

Beneath a topic I can have all kinds of entries (lists, strings, hashes, sets etc.).

Now I know the cli has the memory usage <key> command, but I'd like to somehow call this on the redis server using lua.

My approach was to run something like:

eval "local keys = redis.call('keys',KEYS[1]) ; local sum=0 ; for _,k in ipairs(keys) do sum = sum + redis.call('memory usage', k) end ; return sum" 1 Topic:SubTopic:*

I know that this command has a performance impact, and therefore I'm going to run this offline just to analyze our data structures and not on production.

My main problem is that I couldn't find a redis server command to find the memory usage of a key that is similar to the cli command.

Questions:

  1. Is there a better approach to analyze my keys? Using bigkeys isn't enough.

  2. If not, is there any equivalent memory usage command that I can run as part of a lua script to get the byte size of a key?

Upvotes: 2

Views: 390

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 50122

You can call MEMORY USAGE to obtain the byte size including overheads of a given key and value.

Note: even if running offline, you should use SCAN instead of KEYS in your Lua script - this will reduce the script's runtime memory consumption and prevent the sandbox's stack from overflowing.

Upvotes: 1

Related Questions