Read all keys & values from a Memcached instance

Mattias Geniar, Wednesday, May 29, 2013 - last modified: Monday, August 17, 2015

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).



Hi! My name is Mattias Geniar. I'm a Support Manager at Nucleus Hosting in Belgium, a general web geek, public speaker and podcaster. Currently working on DNS Spy. Follow me on Twitter as @mattiasgeniar.

I respect your privacy and you won't get spam. Ever.
Just a weekly newsletter about Linux and open source.

SysCast podcast

In the SysCast podcast I talk about Linux & open source projects, interview sysadmins or developers and discuss web-related technologies. A show by and for geeks!

cron.weekly newsletter

A weekly newsletter - delivered every Sunday - for Linux sysadmins and open source users. It helps keeps you informed about open source projects, Linux guides & tutorials and the latest news.

Share this post

Did you like this post? Will you help me share it on social media? Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *