Read all keys & values from a Memcached instance

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

Update for modern Memcached: the stats cachedump command stopped working as of Memcached 1.5.0 (2017) and was effectively retired – it could only ever dump one page (~1MB) per slab and held locks while doing so. On any current version, use lru_crawler metadump all instead. It walks the entire keyspace without those limits and without blocking, so there’s no need to loop over slab IDs anymore.

$ echo "lru_crawler metadump all" | nc localhost 11211
key=cache_path-admin%2Fstructure exp=1367247294 la=1367240094 cas=12 fetch=yes cls=5 size=140
key=cache_field-field%3Anode%3A9 exp=1367247294 la=1367240094 cas=13 fetch=no cls=5 size=145
...
END

Note that metadump returns only the keys and their metadata (expiry, last-access, CAS, size), not the values – the idea is that you fetch the values you care about separately with a regular get.