April 15, 2009

6 Quick and Simple techniques to speed up Drupal performance


Attaining performance and scalability in any large production Drupal site requires a substantial amount of time and effort. In addition, speed gains become smaller and smaller after more and more work is done. Eventually, it feels like the exercise is becoming futile. On the other end of this optimization, you can do a few small things to a small Drupal site and considerably decrease load times; and (almost) no coding is required. Here are 6 suggestions that will help almost any Drupal site and they are easy to do. This is based on years of experiencing with Drupal, and some lovely research done by my Drupal peers, such as the great performance writeups at 2bits.com

Attaining performance and scalability in any large production Drupal site requires a substantial amount of time and effort. In addition, speed gains become smaller and smaller after more and more work is done. Eventually, it feels like the exercise is becoming futile. On the other end of this optimization, you can do a few small things to a small Drupal site and considerably decrease load times; and (almost) no coding is required. Here are 6 suggestions that will help almost any Drupal site and they are easy to do. This is based on years of experiencing with Drupal, and some lovely research done by my Drupal peers, such as the great performance writeups at 2bits.com

Integrate Memcached, a memory based caching system.

  • The Good:
    • Faster than Drupal’s DB-based caching.
    • Two modules available: Memcache and Cache Router.
    • No patching required for Drupal 6.x
  • The Bad:
    • Patching required for Drupal 5.x
  • Additional Notes:
    • You need to have Memcached installed and running.
    • Either module will work but Cache Router is more flexible in that it can use caching tools other than Memcached.
    • In a multi-web server setup, configure Memcache settings (in settings.php) to use an IP address instead of 'localhost', so that the web servers may share Memcached bin(s) and increase cache hit rate.

Use Path Cache module to cache URL aliases.

  • The Good:
    • Significant reduction in URL lookup queries (drupal_lookup_path).
  • The Bad:
    • Useless without Memcached integration; actually, this module will make things worse by increasing DB queries.
    • Requires patching.
    • Module only available for Drupal 6.x
  • Additional Notes:
    • I rewrote the patch to work with Drupal 5.16 and it works well; download the patch here.

Use Boost module to statically cache whole pages to files.

  • The Good:
    • Serving the cached pages is blazing fast – no significant hit to the Drupal stack or Database.
    • Choose to cache certain pages or exclude specific ones.
    • If Akamai or a similar CDN is properly configured for your Drupal site, your static XHTML will be cached at Akamai (eliminating a server hit altogether).
  • The Bad:
    • Only really beneficial for anonymous users (similar to traditional Drupal page caching). However - massive speed ups if you have anonymous users.
  • Additional Notes:
    • It is important to modify .htaccess as directed and the cache directory in the root of the Drupal directory has to be writable.
    • When the page doesn’t look right upon reload, try excluding the path.

If using Drupal 5.x, Use Javascript Aggregator to “compress” JS files.

  • The Good:
    • Provides Drupal 6 JS aggregation to Drupal 5.x
    • Minifies JS files.
  • The Bad:
    • Some JS code may not work properly after being minified. Ensure that you do proper regression testing on a QA environment following the activation of this module
  • Additional Notes:
    • Clear the Drupal cache every time JS code has been updated.

Move JS to bottom of the page (a YSlow best practice)

  • The Good:
    • Page becomes visible even if JS component is not yet fully loaded.
  • The Bad:
    • You will get JS errors if you have in-line JS code because of dependency issues with JQuery or other libraries.
  • Additional Notes:
    • Cut and paste <?php print $scripts ?> from the top of the page.tpl.php template file to the bottom before the closing body tag.

Gzip components on the fly to decrease page size.

  • The Good:
    • Decreases bandwidth usage too
  • Additional Notes:
    • Add the following to your .htaccess:
       AddOutputFilterByType DEFLATE text/plain
       AddOutputFilterByType DEFLATE text/xml
       AddOutputFilterByType DEFLATE application/xhtml+xml
       AddOutputFilterByType DEFLATE text/css
       AddOutputFilterByType DEFLATE application/xml
       AddOutputFilterByType DEFLATE image/svg+xml
       AddOutputFilterByType DEFLATE application/rss+xml
       AddOutputFilterByType DEFLATE application/atom_xml
       AddOutputFilterByType DEFLATE application/x-javascript
       AddOutputFilterByType DEFLATE application/x-httpd-php
       AddOutputFilterByType DEFLATE application/x-httpd-fastphp
       AddOutputFilterByType DEFLATE application/x-httpd-eruby
       AddOutputFilterByType DEFLATE text/html
      
    • Requires the Apache module, mod_deflate.