Magento speed

APC (Alternative PHP cache) is a free opcode cache for PHP, which optimize PHP intermediate code. APC engine cache the compiled bytecode of PHP scripts to avoid the overhead of parsing and compiling source code on each request.

Magento stores its cache data in the file system by default. This is fine for small sites with low traffic. As you get more and more traffic and HTTP requests, file system reading and writing will become slower.

The APC PHP module will not speed up the Magento itself, its eliminates the delay caused by having to parse Magento PHP files code on every HTTP request. It is recommend to use APC on production servers for Magento optimization.

If you use Firebug Net panel in your browser, you can measure Magento page generation time before and after APC cache activation:
 

 

Installation of APC on your server

Nowadays all modern hosting providers support a variety of PHP cache engines including APC cache. You can ask your hosting support to install APC module for you. If you have a dedicated server, you can install APC yourself.

The PHP APC is PECL module, and should be installed in your system as any PECL module for PHP, more details is here.

Recommended APC configuration for Magento

By default APC configuration is not adjusted for Magento. In some cases you will see fatal errors in server logs, e.g. "PHP Fatal error: Cannot redeclare class". To fix such errors you need to change default APC configuration in PHP.INI file or APC.INI (depending on your configuration).

Here are recommended APC settings for Magento Community and Enterprise:

apc.enabled=1
apc.shm_size=512M
apc.num_files_hint=10000
apc.user_entries_hint=10000
apc.max_file_size=5M
apc.stat=0
apc.optimization=0
apc.shm_segments=1
apc.enable_cli=1
apc.cache_by_default=1
apc.include_once_override=1

In case of using APC bytecode cache, the modification time check option (apc.stat) must be disabled.

Activate APC support in Magento configuration

In order to activate APC support in Magento core, you should modify /app/etc/local.xml configuration file.
Add these lines between GLOBAL tags:

<global>
        ...
        <cache>
            <backend>apc</backend>
<prefix>MYSTORE_</prefix>
        </cache>
        ...
</global>

The "Prefix" value is a short descriptor that will allow you to use APC caching for several Magento stores on the same server, it should be unique for each Magento store you have.

If you want to enabled 2 level cache for your Magento, you can use slow_backend tag in local.xml.

Magento has two levels cache: a fast level and a slow level cache. Magento will use the fast level if possible, and if there is no memory available in APC, it will revert to the slow level cache. It will eliminate any down time connected with cache memory issues.

You can define "database" or "file" values for slow_backend:

<global>
        ...
        <cache>
            <backend>apc</backend>
            <slow_backend>database</slow_backend>
<prefix>MYSTORE_</prefix>
        </cache>
        ...
</global>

Save local.xml and test Magento storefront. If you will notice any errors, make sure that local.xml was modified correctly.

How to check if APC is working correctly?

First of all you should notice that Magento PHP scripts become faster. Use Firebug Net panel to measure Magento load time, as it described in the beginning of this article. You can download Firebug extension for Firefox here.

Next, you can download special APC.php script that will show APC usage statistics, such as memory usage, uptime, number of successfully cached queries (Hits & Misses), etc. You can also clear APC cache using this script.

APC.php script could be downloaded from official PECL APC website: http://pecl.php.net/package/apc

Download the latest stable APC archive, open it and upload /apc-X.X.XX/apc.php script to your WWW folder. Next, visit apc.php using your browser.

You can also download this script directly from SVN repository: http://svn.php.net/viewvc/pecl/apc/trunk/apc.php?view=log

Don't forget to protect apc.php script using your own password. You can change APC password directly in the apc.php code using any text editor.

Here is screenshot of APC statistics that you will see after script uploading and execution in your browser:

 

 

 

The APC is powerful and stable cache engine, it is one of most popular PHP optimization tools that improve performance on high loaded websites and widely used with Magento.

Further reading:

Nexcess: Optimizing APC Cache Settings for Magento

Magento White Papers: Maximizing Performance and Scalability with Magento Enterprise Edition

Magebase: Speeding up Magento with APC

PECL: APC package

PECL: Latest APC dashboard (apc.php)