Nginx & PHP caching: fastcgi_cache, proxy_cache, APC or memcached or combo?

If your box can handle the entire cache on its own, memcache will only slow you down. APC is shared memory. Used right it will blow away memcached. Nginx fast cgi cache will make all dynamic php pages scream. Even if you set the cache to just 10 seconds, this makes it such that that max hit rate to any given php page will be once every 10 seconds. Makes it impossible to crash a page with load. I run a bunch of websites on a single small box that gets millions of people per month. All you need is nginx caching and APC at this point.

Memcache comes into the picture only when you have to scale your cache beyond a single box.


http://php-fpm.org/ is what we've used on a few recent installations rather than FastCGI itself.

Nginx can serve pages right from memcached, so, your application could write the pages direct to memcached. Otherwise, you'd need to make sure your code utilized memcached for queries/objects. An opcode cache will help depending on your codebase. If you have a small set of scripts that are repeatedly run, APC or XCache (or eaccelerator in some cases) can provide a nice boost.

Your caching method is determined by your code. Can you cache pages? fragments? sql results? values? What is the lifetime of those entities, how much space do they need, how large is the key and result set? As for memcached being slower than APC, since they don't really perform the same task, I'm not sure what you compared.


IMHO we sysadmins tend to focus on this problem backwards, starting at the back end, because thats our turf. The most effective stuff is really at the front end. If you can get the browser cache (http headers) and http cache (cdn, headers again) part right you can do astonishingly sloppy things at origin and be fine.