What Is APC And How You Can Use It To Speed Up Your Website

When having a website, especially with high traffic, speed is everything. With a fast loading website, its pages can be delivered fast and easy, making them have a higher chance to convert visitors to leads.

Among the ways to speed up websites, one of the most popular is by using Alternative PHP Cache, or APC. It's a framework that optimizes PHP intermediate codes to then cache and compile it from the PHP bytecode compiler in the shared memory.

APC was originally scheduled for inclusion into the PHP core starting PHP 6. For previous versions, APC should be first installed and enabled manually on the server. Other accelerator projects that works similarly to APC are Optimizer Plus and Zend Opcache that is included in the core distribution of PHP starting version 5.5.

To understand why APC can benefit your PHP website by margins, we should first know how PHP is run.

PHP is a dynamic scripting language. What this means is when there is request for a page, the server must first parse the code in your PHP script in order to generate its resulting HTML code. This HTML code is then seen by visitors. PHP is ideal for web pages that are constantly updated and dynamic because each visitor will get a fresh copy of the page. But this advantage has a drawback, and that is an additional load to the server.

By having APC in place, it will cut down repeated PHP script execution, skipping the parsing and compiling process. To do this, APC stores the data in the opcode that can be simply executed each time the script is called again.

Without APC - With APC

The following are the steps how a PHP script is run by Zend so the code can be used by the Engine (opcode). In order:

  1. Reading the PHP code from the filesystem and put it into memory.
  2. Lexing the php code inside is converted into tokens (Lexicons).
  3. Parsing the tokens to be processed and derived at meaningful expressions.
  4. Compiling the derived expressions into opcodes.
  5. Executing the opcodes to get the final result.

PHP should go through the list in order. The goal of APC is to bypass steps from 1 to 4. And if configured correctly, APC can significantly increase websites that has many source codes.

By having the codes ready to serve inside the memory and by cutting down on dynamic PHP executions, web pages will load faster and caching of the PHP script executions can run more efficiently. This makes APC the de facto standard and one of the most widely used PHP opcode caching solutions on many high traffic websites.

APC is a PECL module that can be loaded into PHP. But because it operates at a server-level, you need to have access to your server in order to make APC run and to configure it. One thing that you should pay attention to when configuring APC is the size of its cache. If scripts loaded exceed the cache, APC will flush its cache and rebuilt it. This results in slow execution time, and can sometimes make a website to load far slower than running without APC in the first place.

To keep up with the update, you need to follow messages in your site's development environment and its log list.

APC is included and mentioned many times as a way for speeding up a website. But the state of the project is dormant with no new releases since September 2012.