APC is a performance-enhancing extension. It should not be confused with a magic pill, although having it around does provide a positive impact on performance! If configured incorrectly, APC can cause unexpected behaviour, however when implemented optimally APC can be a useful weapon in your arsenal. In this post we will examine APC's capabilities and it's application both as an opcode cache and in its less common usage as a data cache.
How APC differs from Memcached
APC is not a replacement for memcached. They are designed for different roles. memcached is a generic, distributed caching daemon. APC on the other hand is PHP-specific and limited in size. As such, you'll want to only use APC for local caching of articles and objects that are relatively small. This begs the question: why not just use memcached universally?
The answer to this question, and indeed the question of whether to use memcache or APC, depends entirely on your circumstance. If you have a small application under a lot of load, then APC is a good choice. APC is a PECL extension, so it is very easy to add into your PHP ecosystem if you have control over your platform. Memcache on the other hand has a dependency on the memcache libraries and an external daemon running. The additional dependencies can imply additional maintenance costs in terms of administering the system, so APC, which does not have these dependencies, can be a better choice.
As generic caches, we can store many types of item in either APC or memcached. These include opcodes (which we will examine in more depth later in this article), strings, or other result representation from database queries or HTTP calls to feeds and services external to your application. We can also store objects that are expensive to create within our application, ready to use again later. The key is that all these objects can or will be in the same state for a certain period of time.
A key metric is the projected sum of the size of all objects; this is a determining factor between chosing APC or memcached since this dictates the size of the cache required. Memcached is more relevant than APC when a large cache is required because the maximum allocated memory is only limited by the architecture in which it runs. As it is language-independent, memcached can be helpful when used creatively within applications constructed in multiple languages. The distributed nature of memcached also enable resource sharing and makes it flexible enough to fit within a scalable environment.
We should view APC and memcached as tools that have different applications, although some part of their functionalities do intersect. That said, APC and memcached can be used together if the application architecture would benefit from this approach. They can be part of a layer of caches, covering anything from database results to pages or snippets of pages for the view layer. This article examines the use of APC for opcode and data caching, if you want to know more about using memcached, read our other posts about getting started with Memcached and regarding the new memcached extension.
How important is APC?
How important APC is for your setup depends on whether speed is important, and that depends on where you use PHP. Certain scripts may not be time critical, but user-facing applications usually are. Another aspect to consider is whether there is a need to minimise resource utilisation. Usually, the full potential of APC can be seen best in environment where usage/traffic volume is high, i.e. there is a small code base being executed very frequently.
As you will know, PHP is an interpreted language. This means that a PHP script has to be read, parsed and compiled before it is executed. (There is a very interesting resource on slideshare regarding this, from Joseph Scott - Anatomy of a PHP Request). The process of serving a PHP script ends up looking something like this:
This process is not ideal in a deployment where speed and resource minimalisation is a requirement, because the code is repeatedly parsed and compiled. APC is an excellent tool under such circumstances as it caches compiled code for execution so that there is no need for code to be parsed an
Truncated by Planet PHP, read more at the original (another 27860 bytes)