Dec 2, 2008

How to enable Memcached cache in TYPO3 4.3

Yesterday evening I committed a fix for the Memcached cache backend to TYPO3 4.3. If you work with current trunk version of TYPO3, you can enable this backend and enjoy speed improvements.

Types of caches in TYPO3

There are three different caches in TYPO3 4.3:
  • cache_hash
    This cache saves data produced by some TYPO3 functions, mainly by substituteMarkerArrayCached. This cache can be very large, it is used only if page is not cached and it does not make sense to enable Memcached for this cache. Database or file will work best.
  • cache_pagesection
    This cache stores parsed TypoScript templates. It can be stored in Memcached but database is my personal preference for this cache.
  • cache_pages
    This cache stores pages. This is what needs to be as fast as possible. So Memcached will be the best candidate for this cache.

How to configure caches

By default caches use database to store data. However it can be changed. For example the following code in typo3conf/localconf.php will use different cache backends for different caches:

$TYPO3_CONF_VARS['SYS']['caching']['cacheBackendAssignments'] = array(
'cache_hash' => array(
'backend' => 't3lib_cache_backend_File',
'options' => array(
)
),
'cache_pages' => array(
'backend' => 't3lib_cache_backend_Memcached',
'options' => array(
'servers' => array('localhost:11211'),
)
),
'cache_pagesection' => array(
'backend' => 't3lib_cache_backend_Db',
'options' => array(
'cacheTable' => 'cache_pagesection',
)
)
);

Bold text shows you how different caches are configured. Here cache_hash uses file backend. If you symlink typo3temp/ or typo3temp/cache/ to the RAID1 partition, this cache will be very fast and will not produce any load on MySQL.

cache_pagesection uses database. Notice how we configure table name.

cache_pages uses Memcached. Configuration says that Memcached server is located on the localhost and accessible through port 11211 (default Memcached configuration). You can set up other Memcached instances and add them to the list separating instances by commas. Make sure you have no spaces anywhere. But localhost will work best because network delays will make Memcached useless.

Install Memcached

Memcached should be installed on the server. Internet has plenty of instructions about it, so I will not repeat them.

Configure Memcached in PHP

I do not pretend to be an expert in Memcached but I created a reasonable configuration for Memcached locally. This requires modifying php.ini. After changing default values Memcached requests became faster (I confirmed it by profiling with XDebug). Here is my settings:

memcache.allow_failover 1
memcache.chunk_size 32768
memcache.default_port 11211
memcache.hash_function fnv
memcache.hash_strategy consistent
memcache.max_failover_attempts 20

And of course you need to have Memcached PHP extension installed.

I am going to use Memcached on my own servers when TYPO3 4.3 is out. It is really promising in terms of performance.

1 comment:

  1. The above examples arent correct anymore, it's more like:

    $TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations']['cache_pagesection']['backend'] = 't3lib_cache_backend_FileBackend';

    now.

    ReplyDelete