Open SourceGetting Started with Memcached Distributed Memory Caching

Getting Started with Memcached Distributed Memory Caching

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Wikipedia describes Memcached as a general-purpose distributed memory caching system, but what exactly does the term Memcached mean? Cache is memory used to store the most frequently used resources (e.g. browsers store every website visited during a session in cache), because accessing resources from a cache is faster than accessing them from a disk drive. So Memcached means “memorycached,” which simply is caching resources in the memory. These resources can be data retrieved from API calls, database operations or even HTML pages. The data is stored in key/value pairs in the form of large hash tables.

As distributed system is part of the Memcached definition, you can install Memcached on various servers to make a larger caching server. In this way, Memcached helps reduce database loads to a minimum, resulting in faster and more responsive Web applications. Figure 1 shows how Memcached works when used with database.


Figure 1. How Memcached Works:
Here is a diagram of how Memcached works when used with database.

How Does Memcached Work?

Figure 1 will be familiar to anyone who has ever written a script that interacts with a database. Here is the step-by-step explanation of how it works when you want to pull certain data from the database:

  1. Check whether the desired data exists in the cache. If it is in the cache records, then just retrieve the data and hence there is no need to query the database.
  2. If the data that you are looking for is not in the cache, then query the database. Return the required data to the script and store the information in the cache.
  3. Keep the cache fresh. Whenever the data is changed (i.e. altered or delete for some reason), update this information into the cache. That way, when the cache is queried for the old data then it should either redirect to the database or give the updated information.

Apparently, Memcached is best implemented for queries that are triggered multiple times in a second and demand huge data as output. Access to Memcached data is faster than the access time to disk drives because the Memcached data is stored in temporary memory.

How to Install Memcached?

You will find Memcached preinstalled on almost any production server. But if you are trying Memcached for the first time, the first step is to install the Memcached extension on your server. First, check whether Memcached is already installed on your system using this command:



memcached -h

If Memcached is installed, the command will output the current version. Otherwise, it will return an error.

The next step is to install Memcached. The following command installs Memcached on a CentOS Linux distribution.



yum install memcached

It will search the packages and install the latest version of Memcached if not install already.

When — and When Not — to Use Memcached

When should you use Memcached and when should you avoid it? By now you might have figured that Memcached was designed to shed the load from the database. But you should implement this cache system with a strategy to ease more expensive queries as well. You can also write a log that records the query execution time, which will then help you to analyze performance.

For instance, suppose you operate an e-commerce site. You can cache a query that fetches the description, shipping options and availability of different products by firing a complex query (involving joins, etc.). So every time the product page is loaded the database query is skipped and the result is fetched from cache. Certainly, caching a query like this will improve the overall performance of the webpage. You would just need to update the product details when they change.

Sometimes caching your queries is not a good idea, however. This may be the case when a database operation involves more update queries than fetch queries. So, every time the database is updated, you need to update the cache as well, which causes the overall performance of the Web application to drop. Hence, querying the database becomes a better solution than using a cache solution.

Memcached and Security

If you follow how Memcached works, you might have noticed that the data that gets stored in the cache resides in the memory and the memory does not have any access-level control over it. But if your data is not crucial, you should not worry much about security. However, you can secure your cache data in the following ways if you need to:

  1. Choosing unique keys: Because data stored in Memcached can be taken as a large associative array, you should use unique keys. The only way you can access data is by keys and there is no other way to query Memcached. This limitation can be used to your advantage by choosing unique keys that are hard to guess. You can name the keys using some unpredictable convention.
  2. Securing Memcached server: A server that has Memcached installed and is queried remotely should be behind a firewall. You can define an access-level control in the server, which allows access to only authorized machines.
  3. Encode your data: You can encode your data/key or even both before storing them in the cache. That involves an extra CPU cycle and is sometimes expensive in the case of larger data, but you still can give it a try and check the performance.
    1. Memcached Code in Action

      Let’s see some Memcached code in action and learn how to make it work using the PHP code snippet below. It is a class that shows how to retrieve data from the cache. If that data does not exist, then it queries the database and stores it to cache for subsequent usage.

      <?phpclass MyCache{  private $cache;  function  __construct()  {    $this->cache = new Memcache();    // you can replace localhost by Memcached server IP addr and port no.    $this->cache->connect('localhost', 10987);  }  function get_data($key)  {    $data = $this->cache->get($key);    if($data != null)      return $data;    else    {      if($this->cache->getResultCode() == Memcached::RES_NOTFOUND)      {        //do the databse query here and fetch data        $this->cache->set($key,$data_returned_from_database);      }      else      {        error_log('No data for key '.$key);      }    }  }}$cache = MyCache();$cache->get_data('foo');?>

      To use this code in your development, make sure you have installed the Memcached PHP extension. In the latest version of PHP the extension is bundled with the PECL and PEAR packages.

      Summary

      Memcached is a powerful tool that various Web applications such as Wikipedia, Flickr and Digg all use. If using Memcached can dramatically increase the performance of Web applications like these, what are you waiting for?

      About the Author

      Sachin Khosla is a Web developer, open source technology evangelist and writer. Read his blog at digimantra.com.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories