development icon

Using the command line to get memcache statistics

Steven Merrill, Director of Devops
#Development | Posted

There are GUI tools that show the performance and statistics of your memcache servers. However, if you just want a quick answer to some simple questions, you can use basic commands and save the time of installing and configuring the GUI tools.

To get started, you can use telnet to connect to your memcache server. ( where 11211 in this example is the port number )

telnet localhost 11211

If you are using Drupal, the IP and ports of your memcache instances should be listed in the $conf array in your settings.php file.

'memcache_servers' => array(

  'localhost:11211' => 'default',

  'localhost:11212' => 'menu',



'memcache_bins' => array(

  'cache' => 'default',

  'cache_menu' => 'menu',



Once you have connected to the server, there are a few commands that you can run to get quick information on the status of your server.

Use stats to determine if your memcache server is receiving traffic. The output will include lines such as:

STAT cmd_get 13889276

STAT cmd_set 3627007

STAT get_hits 13771861

STAT get_misses 117415

Check the statistics before and after a page refresh, and you should see the numbers increase. If they are not increasing, you may have a configuration problem.

To understand the next commands, you need a quick overview of how memcache stores its data. Memcache contains a number of ‘slabs’, each of which holds key/value pairs that are within a particular size range. Each slab class can have a number of 1MB ‘pages’, which are themselves separated into ‘chunks’.

When a key/value pair gets inserted, memcache finds the appropriate slab based on the size of the value, and then finds the next empty chunk in memory for that slab and stores the data.

If you’re interested in viewing detailed information on the usage of your memcache memory, or to find a specific key, you can use one of the following commands:

stats items shows information on the number of items in each slab, including the number of times that items have been purged before they have expired or items have failed to be inserted because the slab was out of memory.

stats items

STAT items:1:number 2

STAT items:1:age 526034

STAT items:1:evicted 0

STAT items:1:outofmemory 0

stats slabs shows more detailed statistics on each slab, such as the amount of pages that have been allocated, and the percentage of available chunks that are being used.

stats slabs

STAT 5:chunk_size 240

STAT 5:chunks_per_page 4369

STAT 5:total_pages 1

STAT 5:total_chunks 4369

STAT 5:used_chunks 7

STAT 5:free_chunks 1

STAT 5:free_chunks_end 4361

If you’re interested in viewing the individual keys that are stored on the server, you can use the stats cachedump command to get a list of the keys in each slab. The command takes two arguments: the slab id and the number of items to retrieve ( 0 = retrieve all ).

stats cachedump 6 3

ITEM cache_menu-links%3Asecondary-links [160 b; 1275506090 s]

ITEM cache_menu-links%3Aprimary-links [159 b; 1275506090 s]

ITEM cache_menu-links%3Anavigation [158 b; 1275506090 s]

GUI tools to view your memcache statistics can be very useful, but if you don’t want to set them up and just need to occasionally get information on the status of your servers, the command-line options easily let you accomplish that.

Steven Merrill

Director of Devops