43

I keep hearing about some PHP (opcode) caches like - APC, XCache, Memcache, eAccelerator, etc.

But I couldn't ever figure out how to go about choosing one. Apart from performance benefit, which a caching system is supposed to deliver, which other factors should be a point of concern.

Like why you will say X cache system is better than Y? I am less worried about relative performance gain. Small differences between any two systems matter less.

If a generic answer to my question is not possible, here are few pointers. I use dedicated VPS with Mediatemple (with root access). RAM is 512 MB (physical) + 400MB (swap) I am concerned about WordPress and its cousins WordPress-MU and BuddyPress. 90% of our codes/sites fall into WordPress family.

Thanks in advance for some help.

rahul286
  • 1,647
  • 4
  • 20
  • 25

6 Answers6

32

The products you list serve different purposes.

OPCode caches

There are many PHP Accelerators (OPCaches) as seen on this Wikipedia list. As is common with open source products, they are all fairly similar. XCache is the lighttp PHP accelerator, and is the default choice when you are running that HTTPd. It works well with Apache as well, however APC seems to be slightly more "plays well with others" socially speaking, being officially supported as part of PHP, and is released in-step with the official PHP distribution.

I abandoned usign eAccelerator due to its slowing development, and lagging against the releases of PHP, and the official blessed status APC offers with similar performance.

These products typically are drop in; no code change instant performance boost. With large codebases (Drupal, Wordpress) the performance can be up to 3x better while lowering response time and memory usage.

Data Caching

Memcache is a slightly different product -- you might think of it as a lightweight key value system that can be scaled to multiple servers. Software has to be enhanced to support Memcache, and it solves certain problems better than others. If you had a list of realtime stock values on your website, you might use Memcache to keep a resident list of the current value that is displayed accross your website. You might use it to store session data for short term reuse. You wouldn't use it for other things such as full-page caches, or as a replacement for MySQL.

There are also Wordpress addons such as WP-Super-Cache that can drastically improve Wordpress' performance (infact, WP-Super-Cache can rival static HTML based sites in many cases)

In summary -- I would highly recommend APC if you want a "set it and forget it, well supported product".

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
SirStan
  • 2,373
  • 15
  • 19
  • This answer is being discussed on Meta: http://meta.stackexchange.com/questions/15474/bug-entire-section-of-text-turning-into-link – Brad Gilbert Aug 19 '09 at 16:19
  • Fill in "`[4]: http://`" to fix it. – Brad Gilbert Aug 19 '09 at 16:26
  • Thanks a ton SirStan! Your answer not only solved my problem but also helped me get more insight into caching world. I just want to ask you one more thing. I am already using wp-supercache. Is it good idea to combine it with APC? Will APC further improve performance significantly? Will APC and Wp-SuperCache will work together? Do I need them both? Or APC will make Wp-SuperCache redundant? – rahul286 Aug 21 '09 at 05:33
  • Note: APC does both data caching & op-code caching. – lo_fye Aug 24 '09 at 14:14
  • 2
    This answer is now very dated. APC is unlikely to be updated for future versions of PHP having been replaced by Zend's optimizer and opcode cache (now known as opcache) since v. 5.5. However I've not seen a substantial difference in performance ( http://symcbean.blogspot.co.uk/2013/09/zend-optimizer-plus-still-not-following.html ) further the lack of data support in opcache and lack of memory reclaims may make upgrading counter productive for some. – symcbean Sep 26 '15 at 23:37
8

A good answer was posted on stackoverflow which answers your question nicely.

https://stackoverflow.com/questions/28716/which-php-opcode-cacher-should-i-use-to-improve-performance

The Pixel Developer
  • 847
  • 3
  • 9
  • 20
6

Just to note that things has changed a little bit and it seems that APC will not be included in PHP 6 core.

APC has slow development and it looks like it will never be PHP 5.5 compatible. Because of that, looks like guys from PHP will be setting Zend OPCache opcode cache extension as the PHP CORE extension. You care read more here http://wiki.php.net/rfc/optimizerplus.

Important note: Zend OPCache does not have user data cache like APC, so if you need user data cache you can you use it together with Memcache.

Timur Bakeyev
  • 347
  • 3
  • 7
Nemke
  • 248
  • 5
  • 9
  • 1
    It also invalidates the entire cache when it gets full (invalid entries are NOT removed) - hence if you've got more code than memory or deploy frequently, then expect to see performance spikes. – symcbean Nov 05 '13 at 23:30
6

APC will be built into PHP6, so it's a logical choice. I use it, and the performance boost is amazing. If you need to cache something other than opcodes (i.e. db query results), you can use APC for that, too, but it's not possible to share APC caches between multiple servers. If you only need to cache on a single server, APC is great. If you need to scale out to multiple servers, and want to share a cache between them, memcached is your man.

One thing I would do, though, is create a wrapper class for any (non-opcode) caching you do. That way you can swap out the caching engine without changing your code.

lo_fye
  • 193
  • 5
  • WOW. Had I known APC's official status, I would have switched to it long term back... :-) I also didnt know much about opcode/non-opcode cache difference. As a personal choice, I do not want to go through overhead of creating wrapper classes or modifying source of my apps to help them adapt with caching env. – rahul286 Aug 21 '09 at 05:30
  • 6
    Note that, since PHP 5.5 integrated Zend Opcache as "official" opcode cache, I think it is unlikely they'll switch to APC for PHP 6. – Matteo Tassinari Aug 26 '13 at 06:45
  • 1
    To correct this: PHP 5.5 indeed ships with Zend Opcache. There will be no PHP 6. There will be a PHP 7 instead. APC is to be considered almost (?) obsolete. – Jisse Reitsma Sep 03 '15 at 13:19
3

If running PHP version at least 5.50, OpCache is your best bet (PHP / PECL native library). It should come pre-compiled if installing from binary.

http://php.net/manual/en/book.opcache.php

If running PHP version prior to 5.5, APC (PHP / PECL native OpCode cache) would be the simplest choice, although it's considered unmaintained and dead:

http://php.net/manual/en/book.apc.php

Using the native OpCache functionality of PHP should save you the trouble of maintaining 3rd party libraries.

recurse
  • 131
  • 2
  • 1
    If running PHP < 5.5, the very first thing you should be doing is updating it. – Michael Hampton Jan 16 '17 at 22:07
  • @michael-hampton :: Indeed! But in some rare cases you can't update PHP without upgrading the OS, which may even mean a new hardware requirement. Example - I work for a company running IBM i-series servers, version 6somethings. Our PHP version is 5.4.3. Since IBM relies on Zend for porting PHP versions over, PHP 5.6 requires a newer version of the IBM-i OS - 7.1 or newer (and subsequently a newer version of the Zend PHP server), which requires new Power-8 servers and so on ... meh. – recurse Jan 16 '17 at 23:18
0

Memcache caches key/value pairs, not opcodes. You can use it conjunction with one of the opcode caches.

user10699
  • 61
  • 1
  • 1
  • 2
  • It seems from SirStan's answer (below) that memcache may need me to modify my PHP apps. Actually I am using Wordpress and modifying its core code won't be a good idea. – rahul286 Aug 21 '09 at 05:27