Caching with Drupal DrupalCamp Helsinki 27.05.2009 Kalle Virta, Exove
Why to cache with Drupal As Drupal is made with PHP it tends to burden the SQL server pretty much Drupal is a single-controller architecture, and it loads a lot of code by default on every pageload Compared to a pure MVC-model (e.g. CodeIgniter framework), the architecture of Drupal makes it usually somewhat slower You always need caching, and you should always be aware of your scalability, i.e. the upgrade path Even positive problems (too much traffic) are problems at the end
Overview of caching levels Drupal block caching Drupal internal page caching HTTP cache, e.g. reverse proxy CDN (Content Delivery Network) solutions Custom cache built-in to your code with Drupal’s cache API Opcode caching (e.g. APC) SQL query caching
Drupal internal caching From Drupal 6, there’s been a block cache available Drupal also has internal pagecache in it’s core It caches pages by saving the finished HTML to the database It (in the usual cases) turns off if you’re logged in to the system With a contrib module you can make Drupal use other caching systems instead of the database
HTTP level caching If you can use it fully, HTTP caching can make the site really fast, depending on the software used (Using nginx as the system paired with it’s own connection to your pagecache running on memcached, you can get to over 3000 reqs/sec with a single server) Only for anonymous users Most pages logged in user views are really not that dynamic You can use Javascript to cheat and use http caching partially for logged in users
Magnitude of performance gain through a couple of “page” caching techniques
Code level caching Code level caching in Drupal usually focuses to caching data coming from SQL, since all data is usually saved into the SQL server Not always, though. An XML data storage is usually way slower and needs caching even more. Drupal has it’s own cache control API for custom caching, many contrib modules are already programmed to use this Drupal’s own cache saves data to the SQL server, this is fine from low to medium traffic sites, but high traffic sites need to save the cache to memory For saving the Drupal cache (through the normal API) to memory, you can override the Drupal default database caching to use e.g. memcached using Cache Router contrib module, now in beta8 for D6 This is the best way to make very dynamic content fast
Opcode cache Opcode caching mean caching the compiled bytecode of PHP program code Essentially it skips the parsing and compiling of PHP code when the cached code is ran Opcode caching with a PHP accelerator is simple, works with most PHP applications and gives you a significant boost in performance
Caching with Drupal Drupal HTTP cache Drupal page cache
What should I do with my site? Usually it comes to three questions “ Is the site already built?” “ What’s the share of anonymous/logged in users?” “ What options does my hosting environment even allow?” Profile! Use xdebug or similar profiling tool to see what actually happens during a page load (remember to profile under load, the results tend to change when the system is under load) Usually the first couple of rounds of profiling give you clear bottlenecks in the system – after that it gets harder Call for professional help 
Questions? Thank you for your time

Drupal caching

  • 1.
    Caching with DrupalDrupalCamp Helsinki 27.05.2009 Kalle Virta, Exove
  • 2.
    Why to cachewith Drupal As Drupal is made with PHP it tends to burden the SQL server pretty much Drupal is a single-controller architecture, and it loads a lot of code by default on every pageload Compared to a pure MVC-model (e.g. CodeIgniter framework), the architecture of Drupal makes it usually somewhat slower You always need caching, and you should always be aware of your scalability, i.e. the upgrade path Even positive problems (too much traffic) are problems at the end
  • 3.
    Overview of cachinglevels Drupal block caching Drupal internal page caching HTTP cache, e.g. reverse proxy CDN (Content Delivery Network) solutions Custom cache built-in to your code with Drupal’s cache API Opcode caching (e.g. APC) SQL query caching
  • 4.
    Drupal internal cachingFrom Drupal 6, there’s been a block cache available Drupal also has internal pagecache in it’s core It caches pages by saving the finished HTML to the database It (in the usual cases) turns off if you’re logged in to the system With a contrib module you can make Drupal use other caching systems instead of the database
  • 5.
    HTTP level cachingIf you can use it fully, HTTP caching can make the site really fast, depending on the software used (Using nginx as the system paired with it’s own connection to your pagecache running on memcached, you can get to over 3000 reqs/sec with a single server) Only for anonymous users Most pages logged in user views are really not that dynamic You can use Javascript to cheat and use http caching partially for logged in users
  • 6.
    Magnitude of performancegain through a couple of “page” caching techniques
  • 7.
    Code level cachingCode level caching in Drupal usually focuses to caching data coming from SQL, since all data is usually saved into the SQL server Not always, though. An XML data storage is usually way slower and needs caching even more. Drupal has it’s own cache control API for custom caching, many contrib modules are already programmed to use this Drupal’s own cache saves data to the SQL server, this is fine from low to medium traffic sites, but high traffic sites need to save the cache to memory For saving the Drupal cache (through the normal API) to memory, you can override the Drupal default database caching to use e.g. memcached using Cache Router contrib module, now in beta8 for D6 This is the best way to make very dynamic content fast
  • 8.
    Opcode cache Opcodecaching mean caching the compiled bytecode of PHP program code Essentially it skips the parsing and compiling of PHP code when the cached code is ran Opcode caching with a PHP accelerator is simple, works with most PHP applications and gives you a significant boost in performance
  • 9.
    Caching with DrupalDrupal HTTP cache Drupal page cache
  • 10.
    What should Ido with my site? Usually it comes to three questions “ Is the site already built?” “ What’s the share of anonymous/logged in users?” “ What options does my hosting environment even allow?” Profile! Use xdebug or similar profiling tool to see what actually happens during a page load (remember to profile under load, the results tend to change when the system is under load) Usually the first couple of rounds of profiling give you clear bottlenecks in the system – after that it gets harder Call for professional help 
  • 11.
    Questions? Thankyou for your time