Memcached setup and optimization for different platforms

Have you ever wondered if query caching is enough? Well it isn't because not just data query results increase the load time of a webpage but also the data processing that requires computation power and processor time. What if your application is using the same object processing with no or minor data changes. Then the fastest way to handle this is to have your objects cached in memory and here is where Memcached comes in.

web-accelerators-memcached

As described by it's creators memcached is a Free & open source, high-performance, distributed memory object caching system, Generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

So the "dumb" version of the above is that memcached stores data as it was during a previous processing usage as data chunks in memory which are being used instead of being re-computed if the same input variables are being used. Memcached works as a caching server between the processor (PHP) and the process request thread on a hit/miss system. All misses go their way and continue normal computation. A hit bypasses the processor and returns the data to the requester.

The default installation is accessible in your localhost on port 11211 and can be connected through telnet over the configured port. One of the most useful commands is the "stats" command, which provides a statistic over hits/misses, occupied memory and available memory.

I would like to provide some methods on how to install and implement memcached with some of the open-source platforms on the market:

Memcached installation

First find the appropriate rpm/deb file for your Linux distribution eg.

rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm

Afterwards install using yum or apm

 yum install memcached

Make sure you review your init.d scripts for memcached and validate the memory you would like to provide to the setup

Next step is installing the pecl extension which is used to communicate with PHP

pecl install memcache

And enable it in your active php.ini file:

extension=memcache.so

and restart apache/nginx (or any other web server you are using).

Afterwards memcached should communicate with your PHP installation but you also need to configure your platform:

Magento configuration

Magento supports memcached by default so you would only need to configure in local.xml your server details:

<cache>
   <backend>memcached</backend>
   <memcached>
    <servers>
<!-- any number of server nodes can be included -->
       <server>
          <host><![CDATA[127.0.0.1]]></host>
          <port><![CDATA[11211]]></port>
          <persistent><![CDATA[1]]></persistent>
         </server>
    </servers>
    <compression><![CDATA[0]]></compression>
    <cache_dir><![CDATA[]]></cache_dir>
    <hashed_directory_level><![CDATA[]]></hashed_directory_level>
    <hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
    <file_name_prefix><![CDATA[]]></file_name_prefix>
</memcached>
</cache>

I recommend you setup a file name prefix in case you are using the server for multiple applications so you have no data conflicts. Session data can also be stored in memcached, but I will be back with that on another article.

Drupal configuration

Although Drupal doesn't come with a default memcached module a project is available for this named Memcache API and Integration.

As with Magento Drupal also supports multiple memcached servers and they can be defined to handle separately blocks, views and other elements caching.

$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['lock_inc'] = 'sites/all/modules/memcache/memcache-lock.inc';

/* uncomment if you want stampede protection */
#$conf['memcache_stampede_protection'] = TRUE; 

$conf['cache_default_class'] = 'MemCacheDrupal';

// The 'cache_form' bin must be assigned no non-volatile storage.
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';

// Don't bootstrap the database when serving pages from the cache.
#$conf['page_cache_without_database'] = TRUE;
#$conf['page_cache_invoke_hooks'] = FALSE;

// Important to define a default cluster in both the servers
// and in the bins. This links them together.
$conf['memcache_servers'] = array('127.0.0.1:11211' => 'default');
$conf['memcache_bins'] = array('cache' => 'default',
							 'cache_page' => 'default',
							 'cache_block' => 'default');

The different bins can be linked to different servers.

There are other optimizations you can do to your memcached servers and your specific memcached configurations including for frameworks like Zend Framework 2, but I will return to you in a future article for that.

 

Image source: oscarmlage.com

One thought on “Memcached setup and optimization for different platforms

  1. Pingback: Google Speed Up Mobile Bucharest - Bodescu.me

Leave a Reply

Your email address will not be published. Required fields are marked *

*