Day 1: Tweak Drupal 8 Performance: Use APCu - 24 days of performance goodies
It's the season again. The season for cookies, candles, christmas trees and ... Drupal 8.
Recently released Drupal 8 features a lot of performance improvements, but as Drupal 8 needs to scale from the smallest blog on shared hosting to the biggest websites in the world (with lots of webservers), it is sometimes difficult to find one-size-fits-all solutions. This little series will feature one little detail about Drupal 8 performance every day, but entries will be deliberately short. Let us start!
Day 1 - Use APCu exclusively for caches
Drupal 8 has a so-called fast-chained backend as the default cache backend, which allows to store data directly on the web server while ensuring it is correctly synchronized across multiple servers. APCu is the user cache portion of APC (Advanced PHP Cache), which has served us well till PHP 5.5 got its own zend opcache. You can think of it as a key-value store that is stored in memory and the basic operations are apc_store($key, $data), apc_fetch($keys) and apc_delete($keys). For windows the equivalent on IIS would be WinCache (http://drupal.org/project/wincache).
However when you are having just one server with Apache, MySQL, PHP you don't need the overhead of the fast_chained backend (which uses the database as consistent backend) and can store everything in APCu directly (provided you have enough memory).
To achieve that do the following steps:
Step 1 - Customize the default backend in settings.php
For this to work you need to add 4 lines to your settings.php:
<span class="php-variable" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;font-size:11.0751px;vertical-align:baseline;color:rgb(51,51,0);">$settings</span>[<span class="php-string" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;font-size:11.0751px;vertical-align:baseline;color:rgb(170,0,0);">'cache'</span>][<span class="php-string" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;font-size:11.0751px;vertical-align:baseline;color:rgb(170,0,0);">'default'</span>] = <span class="php-string" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;font-size:11.0751px;vertical-align:baseline;color:rgb(170,0,0);">'cache.backend.apcu'</span>;
$settings['cache']['bins']['bootstrap'] = 'cache.backend.apcu';
$settings['cache']['bins']['config'] = 'cache.backend.apcu';
$settings['cache']['bins']['discovery'] = 'cache.backend.apcu';
While we can override the default backend easily globally, this won't work for bootstrap, config and discovery, which all use the cache.backend.chainedfast, so we need to override those bins explicitly again in settings.php.
Step 2 - Benchmark
Benchmarks are always tricky as it very much depends on the used scenario. But here is one for page_cache and dynamic_page_cache disabled:
As we can see we save a good 5% and 4% also in function calls. As xhprof is always a little tricky, here is one without:
Before: 0.030 s
After: 0.028 s
which is again around 6%, so the change is worth it!
And that is it for today! Tags: drupaldrupal planethigh performance