APC and Memcache with Magento

You need to understand the clear distinction between these two products to understand how to use them.

  • APC is both an OPCode Cache and Fast Backend
  • Memcache is just a Fast Backend

Using APC as an OPCode Cache

Simply install the module on your server

pecl install apc

And enable it in your php.ini

echo "extension=apc.so" >> /usr/lib/local/php.ini       (RedHat/Centos)
echo "extension=apc.so" >> /etc/php5/conf.d/20apc.ini   (Debian)

You then enable and fine-tune the runtime configuration to suit, eg.

apc.enabled
apc.shm_segments
apc.shm_size
apc.optimization
apc.num_files_hint
apc.user_entries_hint
apc.ttl
apc.user_ttl

Then restart PHP/Apache

/etc/init.d/httpd restart                               (RedHat/Centos)
/etc/init.d/apache2 restart                             (Debian)

After that, there is nothing else to do. Confirm APC is enabled with a quick phpinfo() – but otherwise, at this point, the OPCode cache portion of APC is active.

Nothing needs to be configured on Magento’s side.

Using APC as a Fast Backend

You need to add the following to your ./app/etc/local.xml

<global>
  ...
  <cache>
    <backend>apc</backend>
      <prefix>mystore_</prefix>
  </cache>
  ...
</global>

Then flush your existing store caches. To verify it is working, load a page in the front-end and the ./var/cache directory should remain empty.

Using Memcache as a Fast Backend

You’ll need to install Memcache as a PHP extension, and install the respective Memcache Daemon (Memcached) on your server.

pecl install memcache

And enable it in your php.ini

echo "extension=memcache.so" >> /usr/lib/local/php.ini            (RedHat/Centos)
echo "extension=memcache.so" >> /etc/php5/conf.d/20memcache.ini   (Debian)

/etc/init.d/httpd restart                               (RedHat/Centos)
/etc/init.d/apache2 restart                             (Debian)

Then install Memcached on the server. For RH/Centos, adjust the URL to suit your release version and CPU architecture.

rpm -Uhv http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
yum --enablerepo=rpmforge install memcached

apt-get install memcached                               (Debian)

Then modify Magento to use Memcache as a fast backend, change the socket path to a TCP/IP connection to suit.

<cache>
  <slow_backend>database</slow_backend>

  <fast_backend>memcached</fast_backend>
  <fast_backend_options>
    <servers>
      <server>
        <host>unix:///tmp/memcached.sock</host>
        <port>0</port>
        <persistent>0</persistent>
      </server>
    </servers>
  </fast_backend_options>

  <backend>memcached</backend>
  <memcached>
  <servers>
    <server>
      <host>unix:///tmp/memcached.sock</host>
      <port>0</port>
      <persistent>0</persistent>
    </server>
  </servers>
</cache>

The caveats of Memcache and tagging – what is it storing

Memcache only supports a single level of key-value relationships, so it cannot store the Magento cache tags (that are used to flush cache data independently). As a result, you either need to specify a slow_backend to maintain the cache content tag relationship, or don’t define one at all.

If you define a slow_backend, you run the risk of the cache tags growing so large that performance is negated; there is also the inherent problem that you cannot scale across multiple servers if each server is maintaining their own cache tags.

So when using Memcache, the better approach (with the caveat you cannot flush caches independently), is to not bother using the slow_backend.

In which case, we suggest removing <slow_backend>database</slow_backend> and replacing it with:

  <slow_backend>Memcached</slow_backend>
  <slow_backend_options>
    <servers>
      <server>
        <host>unix:///tmp/memcached.sock</host>
        <port>0</port>
        <persistent>0</persistent>
      </server>
    </servers>
  </slow_backend_options>

This will break/disable the 2nd level of caching (and prevent tag storage), but still allow the performance of Memcache.

Which to use

If its a single server deployment – there’s no harm just using APC for everything.

If its a distributed set-up – then you’ll need to use Memcache as the fast backend (so that all machines can access the common store).

What would we use?

Well, neither actually.

We use MageStack – which is an ultra high performance Magento Operating System that provides native operational code caching beyond that of APC (with a ~8% performance improvement), and utilises an advanced cache bac-kend that does support cache tagging.

  • Dave1992

    Hi, thanks for a great blog post. I have multiple stores out of one magento directory – i.e. one app/etc/local.xml. My stores would be store1_en, store1_de, store2_en etc. Can I just put multiple cache entries in the same local.xml and use the prefixes store1_en, store1_de etc in each entry. Somehow that feels like that’s not how this is intended to work. thanks Dave

  • sonassi

    The two are mutually exclusive. Your opcode cache has nothing to do with local.xml cache entries. Its part of PHP itself.

  • Dave1992

    Thanks, sorry I edited my post to delete the question about the differences between the two caches, as I figured it out. I’m still a bit confused about configuring backend cache for multiple stores in the same magento directory.

  • sonassi

    If all your stores are part of a single Magento installation, then you only define a single cache back-end. A good Magento host (www.sonassihosting.com) would be able to tell you this.

  • Dave1992

    OK, thanks. I read somewhere that the backend cache could get confused about which store it was serving, and the prefix entry was supposed to deal with that – if I have one cache entry I’ll have one prefix so would that still work properly with multiple stores.

  • sonassi

    Multiple *independent* installations would have that issue. Not a single Magento installation.

  • Dave1992

    Ah, OK. Thanks very much for clarifying that.

  • sonassi

    Again. Any competent Magento host could tell you this.

  • Dave1992

    I do my own Magento hosting

  • sonassi

    Then perhaps its time to consult a professional? If you can’t differentiate between 2 level caching and an opcode cache, its relevance to the operation of your store or how to configure it – I can’t help but think you would benefit from having a professional take care of the most important element of your business.

    Just my £0.02.

  • Dave1992

    I came here to learn it, that’s presumably what your knowledgebase is mainly about? Now I’ve learnt it, it’s configured and it works. I am a professional, and like many professionals I can google stuff I don’t know and learn about it. Thanks once again.