user3787401
user3787401

Reputation: 1

how to find a particular redis key memory size in lua script

redis.call('select','14')

local allKeys = redis.call('keys','orgId#1:logs:email:uid#*')

for i = 1 , #allKeys ,1
do
    local object11 = redis.call('DEBUG OBJECT',allKeys[i])
    print("kk",object11[1])

end

Here "DEBUG OBJECT" is run successfully on redis-cli, but if we want to run through lua script on multiple key. That send error like this.

(error) ERR Error running script (call to f_b003d960240545d9540ebc2319d863221045 3815): Wrong number of args calling Redis command From Lua script

Upvotes: 0

Views: 955

Answers (1)

antirez
antirez

Reputation: 18514

DEBUG OBJECT is not a good bet. It shows the serialized length of the value, so it is just the size of the object once stored on an RDB file.

To have some hint about the size of an object in Redis, you need to resort to more complex techniques, but you can only get an approximation. You need to run:

  1. TYPE
  2. OBJECT ENCODING
  3. The object-type specific command to get its length.
  4. Sample a few elements to understand the average string length of the object.

Based on this four informations, you need to check the Redis source code to check the different memory footprints of the internal structures used, and do the math. Not easy...

A much more viable approximation is to just to use:

APPROX_USED_MEM = num_elements * avg_size * overhead_factor

You may want to pick an overhead factor which makes sense for a variety of data types. The error is big, but is an approximation good enough for some use cases. Maybe overhead_factor may be something like 2.

TLDR: What you are trying to do is complex and leads to errors. In the future the idea is to provide a MEMORY command which is able to do this.

Upvotes: 2

Related Questions