/ Tools


The official doc is here


Either that's just in the command line used

[root@PROXY ~]# ps -ef | grep memcache
memcach+  2865     1  0 mai25 ?        00:27:33 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024

Or an OS may have a config file here

[root@PROXY ~]# cat /etc/sysconfig/memcached

Used by a service, here with CentOS 7 & systemd

[root@PROXY ~]# cat /usr/lib/systemd/system/memcached.service

ExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS


There's no such thing as a memcache cluster. Client may use multiples servers, reading and writing from them all, but a memcached itself doesn't synchronise other instances.


Is it running

systemctl status memcached
ps -ef | grep memcache

memcached-tool (cli)

[root@PROXY ~]# memcached-tool
Usage: memcached-tool <host[:port] | /path/to/socket> [mode]

memcached-tool display    # shows slabs
memcached-tool            # same.  (default is display)
memcached-tool stats      # shows general stats
memcached-tool dump       # dumps keys and values

Most interesting is stats, includes version, get_hits, get_misses & evictions.

A large number of get_misses may just be an indication that the cache is still being populated with information. The number should, over time, decrease in comparison to the number of cache get_hits. If, however, you have a large number of cache misses compared to cache hits after an extended period of execution, it may be an indication that the size of the cache is too small and you either need to increase the total memory size, or increase the number of the memcached instances to improve the hit ratio.

A large number of evictions from the cache, particularly in comparison to the number of items stored is a sign that your cache is too small to hold the amount of information that you regularly want to keep cached. Instead of items being retained in the cache, items are being evicted to make way for new items keeping the turnover of items in the cache high, reducing the efficiency of the cache.

Rougthly said, slabs are data chunk of a given size in which you put items or this size. That's the way data's organized in memcached.
A full explanation in this article


Here we're gonna talk directly the protocol, to get the same global info as the cli, and more in dept stuff, and command a bit memcached. Here's the doc.

You may send those commands via echo 'command' | nc ip_or_hostname port. ncis an alternative to telnet without the voodoo exit.

yum install -y nmap-ncat

First useful info is version.

[root@PROXY~]# echo \"version\" | nc localhost 11211
VERSION 1.4.15

Second is live settings.

[root@PROXY ~]# echo \"stats settings\" | nc localhost 11211
STAT maxbytes 67108864
STAT maxconns 1024
STAT tcpport 11211
STAT udpport 11211
STAT verbosity 0
STAT oldest 0
STAT evictions on
STAT domain_socket NULL
STAT umask 700
STAT growth_factor 1.25
STAT chunk_size 48
STAT num_threads 4
STAT num_threads_per_udp 4
STAT stat_key_prefix :
STAT detail_enabled no
STAT reqs_per_event 20
STAT cas_enabled yes
STAT tcp_backlog 1024
STAT binding_protocol auto-negotiate
STAT auth_enabled_sasl no
STAT item_size_max 1048576
STAT maxconns_fast no
STAT hashpower_init 0
STAT slab_reassign no
STAT slab_automove 0

Stats, like you would got with memcached-tool localhost:11211 stats

[root@PROXY ~]# echo \"stats\" | nc localhost 11211
STAT pid 2865
STAT uptime 9049040
STAT time 1473236101
STAT version 1.4.15
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 539.715691
STAT rusage_system 1114.702266
STAT curr_connections 50
STAT total_connections 35716
STAT connection_structures 1004
STAT reserved_fds 20
STAT cmd_get 28449797
STAT cmd_set 2994511
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 27889368
STAT get_misses 560429
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 2987313869
STAT bytes_written 9709355181
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 2
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT bytes 2649416
STAT curr_items 5185
STAT total_items 2994511
STAT expired_unfetched 784
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 1783

stats slabs and stats items will get you the same info but unformated as memcached-tool localhost:11211

stats sizes. Caution this command can take some times on large cache and prevent set & get while completings. First columns is an item's size, second is the number of items of that size.

stats detail on & dump & off to see what's happening.

watch fetchers & mutations & evictions. Meant for some live log, couldn't test since not available in version 1.4.15.

flush_all. Wipe'em'all.

A bit of PHP for testing

Some get started idea from another tutorial. Install a LAMP plus memcache stuff, roughlty

sudo apt-get install mysql-server php5-mysql php5 php5-memcached memcached apache

phpinfo should show memcached


Get some test data into the database

mysql -u root -p
USE mem_test;
GRANT ALL ON mem_test.* TO test@localhost IDENTIFIED BY 'testing123';
CREATE TABLE sample_data (id int, name varchar(30));
INSERT INTO sample_data VALUES (1, \"some_data\");

And here's the /var/www/test_memcache.php to use mysql & memcached

  $mem = new Memcached();
  $mem->addServer(\"\", 11211);

  mysql_connect(\"localhost\", \"test\", \"testing123\") or die(mysql_error());
  mysql_select_db(\"mem_test\") or die(mysql_error());

  $query = \"SELECT name FROM sample_data WHERE id = 1\";
  $querykey = \"KEY\" . md5($query);

  $result = $mem->get($querykey);

  if ($result) {
    print \"<p>Data was: \" . $result[0] . \"</p>\";
    print \"<p>Caching success!</p><p>Retrieved data from memcached!</p>\";
  } else {
    $result = mysql_fetch_array(mysql_query($query)) or die(mysql_error());
    $mem->set($querykey, $result, 10);
    print \"<p>Data was: \" . $result[0] . \"</p>\";
    print \"<p>Data not found in memcached.</p><p>Data retrieved from MySQL and stored in memcached for next time.</p>\";

Call http://lamp/test_memcache.php multiple times to see the caching going on.