Tech Blog

Finally you can see what’s stored in your Memcached

 

Memcached is great and everyone loves it. It’s relatively simple to install and configure and then everything runs much faster. But what if you need to debug it? Can you hook into your Memcached and see what is currently stored in it ? The answer is NO. Memcached is completely blind to its content. Moreover, if you want to see which web/app server writes what object to which Memcached node, there is no simple way to do it. You have to select between the following nontrivial alternatives:

  • Install and operate Wireshark (or another sniffing tool) on each of your Memcached nodes or web/app servers and then somehow try to sync between all the recorded files. This is a frustrating task; moreover, when you activate the sniffer your application performance is significantly degraded so you may not be able to emulate your real use case.
  • Somehow debug your app code, which again is very complex in a distributed application environment. You need to track each of your web/app servers separately and understand “who writes what” to each of your Memcached nodes.

We are happy to announce that all this is no longer necessary. The new release of our Memcached Cloud Service supports two simple commands that change the way devops debug and analyze their Memcached datasets, as explained below:

  1. keys

Format keys pattern Description

  • keys returns all keys matching a pattern, similar to Redis’ KEYS command
  • keys supports glob-style pattern matching, e.g.:
    • h?llo matches hello, hallo and hxllo
    • h*llo matches hllo and heeeello
    • h[ae]llo matches hello and hallo, but not hillo
  • keys is available via telnet.
Return value
List of keys matching the pattern; Each response line starts with the keyword “KEY” followed by the length of the key name (number of bytes) and the key name itself; this continues until the server finished with the “END” response line.
Examples

~# telnet <my_bucket_url>
Trying <my_bucket_url>…
Connected to <my_bucket_url>.
Escape character is ‘^]’.

>set one 0 0 1
1
>STORED
>set two 0 0 1
2
>STORED
>set three 0 0 1
3
>STORED
>set four 0 0 1
4
>STORED
>keys *
KEY 4 four
KEY 5 three
KEY 3 one
KEY 3 two
END

>keys *o*
KEY 3 one
KEY 4 four
KEY 3 two
END

>keys t????
KEY 5 three
END

Warning
The keys command is intended for debugging and for special operations such as changing your keyspace layout. Don’t use keys in your regular application code. In production environment and especially with large datasets, keys should be used with extreme care as it may degrade your application performance.

  1. monitor

Format
monitor
Description

monitor is a debugging command that streams back all commands processed by your Memcached bucket, similar to the Redis’ MONITOR command. It helps understanding your Memcached dataset and spot bugs in an application. The fact that only one DNS is used to access your resources in our Memcached Cloud service significantly reduces the complexity associated with understanding “who does what” in your environment. Furthermore, although there are some performance implications while running the monitor command, you can still monitor over 10K req/sec with less than 1msec latency, which is good enough for most applications in a debug mode. The monitor command is available via telnet. Manually issue a Memcached quit command via telnet to stop the monitor stream.

Example

~# telnet <my_bucket_url>
Trying <my_bucket_url>…
Connected to <my_bucket_url>.
Escape character is ‘^]’.

monitor +OK
1342367056.942187 “set” “memtier-644743” “0” “0” “6”
“xb6vxa5^x19x99”
1342367056.942739 “get” “memtier-5058846”
1342367056.943247 “set” “memtier-6371238” “0” “0” “6”
“xb6vxa5^x19x99”
1342367056.943740 “get” “memtier-6864786”
1342367056.944258 “set” “memtier-7431748” “0” “0” “6”
“xb6vxa5^x19x99”
1342367056.944747 “get” “memtier-5485182”
1342367056.945353 “set” “memtier-798822” “0” “0” “6”
“xb6vxa5^x19x99”
1342367056.946004 “get” “memtier-2654281”
1342367056.946638 “set” “memtier-1493414” “0” “0” “6”
“xb6vxa5^x19x99”

Note Each line in the monitor output has the following format:

<unix timstamp> <command name> <key> <flags> <exptime> [<bytes>] [<value>].

Where:

  • unix timestamp is provided in microsecond granularity
  • bytes and value are presented in case of Memcached storage command.
    bytes might be zero in which case it’s followed by an empty data block

Want to try it? Register and create your free Memcached bucket in our Memcached Cloud service.

Subscribe

Get the latest Blog posts by email.