Skip to content

Instantly share code, notes, and snippets.

@kokizzu
Last active December 29, 2023 22:08
Show Gist options
  • Save kokizzu/ec099abc6037a380e0d3ba7c518b2f3d to your computer and use it in GitHub Desktop.
Save kokizzu/ec099abc6037a380e0d3ba7c518b2f3d to your computer and use it in GitHub Desktop.
Count and Sum size per keys prefix of Redis instance
/*
How to use:
# 1. install bun
curl -fsSL https://bun.sh/install | bash
# 2. bun install ioredis
# 3. customize the connection string below
# 4. run this script
bun size_prefix.js prefixToScan
*/
import Redis from 'ioredis'
const redisClient = new Redis({
port: 6379, // Redis port
host: "127.0.0.1", // Redis host
//username: "default", // needs Redis >= 6
//password: "xxyyzz",
//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, iter/cursor: ${iterations}/${nextCursor}`);
iterations++;
}
return { totalKeys, totalBytes };
}
console.log('scanning prefix', Bun.argv[2])
let x = await calculateKeysSize(redisClient, Bun.argv[2]+"*");
console.log(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment