Memcached VS Apc Which One Should I Choose

Difference between Memcache, APC, XCache and other alternatives I've not heard of

First, a list of opcode cachers for php.

Second Memcache/MemcacheD is not an Opcode Cacher. It is a distributed memory caching system. It does not improve the speed/performance of your PHP code. It can be used to store data only.

APC, EAccelerator, XCache and the others are non distributed, meaning you can only store data on the local web-server. However all of these are opcode cachers and can improve the performance of your PHP app. Most, excluding EAccelerator (in the current version) can also store data.

I generally choose APC for the opcode cacher (It reportedly will be included into the core of PHP 6). However if I also have more than one web-server for the site I will also make use of MemcacheD.


Edit 1 I agree it is very annoying to setup APC, Memcache on MAMP. There are however tutorials out there dealing with such.


Edit 2 Also with regards to the best Opcode Cacher for your app really depends on which server you are using. Some work better on some systems. It also depends on the size and scale of your app as to how the cachers perform.


Edit 3 Very interesting article here about comparing performance of a few different cachers. (This article appears to be written in 2006 and should not really be used for current reference)

What should I use for caching?

Although both APC and Memcache are used for different purpose, you should use both. Since you have multiple servers, memcache will help in caching and maintaining user data state across servers and APC will help in speed up script execution time.

APC compiles the plain PHP code into machine code and saves it so in all future requests, compilation time can be saved. Here's the link which can give you some idea on how to use it with Yii - http://www.yiiframework.com/wiki/312/getting-the-most-out-of-apc-for-yii/

apc vs eaccelerator vs xcache

APC is going to be included in PHP 6, and I'd guess it has been chosen for good reason :)

It's fairly easy to install and certainly speeds things up.

Using memcached/APC for session storage?

Generally, session data is something which should be treated as volatile in any situation. The user can always choose to eliminate the cookie themselves at any point (if you are using cookies, of course). For this reason, I see no problem with using memcached for session data.

For me, I'd just keep it simple - no need for a DB fallback unless you absolutely must never lose the user's session in the event of a memcached server failure. As I said at the beginning, I always treat sessions as purely volatile in any case and don't really store anything of any significance in them.

That's my two cents anyways.

Selecting an appropriate cache mechanism

I'm using the same kind of caching mecanism for some projects ; and we are using APC + memcached as caching systems.

There are two/three main differences between APC and memcached, when it comes to caching data :

  • APC access is a bit faster (something like 5 times faster than memcached, if I remember correctly), as it's only local -- i.e. no network involved.
  • Using APC, your cache is duplicated on each server ; using memcached, there is no duplication accross servers

    • whic means that memcached ensures that all servers have the same version of the data ; while data stored in APC can be different on each server


We generally use :

  • APC for data that has to be accessed very often, is quick to generate, and :

    • either is not modified often
    • or it doesn't matter if it's not identical on all servers
  • memcached for data that takes more time to generate, and/or is less used.

    • Or for data for which modifications must be visible immediatly (i.e. when there is a write to the DB, the cached entry is regenerated too)

For instance, we could :

  • Use APC to store configuration variables :

    • Don't change often
    • Are accessed very often
    • Are small
  • Use memcached for content of articles (for a CMS application, for example) :

    • Not that small, and there are a lot of them, which means it might need more memory than we have on one server alone
    • Pretty hard/heavy to generate


A couple of sidenotes :

  • If several servers try to write to the same file that's shared via NFS, there can be problems, as there is no locking mecanism on NFS (as far as I remember)
  • APC can be used to cache data, yes -- but the most important reason to use it is it's opcode caching feature (can save a large amount of CPU on the PHP servers)
  • Entries in memcached are limited in size : you cannot store an entry that's bigger than 1M (I've sometimes run into that problem -- rarely, but it's not good when it happens ^^ )

Memcache vs APC for a single server site data caching

A quick Googling says that APC is 5 times faster than Memcached.

My experience say that APC is nearly 7-8 times faster than Memcached.. but, memchached can be accessed by different services (for example, if you run mainly on apache and delegates some traffic, e.g. static contents like images or pure html, to another web-service, like lighttpd), that can be really usefull, if not indispensable.

APC have less feature than memcached and is easly to use and optimize, but this depends on your needs.

HipHop vs APC vs Other or php optcode caching

Personally, I'd stick with APC.

HipHop is really only for a certain kind of optimization and, from what I've ready, is a pain in the ass to even get your code working with. It doesn't just seamlessly take what you have and convert it. It's my understanding that there's some PHP that can't be used with it.

Full alternative to APC user data cache for PHP?

If anyone is wondering what I have ended up doing - I use XCache now. It's surely not perfect - mainly because of its very weak (if any at all) documentation. But it's stable, maintained, and works as expected so it is pretty much good replacement of APC now.



Related Topics



Leave a reply



Submit