Magento Expert Forum - Improve your Magento experience

Results 1 to 4 of 4

Magento Cache strategy for best performance

  1. #1
    Administrator david's Avatar
    Join Date
    Nov 2012
    Posts
    261
    Thanks
    22
    Thanked 42 Times in 34 Posts

    Post Magento Cache strategy for best performance



    There are many blog posts about magento cache and how to configure it right.

    In this blog post i want to explain which cache backend (File System, APC, Memcached, Redis) is recommended in which environment (single server, multi server) and how to configure it.

    Understanding Magento’s Two-Level Caching

    Magento uses by default the two level cache backend from zend framework.

    Basically it’s about storing cache records in two backends, in a very fast one (but limited) like APC or Memcached and in a “slow” one like file system.

    Each cache backend has it’s features. APC and Memcached are key/value cache backends, they don’t support tagging (grouping of cache entries). File system and Redis has tagging support.

    Here you see how two level caching i supposed to work: (Thanks to Fabrizio Branca)




    Explanation of magento cache backends


    File system (var/cache)

    By default, Magento stores it’s cache entries in the file system, you find the cache in var/cache/.

    This cache backend is fine for small websites with a small cache size but if you get more and more requests through visitors, reading and writing to the file system gets slower and slower from time to time because the cache grows.

    The Magento cache is organized by tags, this means you have cache entries which belongs to a cache group.

    Advantages

    It works by default, you don’t need to install additional software.

    Disadvantages:

    Magento clears cache entries by tag e.g. after placing an order or saving a product to make sure the block caches are updated on the store. During clearing entries by cache tag magento has to open each file to check if the cache entry belongs to the cache group. If you have a magento shop with >= 1000 products you will have a cache size of > 50 MB. A 50 MB file system cache has about 3500 entries (files), you can imagine that this is very slow.

    Tips for better file system performance

    1. Use a SSD instead of normal hard disk
    2. Put the var/cache directory in tmpfs

    APC – Alternative PHP Cache (Key/Value)

    APC stands for Alternative PHP Cache and is a free and open opcode cache for PHP. It’s goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.

    Advantages

    A very fast cache backend.

    With APC on your server you can automatically have duplicate PHP script executions optimized to run more efficiently.

    APC also provides a user cache for storing application data.

    Disadvantages

    APC does not support tagging, you still have a slow file system as slow level cache.

    Requirements

    PHP APC extension must be installed on server.

    Tips for better performance

    Make sure to give APC enough memory by the parameter “apc.shm_size”

    Checkout our best practise configuration for php.ini (below)

    Configuration (app/etc/local.xml)

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

    Settings for php.ini

    apc.enabled = 1
    apc.optimization = 0
    apc.shm_segments = 1
    apc.shm_size = 768M
    apc.ttl = 48000
    apc.user_ttl = 48000
    apc.num_files_hint = 8096
    apc.user_entries_hint = 8096
    apc.mmap_file_mask = /tmp/apc.XXXXXX
    apc.enable_cli = 1
    apc.cache_by_default = 1
    apc.max_file_size = 10M
    apc.include_once_override = 0

    Memcached (Key/Value)


    memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

    Advantages

    A very fast cache backend.

    Disadvantages

    Memcached does not support tagging, you still have a slow file system as slow level cache.

    Requirements

    - Memcached server
    - PHP extension for memcached

    Configuration (app/etc/local.xml)


    <global>
    ...
    <cache>
    <backend>memcached</backend>
    <!-- apc / memcached / empty=file --> <memcached>
    <!-- memcached cache backend related config -->
    <servers>
    <!-- any number of server nodes can be included -->
    <server>
    <host><![CDATA[127.0.0.1]]></host>
    <port><![CDATA[11211]]></port>
    <persistent><![CDATA[1]]></persistent>
    </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>
    ...
    </global>

    Redis – Advanced key-value store with full cache tag support

    This magento cache backend allows you to use a redis server as a central storage. Cache tags
    are supported, we don’t need to use the slow level file system cache anymore.
    This magento cache backend is highly recommended in multi server environments where you have more than one webserver.

    Advantages

    Very fast cache backend with fully cache tag support, no slow level file system cache is needed
    Tested on high traffic magento stores with more than 500.000 visitors / day, performance is great and stable.
    Requirements:

    - Redis must be installed on the server
    - PHP Extension phpredis must be installed
    - Magento extension “Cm_Cache_Backend_Redis” must be installed

    Installation

    1. Install redis (2.4+ required)
    2. Install phpredis
    3. Install the magento extension “Cm_Cache_Backend_Redis”
    4. Edit your app/etc/local.xml

    <global>
    ...
    <cache>
    <backend>Cm_Cache_Backend_Redis</backend>
    <backend_options>
    <server>127.0.0.1</server>
    <!-- or absolute path to unix socket -->
    <port>6379</port>
    <persistent></persistent>
    <database>0</database>
    <password></password>
    <force_standalone>0</force_standalone>
    <connect_retries>1</connect_retries>
    <automatic_cleaning_factor>0</automatic_cleaning_factor>
    <compress_data>1</compress_data>
    <compress_tags>1</compress_tags>
    <compress_threshold>20480</compress_threshold>
    <compression_lib>gzip</compression_lib>
    <!-- Supports gzip, lzf and snappy -->
    </backend_options>
    </cache>
    ...
    </global>

    Useful tool for redis


    phpRedisAdmin
    phpRedisAdmin is a simple web interface to manage Redis databases.
    Demo: http://dubbelboer.com/phpRedisAdmin/?overview




    Conclusion



    If you have a small store with a small cache size then APC + file system as second level
    cache would be fine for you. Furthermore i recommend to use a SSD and to put the cache directory into tmpfs.
    If you have a bigger store and a dedicated server you should take a look a redis, it’s very fast also for big cache sizes > 500 MB. Redis is also the perfect cache solution in a multi server environment where you need a central cache.

    View more threads in the same category:


  2. The Following User Says Thank You to david For This Useful Post:

    Parth Patel (31-07-2013)

  3. #2
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    WooooooooooW!!! Very technical and really helpful! Thanks for sharing.

  4. #3
    New member
    Join Date
    Jul 2013
    Location
    52 Springvale Pope Pius XII Street Mosta MST2653 Malta
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes! agreed one of the best Platform of Ecommerce. Magento plays a vital role for E-Business Companies. This reason is good, but some issues like speed, cache and multisite are suffer mostly magento users. CloudWays bring a best service to Managed Complete platform to support and solve technical issues of Magento.

    Managed Magento Hosting

  5. #4
    Junior Member
    Join Date
    May 2014
    Location
    India
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    That was a great post and the coding was useful too!!!

Similar Threads

  1. Move Magento Cache to RAM by tmpfs
    By david in forum Magento Speed up, Performance and Optimize
    Replies: 4
    Last Post: 13-06-2016, 12:19 PM
  2. How I can check my magento site performance?
    By brent in forum Magento Speed up, Performance and Optimize
    Replies: 4
    Last Post: 26-02-2015, 03:48 PM
  3. Optimizing Magento Performance
    By shunavi in forum Magento Speed up, Performance and Optimize
    Replies: 13
    Last Post: 14-10-2014, 11:14 PM
  4. Adding Cache Support to Magento Blocks
    By sgdev in forum Magento Speed up, Performance and Optimize
    Replies: 2
    Last Post: 29-05-2014, 10:47 AM
  5. How to configuring APC cache for Magento?
    By david in forum Programming & Development
    Replies: 1
    Last Post: 24-03-2013, 09:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •