Improve the Performance and Scalability of Your Drupal Site

Improve the Performance and Scalability of Your Drupal Site

Here at Achieve Internet, we perform a lot of system reviews for our clients to help them improve the performance and scalability of their sites.  From these numerous system reviews, we've identified 5 common things that are missing from many hosting configurations.

  • APC
  • PHP realpath_cache_size
  • MySQL Query Cache
  • Memcached
  • Drupal optimizations

APC

APC, or one of its alternatives, should be installed and configured on every web server. APC caches the compiled op-code for your PHP application. This has two main benefits:

  • Reduces the amount of time that it takes the server to render each page (pages load faster)
  • Reduces the amount of RAM that the server must use to render each page

There are two main APC settings that will have the greatest impact on your site:

  • shm_size
  • stat

The "shm_size" setting determines how much memory APC will allocate to caching your Drupal site's PHP code. A good starting point for a single Drupal 7 site is 64MB although you should monitor APC via the apc.php page to determine the optimal value for your server and sites.

The "stat" setting determines if APC checks the original PHP source files to see if they have been modified before using the cached version. By setting stat=0, you can tell APC that the source PHP files do not change and thus reduce the overhead of the disk I/O to check the status of the PHP files. A word of caution though for this setting: only disable stat on production servers where the source code rarely or never changes. Otherwise you'll find yourself having to manually clear the APC cache after every code update.

PHP realpath_cache_size

Drupal sites make extensive use of include files. By default PHP attempts to cache the file system pointers for any included file. However, the default cache size is too small for Drupal sites. Increasing this setting in your php.ini will reduce the number of disk I/O operations that PHP performs in rendering pages. A good starting point for this setting is 64KB although as with all optimizations, you should monitor the actual usage to determine the optimal value for your particular situation. You can see how much of the realpath cache is being used by creating a PHP page that calls ealpath_cache_size().

The impact of this setting will vary greatly from site to site.  On a large Drupal site you could see anywhere from a 1% to 10% improvement in page load times.

MySQL Query Cache size

The MySQL query cache caches the results of frequently run queries, though amazingly some default MySQL installations comes with this disabled. A good starting point for the query_cache_size is 128MB. The optimal value for this should be a value that provides a cache hit ratio of at least 95% while not having an excessive amount of unused memory.

A related setting is the query_cache_limit. This determines the largest query result set that can be stored in the cache. Generally, we start with the default value of 1MB and adjust up or down based on the information being cached.

Memcached

Memcached is a memory resident data cache that is used to mostly replace the cache_* tables in the MySQL database. Installing Memcached and configuring your site to use it can have a dramatic impact on the performance and scalability of your site. On a stock Drupal installation, a test of Memcached showed that cache reads were 2x faster, cache writes 40x faster, and the number of database queries was reduced by almost 50% per page load.

Memcached can sometimes be challenging to setup and get working correctly since there are several pieces that have to be installed and configured to work together. The three main components you need to use Memcached are:

  • The memcached daemon itself (http://www.memcached.org)
  • The PHP PECL memcached extension (http://pecl.php.net/package/memcached)
  • The Drupal Memcache module (http://drupal.org/project/memcache)

Drupal optimizations

Drupal comes with some good performance optimization options out of the box when properly configured. Here are a few:

  • Enable Anonymous page caching.
  • Enable CSS, JS, and Page Aggregation/Compression.
  • Set the page cache expiration time reasonably large, say 60 minutes or more for sites with infrequently changing content.
  • Consider using the Boost module (http://drupal.org/project/boost) if your site is mostly anonymous traffic and you cannot setup a Varnish (https://www.varnish-cache.org/) server.

While most of the options out of the box work well, do not use the Statistics module.  Instead, use an external analytics service such as the Google Analytics API.

Conclusion

Achieving a high performance and scalability website is all about enabling your servers to render pages as quickly as possible while using as few resources as possible. These core tools are just the starting point for having a well tuned hosting environment and should be tailored for your specific situation and website.

A final word of caution, while we've provided some reasonable starting points for these optimizations, just blindly dropping them into your environment without tuning for your specific needs can actually reduce the performance of your site. Once these settings are tuned for your environment though, you will see a significant improvement in both performance and scalability.