
Upgrade Your Drupal Skills
We trained 1,000+ Drupal Developers over the last decade.
See Advanced Courses NAH, I know EnoughDay 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:
$settings['cache']['default'] = 'cache.backend.apcu';
$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!
About Drupal Sun
Drupal Sun is an Evolving Web project. It allows you to:
- Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
- Facet based on tags, author, or feed
- Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
- View the entire article text inline, or in the context of the site where it was created
See the blog post at Evolving Web