Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the data/value size in Redis keys with prefix?

I stored a lot of stuff in Redis. One group of them are with namespace cache (key starts with cache:). I want to know the size of the data/values with namespace cache. Can I achieve this in Redis? Any suggestions?

like image 904
Lusha Li Avatar asked Dec 14 '25 14:12

Lusha Li


2 Answers

nodejs snippet using ioredis:

const Redis = require("ioredis");
let redisClient = new Redis({
  port: 6379, // Redis port
  host: "127.0.0.1", // Redis host
  username: "default", // needs Redis >= 6
  password: "my-top-secret",
  db: 0, // Defaults to 0
});
async function calculateKeysSize(redisClient, matchPattern) {
  let iterations = 0;
  let totalKeys = 0;
  let totalBytes = 0;
  let nextCursor, currCursorKeys;
  while (nextCursor !== "0") {
    [nextCursor, currCursorKeys] = await redisClient.scan(nextCursor || 0, "match", matchPattern);

    totalKeys += currCursorKeys.length;

    const pipeline = redisClient.pipeline();
    for (const currKey of currCursorKeys) {
      pipeline.memory("usage", currKey);
    }
    const responses = await pipeline.exec();
    const sizes = responses.map((response) => response[1]);
    totalBytes += sizes.reduce((a, b) => a + b, 0);

    if (iterations % 1000 == 0) {
      console.log(`scanned ${totalKeys} so far.. total size: ${totalBytes} Bytes`);
    }
    iterations++;
  }

  return { totalKeys, totalBytes };
}

await calculateKeysSize(redisClient, "cache:*");

gist link for ready-to-use script.

like image 175
Liran Brimer Avatar answered Dec 16 '25 02:12

Liran Brimer


You can do this with RedisGears (https://oss.redislabs.com/redisgears/) with a single line:

RG.PYEXECUTE "GB().map(lambda x: int(execute('MEMORY', 'USAGE', x['key']))).aggregate(0, lambda a,x: a+x, lambda a,x: a+x).run('cache:*')"

The first map operation get the size of each key and the aggregate operation sums it. the argument to the run function is the keys prefix to run on.

like image 39
Meir Shpilraien Avatar answered Dec 16 '25 03:12

Meir Shpilraien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!