166

I read this article: http://www.mysqlperformanceblog.com/2006/09/27/apc-or-memcached/ from way back when.. I want to get the best caching engine available so that my application is really fast. Of course I don't want to over-cache but I want to at least choose the best thing out there. In that article it says Memcached is slow and apc is fast so why is everyone choosing memcached?

http://framework.zend.com/manual/en/zend.cache.backends.html#zend.cache.backends.twolevels here is says "use a fast one (but limited) like Apc, Memcache... and a "slow" one like File, Sqlite..." do you think using Apc as the fast and Memcache as the slow is a good idea?

5
  • 15
    BTW: you should use APC even if you don't use apc_store. Opcode cache alone makes PHP few times faster.
    – Kornel
    Commented Oct 14, 2010 at 9:52
  • @porneL: what do you mean? Does APC increase PHP speed after just the installation, even if I don't add code to cache anything in my scripts? Commented Apr 2, 2012 at 11:30
  • 5
    @Marco Demaio: Indeed. APC is mainly a PHP optimizer (optimizes by default as long as it's enabled), and apc_store() is just an extra bonus on top of that.
    – Kornel
    Commented Apr 2, 2012 at 21:16
  • 3
    in 2014, if you only use it for caching on 1 server, go for APCu.
    – Flion
    Commented May 8, 2014 at 15:33
  • Also stackoverflow.com/q/930877/632951
    – Pacerier
    Commented Jan 19, 2015 at 10:19

7 Answers 7

275

Memcached is a distributed caching system, whereas APC is non-distributed - and mainly an opcode cache.

If (and only if) you have a web application which has to live on different webservers (loadbalancing), you have to use memcache for distributed caching. If not, just stick to APC and its cache.

You should always use an opcode cache, which APC is (also APC will get integrated into php6 iirc, so why not start using it now).

You can/should use both for different purposes.

6
  • 6
    Stunning answer +1. My question now is what's an opcode cache? Commented Apr 2, 2012 at 11:20
  • 4
    @Karsten If you have different servers (like a cloud loadbalaced environment) as long as you don't need to maintain/share state across them, APC will also work fine as a straight up memory cache--though maybe not as optimal as each cloud instance will have to warm their own cache...
    – Ray
    Commented Jul 19, 2012 at 14:47
  • 36
    An opcode cache is one that compiles the plain PHP code into machine code ("opcode") and then stores it in the compiled form for future requests (until it detects the original PHP file has changed). This means that PHP doesn't have to run this compile step on every single request, saving some time. It's just one of the ways to help ensure a speedy site (amongst many others).
    – Simon E.
    Commented Oct 2, 2012 at 23:44
  • 2
    There's an interesting slideshow that explains some of the differences with diagrams and benchmarks, here: slideshare.net/FordAntiTrust/php-performance-with-apc-memcached
    – Simon E.
    Commented Oct 2, 2012 at 23:56
  • 1
    Can memechached be used in non-distributed system instead of APC? It will be good to keep only one if in the future you will have more machines. right? Or use them both, one for opcode and the other for extensible caching?
    – Adib Aroui
    Commented Jul 25, 2013 at 23:29
44

Memcached if you need to preserve state across several web servers (if you're load balanced and it's important that what's in the cache is the same for all servers).

APC if you just need access to quick memory to read (& write) on a (or each) server.

Remember APC can also compile and speed up your script execution time. So you could for example be using APC for increased execution performance, while using memcached for cache storage.

2
  • 3
    APC can cache storage too.... Or?
    – mr_app
    Commented Jul 19, 2013 at 15:37
  • 7
    2014 update: PHP 5.5 will nativly include Zend Optimizer Plus (which is only for opcache, so not for user cache) and it seems APC will not be developed beyond PHP 5.4? However there is now APCu pecl.php.net/package/APCu, which took only the user cache parts of APC
    – Flion
    Commented May 8, 2014 at 15:28
20

The main advatage of APC is opcode cache. Since PHP 5.5 integrated OpCache to its core and APC for PHP 5.4 is still flagged as beta, it's not official annoucement, but the development of APC would be dropped in near future.

So I would recommend you to choose Memcached.

1
  • 5
    I have spent hours on the web to think about it, and I have the same conclusion. Commented Dec 29, 2014 at 8:33
9

I use both one for speed and the other to sync all my servers. If you do use memcache then please do keep in mind of the open ports which you will need to block with iptables.

6

Hey Thomaschaaf, I hope this is not tool late for you but please note that APC has some issues related to "user-cache". To make a long story short, when you set time-outs for cache entries, or if your apache crashes inside internal APC code (timeout, for example), then you may suffer some problems.

I have an entry about the issue here: http://nirlevy.blogspot.com/2009/06/apc-futexwait-lockdown-make-your-apache.html, and you should also read http://t3.dotgnu.info/blog/php/user-cache-timebomb.html (from one of the APC developers i think)

1
1

I use only APC since APC is a code cache and acts like memcache ! Only 1 config file instead of 2.

And only 1 place to monitor both cache.....

1

It depends on what you are doing but for my drupal websites running on a VPS I find APC works great! If you are running CentOS 6 it is available as a yum update so dead simple to install and no config as the defaults are reasonable. A no brainer imho.

Not the answer you're looking for? Browse other questions tagged or ask your own question.