Read all keys & values from a Memcached instance

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, May 29, 2013

Follow me on Twitter as @mattiasgeniar

Memcached is a simply key/value store, often used as a cache to reduce load on a database system. It uses a concept of slabs and chunks to store data. Each piece of data you want to store, depending on the object size, will get stored in a different ‘slab’. A slab is a fixed in size and will store your data.

Memcached allows you to retrieve all data from within such a slab via the command line (= telnet interface).

Now, assuming your telnet is running on ‘localhost’ on default port 11211, you can access the telnet interface from the command line as such.

$ telnet localhost 11211

Now you’ve entered the Memcached CLI interface. To get started, run a stats command to show you how many slabs there are active.

$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stats slabs
STAT 1:chunk_size 96
STAT 1:chunks_per_page 10922
...
STAT 28:cas_hits 0
STAT 28:cas_badval 0
STAT active_slabs 22

Each slab will show you the usage statistics (great for monitoring each slab btw!) as well as the numeric ID for each of those slabs. Those IDs are needed to get all the data from one such slab.

For instance, to get all the data from the first slab, use the following CLI command. Note that the first digit is the numeric ID for the slab and the second digit is how many items you want to retrieve. Zero (=0) means all items.

$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stats cachedump 1 0
ITEM cache-.wildcard-locale%3A [2 b; 1367247294 s]

In thise case, in slab ID 1, there is one item found called “cache-.wildcard-locale” (this is a Drupal cache) with the value next to it.

Some slabs will contain more items than others.

$ telnet localhost 11211
...
stats cachedump 5 0
ITEM cache_field-field%3Auser%3A1 [144 b; 1367247294 s]
ITEM cache_path-admin%2Fstructure [140 b; 1367247294 s]
ITEM cache_path-admin%2Fappearance [141 b; 1367247294 s]
ITEM cache_field-field%3Anode%3A9 [145 b; 1367247294 s]
ITEM cache_field-field%3Anode%3A4 [145 b; 1367247294 s]
ITEM cache_path-node%2F35%2Fedit [131 b; 1367247294 s]
ITEM cache_path-node%2F32%2Fedit [131 b; 1367247294 s]
ITEM cache_path-node%2F30%2Fedit [131 b; 1367247294 s]

Loop over each slab ID and issue the ‘stats cachedump $ID 0’ command and you’ll see all data.

Now, if you don’t want to telnet each time, you can cheat a bit with this one-liner.

$ echo "stats cachedump 5 0" | nc localhost 11211
ITEM cache_field-field%3Auser%3A1 [144 b; 1367247294 s]
ITEM cache_path-admin%2Fstructure [140 b; 1367247294 s]
ITEM cache_path-admin%2Fappearance [141 b; 1367247294 s]
ITEM cache_field-field%3Anode%3A9 [145 b; 1367247294 s]
ITEM cache_field-field%3Anode%3A4 [145 b; 1367247294 s]
ITEM cache_path-node%2F35%2Fedit [131 b; 1367247294 s]
ITEM cache_path-node%2F32%2Fedit [131 b; 1367247294 s]
ITEM cache_path-node%2F30%2Fedit [131 b; 1367247294 s]

It just requires your swiss army knife for networking: NetCat (= nc).



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.