Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple script to print the size of all your Redis keys.
#!/usr/bin/env bash
# This script prints out all of your Redis keys and their size in a human readable format
# Copyright 2013 Brent O'Connor
# License: http://www.apache.org/licenses/LICENSE-2.0
human_size() {
awk -v sum="$1" ' BEGIN {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "1kb"; } '
}
redis_cmd='redis-cli'
# get keys and sizes
for k in `$redis_cmd keys "*"`; do key_size_bytes=`$redis_cmd debug object $k | perl -wpe 's/^.+serializedlength:([\d]+).+$/$1/g'`; size_key_list="$size_key_list$key_size_bytes $k\n"; done
# sort the list
sorted_key_list=`echo -e "$size_key_list" | sort -n`
# print out the list with human readable sizes
echo -e "$sorted_key_list" | while read l; do
if [[ -n "$l" ]]; then
size=`echo $l | perl -wpe 's/^(\d+).+/$1/g'`; hsize=`human_size "$size"`; key=`echo $l | perl -wpe 's/^\d+(.+)/$1/g'`; printf "%-10s%s\n" "$hsize" "$key";
fi
done
@votintsev

This comment has been minimized.

Copy link

votintsev commented Jun 22, 2013

Nice work! But, how select DB? Can you help?

@hufman

This comment has been minimized.

Copy link

hufman commented Jun 25, 2013

To select the db, change the redis_cmd to include the -n # option.

@fluxsauce

This comment has been minimized.

Copy link

fluxsauce commented Aug 16, 2013

Thanks, this was a nice shortcut when I was troubleshooting some odd performance problems!

@Arsenalist

This comment has been minimized.

Copy link

Arsenalist commented Dec 22, 2013

Exactly what I was looking for. To make this perfect, sum up the total size of the keys.

Thanks again.

@aalfson

This comment has been minimized.

Copy link

aalfson commented Aug 19, 2014

Awesome work!

@merkerxu

This comment has been minimized.

Copy link

merkerxu commented Oct 30, 2014

Thanks. It's very helpful!

@mistidoi

This comment has been minimized.

Copy link

mistidoi commented Dec 1, 2014

This saved me some real time today. Thanks for your good work!

@ghariharan

This comment has been minimized.

Copy link

ghariharan commented Mar 4, 2015

But will the 'serializedlength' include the index size(and all other relavant stuffs) redis maintains for that key?? I need the entire memory size consumed by redis for a single key..

@davidwebber

This comment has been minimized.

Copy link

davidwebber commented May 4, 2015

Nice! Thank you.

@moses-gangipogu

This comment has been minimized.

Copy link

moses-gangipogu commented Jun 3, 2015

0.00 Gb ERR unknown command 'debug'

redis-cli -h amazonaws.redis.hostname DEBUG OBJECT
(error) ERR unknown command 'DEBUG'

any idea on this error ?

@snicky

This comment has been minimized.

Copy link

snicky commented Sep 22, 2015

If you use an external Redis hosting you might have the DEBUG command disabled. For example, this is a list of disabled commands on one of popular Redis providers: CONFIG, SHUTDOWN, BGREWRITEAOF, BGSAVE, SAVE, DEBUG, and KEYS

@emacsist

This comment has been minimized.

Copy link

emacsist commented Dec 24, 2015

It's too slow for large keys....

@sakovias

This comment has been minimized.

Copy link

sakovias commented Mar 9, 2016

Nice work! So there's no way to profile on AWS ElasticCache since DEBUG is disabled?

@pboling

This comment has been minimized.

Copy link

pboling commented Jul 14, 2016

@sakovias - Seems correct. http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/ClientConfig.RestrictedCommands.html

Only option for AWS ElastiCache is to use a tool that does not rely on debug, perhaps: https://github.com/antirez/redis-sampler

@houchen

This comment has been minimized.

Copy link

houchen commented Oct 9, 2016

This work for me. Awesome and niubible!!!

@creativeHats

This comment has been minimized.

Copy link

creativeHats commented Jul 24, 2017

How to use with authentication?

@slenart

This comment has been minimized.

Copy link

slenart commented Nov 2, 2017

guys, you have info command with total memory usage. iterating all keys with ineffective keys * is not a good approach.

@rndbblnn

This comment has been minimized.

Copy link

rndbblnn commented Jan 9, 2018

FYI: the script doesn't work if you're using Redis on AWS Elasticache, 'redis-cli debug' command not supported.

@guoruibiao

This comment has been minimized.

Copy link

guoruibiao commented Jul 17, 2019

guys, you have info command with total memory usage. iterating all keys with ineffective keys * is not a good approach.

that's really a bad idea by using this script on product environment. Maybe using scan command to get the keys by application, and then apply this script can get a better efficientily.

@andrewbrg

This comment has been minimized.

Copy link

andrewbrg commented Oct 30, 2019

guys, you have info command with total memory usage. iterating all keys with ineffective keys * is not a good approach.

that's really a bad idea by using this script on product environment. Maybe using scan command to get the keys by application, and then apply this script can get a better efficientily.

Agree

@navi2589

This comment has been minimized.

Copy link

navi2589 commented Dec 4, 2019

what does that -e does in echo?

@epicserve

This comment has been minimized.

Copy link
Owner Author

epicserve commented Dec 4, 2019

@navi2589, Try running the following and see what it does.

echo -e "Blah\nFoo\n\nBar"

@sg552

This comment has been minimized.

Copy link

sg552 commented Jan 14, 2020

it doesn't work on ubuntu 18.

Just calculate the size by your brain, the serializedsize unit is 'byte', so 1024 = 1k, 1024000 = 1MB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.