How to install and monitor memcache for php on Ubuntu Linux

Memcache is one of the most popular distributed memory based cache system. It can give excellent performance results if configured correctly. Its a good idea to monitor memcache server from memory usage and cache hits/misses perspective. Here are the some steps to install, configure and monitor memcached server:

  1. To install memcache for php on Ubuntu Linux run these commands:
    sudo apt-get install memcached
    sudo apt-get install php5-memcache
  2. The default memory cap is 64m. It can be changed by editing /etc/memcached.conf as shown below:
    # Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
    # Note that the daemon will grow to this size, but does not start out holding this much
    # memory
    -m 64

    It may be better to put all you data in memory (if possible) and you may want to calculate the cap accordingly. E.g. for a wordpress site with 1000 blogs and 20K bytes page size per blog, a 20M memory will be able to hold all the posts. To take care of other pages (category, tags) and overheads, 40MB size should be good initial estimate.

  3. Install memcache.php stats script somewhere on your server. Copy etc/config.php to etc/config.local.php and change the username and password in config.local.php so that you can access it from the web interface with desired auth.
    define('ADMIN_USERNAME','memcache'); // Admin Username
    define('ADMIN_PASSWORD','SOME_PASSWORD'); // Admin Password
  4. Access http://yoursite.com/memcache.php (assuming it is installed at root location). Here is how the host stats looks like:
    memcache-host-cache-usage-hits-misses-statsYou may want to monitor cache usage. If there is no free space left, then there may be high cache churn and it may be bad for performance. Another thing to monitor is hits and misses. Its a good idea to have a high hits ratio. I think it should usually be higher than 80% unless the traffic on the site is very low. First visit on a page results in cache miss and subsequent visits results in cache hit. So for higher traffic the hit ratio is usually high assuming enough memory to hold all pages.
  5. You can also install libmemcached-tools (command line tools). Here is the install command for Ubuntu Linux:
    sudo apt-get install libmemcached-tools

    To dump a list of keys

    memcdump --servers=localhost

    To dump one specific key (ore more separated by space)

    memccat --servers=localhost w3tc_key_infoheap.com_0_pgcache_6c2156b977a576b022f2792b76c973a6_0
  6. To restart memcached (this will clear all cache also):
    sudo service memcached restart
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments