Thursday, July 26, 2012

Memcache vs APC cache in PHP

I often see this question asked; which is a better caching mechanism for PHP: Memcache or APC cache. I wanted to write about how different both of them are inherently in their very concept.

Memcache for PHP is a distributed caching mechanism. If you have several webservers running under a load balancer serving the same content and you want a caching mechanism to avoid frequent database hits , Memcache is the way to go. When using Memcache you will make the update to one webserver and it would be auto-replicated across all the severs through distributed caching mechanism. Since it requires dealing with network protocols in order to support the distributed part of caching, it is slower compared to APC cache. If data is stored in APC cache, updates need to be done individually on all APC caches on all web servers. They wont be automatically replicated.

However, Memcached is NOT an Opcode cacher like APC. APC when employed will cache all the opcode the first time it is converted and serve the cached version for subsequent HTTP requests. APC can also be used to store data like Memcache but in a non-distributed manner. Most of the times memcache is used to store results of time consuming data queries, so the need to hit database on every query is eliminated and this gives a huge performance benefit.

The good part of both these technologies, is that they are compatible with each other. A good design for scalable websites should be employing APC for opcode caching and data-caching through Memcache to exploit the distributed capabilities across several webservers. If there is just a single webserver, using just APC cache for both opcode and data caching is a good idea.



http://www.4elements.com/blog/2011/07/

No comments:

Post a Comment