Cache all the things - A guide to caching Drupal


Published on

Presented at nwdug on 09/01/13

1 Comment
  • I'm using for my slideshares
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Cache all the things - A guide to caching Drupal

  1. Cache All The Things
  2. About MeMike BellDrupal Developer@mikebell_
  3. Exactly what things?erm...everything!
  4. No really...Frontend:- HTML- CSS- Images- JavascriptBackend:- PHP- MySQLSo pretty much everything then!
  5. Where to start?Ask yourself: Why is my site slow?a) Bad codeb) Bad serverc) Its not!
  6. Bad CodeStop.Caching will help... but it wont solve yourbigger issues.There are tools to help - xhprof (not coveredhere)
  7. Bad Server?So hows £5.99 a month working out for you?Caching will help... but you still have biggerissues.Scale horizontally/vertically for a quick fix thenwork on additional caching.
  8. Its not!I like you!Caching is always a good thing, except whenyou have to debug it...
  9. Caching in Drupal- Code Level (cache_get/cache_set)- APC (not covering)- Resource caching (this gets tricky!)- DB Layer- CDNs (related to resource caching)
  10. Code LevelDrupal has its own caching api.Module Developers use it!cache_set - store data in Drupals cache tablecache_get - get data from Drupals cache tableYou can even define your own cache tables!
  11. Code Level (example)<?phpfunction mymodule_cached_data($reset = FALSE) { global $language; $langcode = $language->language; $data = &drupal_static(__FUNCTION__, NULL, $reset); if (!isset($data)) { if (!$reset && $cache = cache_get("mymodule_cached_data:$langcode") && !empty($cache->data)) { $data = $cache->data; } else { $data = t(This would be an array or string generated using translated strings.); cache_set("mymodule_cached_data:$langcode", $data); } } return $data;}?>(source -
  12. Resource CachingMany different types of resourcesEach can be cached by different toolsExample:- Drupals CSS/JS Cache- Drupals Page Cache- Boost- Varnish
  13. Resource CachingDrupal default: CSS/JS and Page(taken from - ooppss!)
  14. Resource Caching - BoostBoost - caching for all Drupal AssetsAnonymous users onlyIdeal for shared hosting environmentsPotentially the easiest to setup
  15. Resource Caching - VarnishApplication AcceleratorLinux OnlyCaches resources based on initial viewAnonymous only - very picky with sessions andcookiesInsanely Powerful
  16. Resource Caching - VarnishAdvanced setup - requires additional moduleand VCL for Drupal- ttls (time to live)Specific resource (image/css) and pageelement! All available through VCL config.
  17. Resource Caching - VarnishAnatomy of a varnish hit:HTTP/1.1 200 OKServer: ApacheX-Drupal-Cache: MISSExpires: Sun, 19 Nov 1978 05:00:00 GMTLast-Modified: Tue, 08 Jan 2013 21:20:17 +0000Cache-Control: public, max-age=900ETag: "1357680017-1"Content-Language: enX-Generator: Drupal 7 ( </node/5468>; rel="shortlink",<>; rel="canonical"Vary: Accept-EncodingContent-Type: text/html; charset=utf-8Date: Tue, 08 Jan 2013 21:20:22 GMTX-Varnish: 979772308 979772303Age: 3Via: 1.1 varnishConnection: keep-aliveX-Cache: HIT
  18. Resource Caching - VarnishCache Stores- RAM- DiskRam - super fast - requires a lot of ramDisk - fast - disk space is rarely an issue
  19. Data Caching - memcachedData and Database caching layerCaches cache...(and things!)
  20. Data Caching - memcachedExample:Core cache - core cache tables (cache_*)Moved into memcached and shared betweenmultiple instances across multiple serversSimple api makes getting and setting easyDrupal requires - and pecl extension
  21. 3rd Party Caching - AkamaiOne of the biggest caching providersA high chance youve experienced Akamai 4-5times today.Static cache of resourcesContent Distribution Network100+ Edge servers all over the planet
  22. 3rd Party Caching - AkamaiCoverage for DDOS attacks- Active monitoring and defenseAdvanced redirection rules through controlpanelIts not cheap!Its also pretty magical.
  23. 3rd Party Caching - CloudflareSimilar to AkamaiSmaller scaleEmphasis on security and DDOS protection
  24. Content Distribution NetworksCDNs for shortOffload resource handling to external provider- Images- Video- Audio- Large filesSyndicate content around the world
  25. Content Distribution NetworkWhy use a CDN?- Content is delivered quicker-- Latency cut downPricing is granular - pay as you go type dealAmazon + Rackspace Cloud Files
  26. How does it all fit together? Apache / Mysql / Varnish Nginx memcache Apache / Mysql / Nginx memcache Akamai Load Balancer Apache / Mysql / Nginx memcache Varnish Apache / Mysql / Nginx memcache
  27. What happens when it all goeswrong?
  28. Caching to the rescueEach layer provides protection3rd Party - shield 99% of anon trafficVarnish - the restAnonymous users should never know your sitehas issues.
  29. Debugging TipsConfigure different urls for different platforms- Easier to identify what cache layer has anissueDont be afraid to flush caches- Your backend should be able to cope with itAnalyse headers! Drupal and Varnish provide alot of info
  30. Debugging TipsMonitor each layer- Icinga- Pingdom
  31. Any Questions?