Cache like a Pro with Memcached

Posted by on Feb 16, 2013 in Environment, PHP Programming | No Comments

Every application have a bottle neck. It might be a database query, request to an external service, hard drive I/O or an expensive algorithm. Even if the software is running smoothly it might be necessary to optimise in order to handle higher traffic. Significant part of optimisation is caching.

Generally speaking cache can be stored on a hard drive or in RAM. Reading data from RAM is far much faster then from a hard drive. On the other hand hard drive has greater capacity. Usually there is no need to cache terabytes of data therefore 1 to 4GB of RAM should be more than enough.

Memcached is high-performance, distributed memory object caching system. It’s well established and respected software used by the biggest web services. It has very clear API and is super easy in use. The best way to access Memcached from PHP is through the extension.

If you would like to give a try to Memcached the first step would be installing the service with the PHP extension. You will probably notice there is other extension with a very similar name “memcache”. Don’t get confused. It’s similar but not the same.

If you would like to compile the extension you also need memcached headers.

The extension source expect headers to be in “libmemcached-1.0″ directory. You might have to create a symlink.

Once everything is in place you might want to review memcached configuration.

It’s very straight forward file with only few options.

The most important setting is memory allocation “-m”. Default value is only 64MB. When changing the value make sure to leave enough memory for operating system. If you fail to do it your OS will start witting to SWAP instead of RAM which will effectively slow everything down.

Writing and reading from the cache is very simple.

First added object “foo” will expire in 60 seconds. Second object “bar” has no expiration attached so will persists until service restart. Memcached can also release the object if it runs out of memory.

If you remember memcached was described as “distributed system”. It means that you can easily extend available memory by attaching more nodes. Lets assume you need another 64MB. You can run another memcached on a different server. The only change you have to do in your software is adding another server to the pool.

memcached-usage
That will enable 128MB caching space for your application.

Another cool thing about using memcached with PHP is it can store sessions.
By default PHP sessions are written to a local hard drive. It’s slow and problematic when scaling.

Memcached can be the central point for storing all sessions from all web host behind a load balancer. To achieve that add two lines into php.ini file.


Caching with memcached is easy and very useful. I strongly recommend to give it a try. Be concision about security. Memcached doesn’t have any access control. It means that if port is exposed to the Internet anybody can connect and read/change data.

Leave a Reply