# Can anyone \*really\* explain opcode caching with PHP?

**URL:** <https://www.ruby-forum.com/t/can-anyone-really-explain-opcode-caching-with-php/217637>\
**Category:** NGINX\
**Created:** [March 7, 2012, 4:13am UTC](https://www.ruby-forum.com/t/can-anyone-really-explain-opcode-caching-with-php/217637 "2012-03-07T04:13:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 7, 2012, 4:13am UTC](https://www.ruby-forum.com/t/can-anyone-really-explain-opcode-caching-with-php/217637/1 "2012-03-07T04:13:56Z")

</div>

Hi folks

Thinking of abandoning mod\_php for the wonderful world of nginx, but  
googling the issue of opcode caching with fastcgi is proving  
surprisingly confusing: there’s a lot of conflicting information out  
there.

My basic question is this: what is the most performant,  
memory-efficient, Slashdot resistant way to set up nginx for running a  
complex PHP app?

More specifically:

1. Why cache? I understood that a fastcgi process compiles the app into  
memory, and serves it from there till it’s recycled after max\_requests.  
So what’s the value of opcode caching? To speed up one request in 500?  
But I’ve seen benchmarks that claim an opcode cache with nginx offers  
50% savings on memory and response time. Where would these savings be  
coming from? No one explains…

2. What cache? Nginx/PHP setup tutorials either omit opcode caching or  
recommend xcache, without explaining why. There’s a lot of conflicting  
information about whether each process needs its own cache (very memory  
intensive?) or whether processes can share a cache. Some bloggers claim  
that Xcache handles this better than APC, but again, no one explains how  
or why. Can anyone offer an authoritative answer to this?

So is opcode caching effective with fastcgi? If so, how does it work,  
and what setup plays best with nginx? I would be more than grateful if  
anyone can make sense of this. Also, can I please suggest this as a  
candidate for the nginx FAQs? Judging by what I’ve been reading, I’m not  
the only one who’se confused by this…

Posted at Nginx Forum:

> **[Can anyone \*really\* explain opcode caching with PHP?](https://forum.nginx.org/read.php?2,223556,223556#msg-223556)**
>
> Hi folks Thinking of abandoning mod\_php for the wonderful world of nginx, but googling the issue of opcode caching with fastcgi is proving surprisingly confusing: there's a lot of conflicting information out there. My basic question is this: what is...

---

<div class="post-metadata">

**Author:** ![gcaplan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8baadc/32.png) [@gcaplan](https://www.ruby-forum.com/u/gcaplan)\
**Post date:** [March 7, 2012, 10:47am UTC](https://www.ruby-forum.com/t/can-anyone-really-explain-opcode-caching-with-php/217637/2 "2012-03-07T10:47:18Z")

</div>

> Hi folks
> 
> Thinking of abandoning mod\_php for the wonderful world of nginx, but  
> googling the issue of opcode caching with fastcgi is proving  
> surprisingly confusing: there’s a lot of conflicting information out  
> there.
> 
> My basic question is this: what is the most performant,  
> memory-efficient, Slashdot resistant way to set up nginx for running a  
> complex PHP app?

You have to take in account one fundamental point:

mod\_php is often see as “easy” because it get the apache’s adaptive  
process spawning. That means you have not to take in account how much  
processes to dedicate to php requests. Apache will spawn processes on  
demand.

With nginx you have a proxy talking to a set of processes (via various  
protocols). The default php fastcgi server does not have adaptive  
process  
spawning, so you have to preallocate them, carefully choosing the  
number.

This often smashes even experienced apache “sysadmin”, used to not care  
that aspect. (i remember one guy working on one of fortune 500 company  
blaming nginx on that list). I still have customer (experienced php  
developers) that still not get the “multiprocess” concept.

I answer your questions below

> More specifically:
> 
> 1. Why cache? I understood that a fastcgi process compiles the app into  
> memory, and serves it from there till it’s recycled after max\_requests.  
> So what’s the value of opcode caching? To speed up one request in 500?  
> But I’ve seen benchmarks that claim an opcode cache with nginx offers  
> 50% savings on memory and response time. Where would these savings be  
> coming from? No one explains…

fastcgi is only a communication protocol from nginx to your app (the php  
server). I am not a big fan of opcode caching, but i can assure you that  
it works in php fastcgi server, php-fpm and uWSGI. It really works  
whenever your php processes are not recycled (that is why it will never  
works on CGI mode)

> anyone can make sense of this. Also, can I please suggest this as a  
> candidate for the nginx FAQs? Judging by what I’ve been reading, I’m not  
> the only one who’se confused by this…

again, caching fully works whenever you do not recycle your processes,  
so  
you will not have problems with that.

I only suggest you to choose a more advanced server, like php-fpm or  
uWSGI  
(even if it is very new in the php world),  
as the old-style embedded php fastcgi-server has very few features  
(read:  
none)

–  
Roberto De Ioris

> **[unbit - Overview](https://github.com/unbit)**
>
> makers of uWSGI and many more open source projects - unbit

---

<div class="post-metadata">

**Author:** ![gcaplan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8baadc/32.png) [@gcaplan](https://www.ruby-forum.com/u/gcaplan)\
**Post date:** [March 7, 2012, 11:11am UTC](https://www.ruby-forum.com/t/can-anyone-really-explain-opcode-caching-with-php/217637/3 "2012-03-07T11:11:01Z")

</div>

Am Wed, 7 Mar 2012 10:46:23 +0100  
schrieb “Roberto De Ioris” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I only suggest you to choose a more advanced server, like php-fpm or  
> uWSGI (even if it is very new in the php world),  
> as the old-style embedded php fastcgi-server has very few features  
> (read: none)

Additionally, one has to take into account that opcode-caching and the  
like that zend, apc etc. do only usually gives you a couple of percents  
of increased performance.

When you get “slashdotted”, that is basically useless.

What you have to do is aggressive output-caching via varnish or nginx  
(we haven’t done this, yet, but I assume it’s possible since some time,  
too).

That will give you much (x-times) more performance than an opcode-cache  
will ever be able to.

Rainer

---

<div class="post-metadata">

**Author:** ![gcaplan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8baadc/32.png) [@gcaplan](https://www.ruby-forum.com/u/gcaplan)\
**Post date:** [March 8, 2012, 4:14am UTC](https://www.ruby-forum.com/t/can-anyone-really-explain-opcode-caching-with-php/217637/4 "2012-03-08T04:14:11Z")

</div>

Hi Volodymyr

Thanks for your helpful response. From what you say, opcode caching is  
essential and Xcache is the way to go.

But just to understand - does a php-fpm pool access a single Xcache, or  
is there a separate cache set up for each process? I’m trying to get a  
handle on the memory implications.

Thanks for the heads up on uWSGI. But my sysadmin skills are basic (I’m  
a developer really) and it looks a bit scary. There’s not much info out  
there, while fastcgi is supported in the php core. For someone who is  
not a proper sysadmin, is uWSGI, in its present state of development,  
really a sensible way to go?

Posted at Nginx Forum:

> **[Re: Can anyone \*really\* explain opcode caching with PHP?](https://forum.nginx.org/read.php?2,223556,223565#msg-223565)**
>
> Hi folks Thinking of abandoning mod\_php for the wonderful world of nginx, but googling the issue of opcode caching with fastcgi is proving surprisingly confusing: there's a lot of conflicting information out there. My basic question is this: what is...

---

<div class="post-metadata">

**Author:** ![gcaplan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8baadc/32.png) [@gcaplan](https://www.ruby-forum.com/u/gcaplan)\
**Post date:** [March 8, 2012, 4:34am UTC](https://www.ruby-forum.com/t/can-anyone-really-explain-opcode-caching-with-php/217637/5 "2012-03-08T04:34:45Z")

</div>

> Roberto
> 
> > I am not a big fan of opcode caching
> 
> I’m interested in your reasons for this. My experience with mod\_php is  
> that I get huge performance improvements with opcode caching (my app  
> loads a lot of code and libraries). If you don’t use opcode caching, is  
> there a better way to get the same benefits?

Your answer, is my reason 🙂 It is app-dependent, so i never blindly  
suggest to someone using it, without a proper undestanding of the  
system.

Often i have seen badly configured apache server exposing the cached  
files  
to all the users of the system and so on. I see it as a second step in  
building an infrastructure. Having said that, i prefer having an  
additional layer (like nginx caching or varnish, as already suggested)  
in  
front of the apps. …But you can obviously have opcode and caching both  
working on the same app 🙂

–  
Roberto De Ioris

> **[unbit - Overview](https://github.com/unbit)**
>
> makers of uWSGI and many more open source projects - unbit

---

<div class="post-metadata">

**Author:** ![gcaplan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8baadc/32.png) [@gcaplan](https://www.ruby-forum.com/u/gcaplan)\
**Post date:** [March 8, 2012, 4:19am UTC](https://www.ruby-forum.com/t/can-anyone-really-explain-opcode-caching-with-php/217637/6 "2012-03-08T04:19:50Z")

</div>

Roberto

> I am not a big fan of opcode caching

I’m interested in your reasons for this. My experience with mod\_php is  
that I get huge performance improvements with opcode caching (my app  
loads a lot of code and libraries). If you don’t use opcode caching, is  
there a better way to get the same benefits?

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,223556,223568#msg-223568](http://forum.nginx.org/read.php?2,223556,223568#msg-223568)

---

<div class="post-metadata">

**Author:** ![gcaplan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8baadc/32.png) [@gcaplan](https://www.ruby-forum.com/u/gcaplan)\
**Post date:** [March 8, 2012, 4:36am UTC](https://www.ruby-forum.com/t/can-anyone-really-explain-opcode-caching-with-php/217637/7 "2012-03-08T04:36:00Z")

</div>

> Thanks for the heads up on uWSGI. But my sysadmin skills are basic (I’m  
> a developer really) and it looks a bit scary. There’s not much info out  
> there, while fastcgi is supported in the php core. For someone who is  
> not a proper sysadmin, is uWSGI, in its present state of development,  
> really a sensible way to go?

php-fpm (far better that the fastcgi embedded server) is in the core  
from  
a couple of php releases (even if not compiled-in by default and  
somewhere  
marked as experimental) but you will find tons of users of it.

uWSGI got proper php support from a couple of weeks, but it has the  
advantage in having years of python/perl/lua hosting, so it has a very  
solid core. Is its php plugin stable ? I would be a fool in saying that  
after only two weeks, but i suggest you to maintain an eye on it 🙂

–  
Roberto De Ioris

> **[unbit - Overview](https://github.com/unbit)**
>
> makers of uWSGI and many more open source projects - unbit

---

<div class="post-metadata">

**Author:** ![gcaplan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8baadc/32.png) [@gcaplan](https://www.ruby-forum.com/u/gcaplan)\
**Post date:** [March 8, 2012, 8:16am UTC](https://www.ruby-forum.com/t/can-anyone-really-explain-opcode-caching-with-php/217637/8 "2012-03-08T08:16:11Z")

</div>

Hi guys

You’ve been brilliant - so for now I go with php-fpm and xcache, and I  
actually understand what’s happening!

As I say, the community might consider adding this info to the FAQs, as  
there’s a lot of misleading blogs and post out there on this whole issue  
of fastcgi and opcode caching…

Thanks for all your help!

Posted at Nginx Forum:

> **[Re: Can anyone \*really\* explain opcode caching with PHP?](https://forum.nginx.org/read.php?2,223556,223583#msg-223583)**
>
> Hi folks Thinking of abandoning mod\_php for the wonderful world of nginx, but googling the issue of opcode caching with fastcgi is proving surprisingly confusing: there's a lot of conflicting information out there. My basic question is this: what is...
