October 20, 2009

Memcached: List all keys

Posted in Software at 17:50 by graham

In the general case, there is no way to list all the keys that a memcached instance is storing. You can, however, list something like the first 1Meg of keys, which is usually enough during development. Here’s how:

Telnet to your server:

telnet 127.0.0.1 11211

List the items, to get the slab ids:

stats items
STAT items:3:number 1
STAT items:3:age 498
STAT items:22:number 1
STAT items:22:age 498
END

The first number after ‘items’ is the slab id. Request a cache dump for each slab id, with a limit for the max number of keys to dump:

stats cachedump 3 100
ITEM views.decorators.cache.cache_header..cc7d9 [6 b; 1256056128 s]
END

stats cachedump 22 100
ITEM views.decorators.cache.cache_page..8427e [7736 b; 1256056128 s]
END

Thanks to Boris Partensky in the Memcached group here

There you go!

22 Comments »

  1. Memcached used as a server side cache for full pages of a Bottle web app said,

    August 13, 2014 at 08:29

    […] Memcached: List all keys via Telnet, 2009-10-20 […]

  2. Azmath Mohamad said,

    May 8, 2014 at 06:49

    Thanks for the command, it helped my cache investigation.

  3. Mr Nice said,

    October 21, 2013 at 12:19

    Thanks for sharing this. Helped find a few bugs in our site’s caching last week.

  4. P K Ghosh said,

    May 28, 2013 at 14:10

    When we dump keys from memcached, it always returns with expiration time in descending order. Is there any command/way so that we can get older items first?

  5. Azmath Mohamad said,

    May 28, 2013 at 10:07

    Thanks, this is exactly i was looking for.

  6. memcached – List keys in PHP said,

    January 18, 2013 at 08:06

    […] http://www.darkcoding.net/software/memcached-list-all-keys/ […]

  7. Harrie said,

    December 26, 2012 at 12:29

    @Graham: thx a lot. That was exactly what I needed.

    @Daniel:

    Your regular expression for parsing the slab id fits on slab ids up to 9 only “/STAT items:([\d]):/”

    Should be: “/STAT items:(\d+):/”

  8. Greg K said,

    February 6, 2012 at 21:40

    That Python code to get Memcache stats works in iPython, on an iPhone, if you delete the references to argv and hardcode the ip / port!

    :)

  9. vikas said,

    December 6, 2011 at 10:35

    b stands for value byte size and s is the time value got set. Its in epoch time.

  10. Billy Kimble said,

    November 14, 2011 at 21:17

    Thanks for this write up. As some others have done, I’ve created a Ruby script to telnet locally and list all of the keys. You can check it out at:

    https://gist.github.com/1365005

  11. Dumping Memcache Keys – The Developer Day said,

    September 1, 2011 at 22:27

    […] first I found a way to dump memcache keys through telnet. However if a memcache instance is fairly large and has a lot of slabs and thousands of keys it […]

  12. MCBrowse – a Java Memcache Browser with Ruby Parsing | Tom Russell – A Ruby/Rails Developer said,

    May 22, 2011 at 11:40

    […] Java developer, so some of the code is likely to be horrific :) Thanks go to Graham King for his explanation on his blog of how to access all keys from […]

  13. Daniel said,

    May 5, 2011 at 12:07

    Here’s a PHP version I created:

    http://snipt.org/xtP

  14. MCBrowse – a Java Memcache Browser with Ruby Parsing « Tom Russell – A Ruby/Rails Developer said,

    May 2, 2011 at 23:32

    […] Java developer, so some of the code is likely to be horrific Thanks go to Graham King for his explanation on his blog of how to access all keys from […]

  15. PlaidCTF Django Challenge writeup (Web 300) | Just 5 Minutes said,

    April 25, 2011 at 07:43

    […] it took me some time to find a special trick to dump all keys  . And even better, Daniel Rust has made a python library called memcached_stats for feature and […]

  16. Tom Russell said,

    February 13, 2011 at 15:05

    Hi Graham,

    Thanks for the info on your post, I used it to create a nice (albeit probably quite buggy) Java-based memcache browser.

    http://www.trdev.co.uk/index.php?/archives/5-MCBrowse-a-Java-Memcache-Browser-with-Ruby-Parsing.html

  17. MCBrowse – a Java Memcache Browser with Ruby Parsing – Tom Russell – A Ruby/Rails developer said,

    February 13, 2011 at 14:51

    […] much a beginning Java developer, so it's likely to be horrific Thanks go to Graham King for his explanation on his blog of how to access all keys from memcache. The code is available on github here, released under MIT […]

  18. Daniel Rust said,

    February 6, 2011 at 02:59

    Thanks for the post, inspired me to write an python interface to easily view a list of current keys. You can check it out here: https://github.com/dlrust/python-memcached-stats

    This can be run from any system with python installed (which includes OSX)

    @Daniel, I’ve just used python-memcached-stats, very useful, thanks! – Graham

  19. Prateek said,

    November 15, 2010 at 07:47

    Hi All,

    Can we list all the keys stored in MemCached through java? Please help.

  20. Draco Yang said,

    October 19, 2010 at 08:05

    s should be the expired unixtime in seconds, if you need to get the exact time, you can use the following java code to parse it as a java date object: Date expiredTime = new Date(s * 1000);

  21. Goktug Ozturk said,

    July 29, 2010 at 07:43

    Most probably, b is for bytes and s means unixtime of last cache

  22. Osman Sebati Cam said,

    May 25, 2010 at 09:47

    Hi Graham,

    This is very nice post,

    do you know the meaining of ‘b’ after 7736 and the meaning of ‘s’ after 1256056128

    b for byte ?

    or b for the byte size of key and s for the byte size of data

    ITEM views.decorators.cache.cache_page..8427e [7736 b; 1256056128 s]

    if you know post will be nicer :)

Leave a Comment

Note: Your comment will only appear on the site once I approve it manually. This can take a day or two. Thanks for taking the time to comment.