Memcache vs PHP Memcache

or Memcache vs. Memcached

It seems that PHP has two memcached libraries named memcache and memcached. What is the difference and how do you know which one to use? The newer memcached PHP extension was created recently by some guys at Digg. It has some new features and performance enhancements.

Here is a quick backgrounder in naming conventions (for those unfamiliar), which explains the frustration by the question asker: For many *nix applications, the piece that does the backend work is called a “daemon” (think “service” in Windows-land), while the interface or client application is what you use to control or access the daemon. The daemon is most often named the same as the client, with the letter “d” appended to it. For example “imap” would be a client that connects to the “imapd” daemon.

This naming convention is clearly being adhered to by memcache when you read the introduction to the memcache module (notice the distinction between memcache and memcached in this excerpt):

Memcache module provides handy procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.

The Memcache module also provides a session handler (memcache).

More information about memcached can be found at
http://www.danga.com/memcached/.

The frustration here is caused by the author of the PHP extension which was badly named memcached, since it shares the same name as the actual daemon called memcached. Notice also that in the introduction to memcached (the php module), it makes mention of libmemcached, which is the shared library (or API) that is used by the module to access the memcached daemon:

memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

This extension uses libmemcached library to provide API for communicating with memcached servers. It also provides a session handler (memcached).

Information about libmemcached can be found at
http://tangent.org/552/libmemcached.html.

In summary, both are functionally the same, but they simply have different authors, and the one is simply named more appropriately than the other.

The most important features that memcached has are:

  • Cas tokens. Whenever you pull something from the cache, you can receive with it a cas token (a double number). You can than use that token to save your updated object. If no one else updated the value while your thread was running, the swap will succeed. Otherwise a newer cas token was created and you are forced to reload the data and save it again with the new token.
  • Read through callbacks are the best thing since sliced bread. It has simplified much of my code.
  • getDelayed() is a nice feature that can reduce the time your script has to wait for the results to come back from the server.
  • While the memcached server is supposed to be very stable, it is not the fastest. You can use binary protocol instead of ASCII with the newer client.
  • Whenever you save complex data into memcached the client used to always do serialization of the value (which is slow), but now with memcached client you have the option of using igbinary. So far I haven’t had the chance to test how much of a performance gain this can be.

    Create a temporary folder
    1. As it is a manually work, you might want to clear the source file after the installation, so create a folder as command below.

    mkdir ~/memcache

    Install LibEvent
    To install memcache, you will need LibEvent, go to their site to get the latest version.


    cd ~/memcache
    wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
    tar zxvf libevent-1.4.14b-stable.tar.gz
    cd libevent-1.4.14b-stable
    ./configure
    make
    make install

    Install Memcache
    Now, install the memcache and get the latest version from their site.

    cd ~/memcache
    wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
    tar zxvf memcached-1.4.5.tar.gz
    cd memcached-1.4.5
    ./configure --with-lib-event=/usr/local/
    make
    make install

    Install PHP Memcache
    Now, install PECL Memcache and get the latest version.

    cd ~/memcache
    wget http://pecl.php.net/get/memcache-2.2.6.tgz
    tar zxvf memcache-2.2.6.tgz
    cd memcache-2.2.6
    phpize
    ./configure
    make
    make install
    vi /usr/local/lib/php.ini

    Find the extension as below and if it is not existed, add it in the php.ini.

    extension=memcache.so

    Restart the Apache services.

    service httpd restart

    A good comparison of the 2 versions is here:
    http://code.google.com/p/memcached/wiki/PHPClientComparison

  • g33kadmin

    I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....