Magento Part 6 - Caching

<< Back to Magento Performance Tips for Scalability Homepage
With Magento you need to utilise some sort of caching service for it's performance to be acceptable.

MemCache

Memcache is a really easy caching service to setup. Simply setup memcache on all of your front-end servers.

Add this config to your local.xml (I just use it for staging and production environments).

<cache>
    <backend>memcached</backend>
    <slow_backend>database</slow_backend>
    <slow_backend_store_data>0</slow_backend_store_data>
    <auto_refresh_fast_cache>0</auto_refresh_fast_cache>
    <lifetime>259200</lifetime>
    <memcached>
        <servers>
            <server>
                <host><![CDATA[127.0.0.1]]></host>
                <port><![CDATA[11211]]></port>
                <persistent><![CDATA[0]]></persistent>
                <weight><![CDATA[2]]></weight>
                <timeout><![CDATA[5]]></timeout>
                <retry_interval><![CDATA[5]]></retry_interval>
                <status><![CDATA[1]]></status>
            </server>
        </servers>
        <compression><![CDATA[0]]></compression>
        <cache_dir><![CDATA[]]></cache_dir>
        <hashed_directory_level><![CDATA[]]></hashed_directory_level>
        <hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
        <file_name_prefix><![CDATA[]]></file_name_prefix>
    </memcached>
</cache>

Redis Cache

Redis is an amazing caching platform that you can use with Magento. The great news is you can easily setup Redis on AWS ElasticCache.

Below is an example of a Redis configuration in local.xml

<cache>
    <backend>Mage_Cache_Backend_Redis</backend>
    <backend_options>
        <server><![CDATA[REDIS_SERVER_HOST]]></server>
        <port><![CDATA[REDIS_SERVER_PORT]]></port>
        <persistent><![CDATA[]]></persistent>
        <database><![CDATA[0]]></database>
        <password><![CDATA[]]></password>
        <force_standalone><![CDATA[0]]></force_standalone>
        <connect_retries><![CDATA[1]]></connect_retries>
        <read_timeout><![CDATA[10]]></read_timeout>
        <automatic_cleaning_factor><![CDATA[0]]></automatic_cleaning_factor>
        <compress_data><![CDATA[1]]></compress_data>
        <compress_tags><![CDATA[1]]></compress_tags>
        <compress_threshold><![CDATA[20480]]></compress_threshold>
        <compression_lib><![CDATA[gzip]]></compression_lib>
    </backend_options>
    <id_prefix>YOUR_CACHING_PREFIX</id_prefix>
</cache>

Varnish

This is where the fun begins! Varnish is a front-end proxy (or reverse proxy). Varnish is amazing, however with Magento it can be a pain to configure correctly (give yourself a few days to get this configured correctly). The setup is easy, it is the testing and tuning that takes some time. I use a free Magento extension called 'Turpentine - varnish cache' to connect Magento and Varnish.

Grab yourself a copy here
http://www.magentocommerce.com/magento-connect/turpentine-varnish-cache.html

This is the basic setup

  1. Install the Varnish Turpentine extension for Magento.
  2. Install Varnish on all your front-end servers (You could run this one separate servers if you wish, but I haven't needed to).
  3. Change apache over to run on port 8080.
  4. Configure Varnish to run on port 80.
  5. How the request cycle works is:
    1. All/most HTTP traffic comes in and hits your Varnish server.
    2. Varnish checks it's local cache for that content/URL.
    3. If it exists in cache, Varnish returns it immediately to the user. 
    4. If it doesn't exist in cache, Varnish passes the request to Apache (in the background on port 8080).
    5. Apache returns the content to Varnish.
    6. Varnish returns the content to the user.
    7. All subsequent requests are then served from Varnish cache.
Varnish will reduce your CPU usage massively, so it's a great feature to install and configure correctly (it is worth the effort).

Make sure you clear your varnish cache on deploying a new codebase (otherwise you may notice strange behavior/bugs on your site). The deployment scripts in Part 2 - Prepare for Scalability have an example of clearing varnish cache on deployment.

Here is an example of a Magento configuration for Varnish




That's about it - head on back to the Magento Performance Tips for Scalability post if you missed anything.


0 comments:

Post a Comment