Cache All The Things
About MeMike BellDrupal Developer@mikebell_http://drupal.org/user/189605
Exactly what things?erm...everything!
No really...Frontend:- HTML- CSS- Images- JavascriptBackend:- PHP- MySQLSo pretty much everything then!
Where to start?Ask yourself:                Why is my site slow?a) Bad codeb) Bad serverc) Its not!
Bad CodeStop.Caching will help... but it wont solve yourbigger issues.There are tools to help - xhprof (not coveredhere)
Bad Server?So hows £5.99 a month working out for you?Caching will help... but you still have biggerissues.Scale horizontal...
Its not!I like you!Caching is always a good thing, except whenyou have to debug it...
Caching in Drupal- Code Level (cache_get/cache_set)- APC (not covering)- Resource caching (this gets tricky!)- DB Layer- C...
Code LevelDrupal has its own caching api.Module Developers use it!cache_set - store data in Drupals cache tablecache_get -...
Code Level (example)<?phpfunction mymodule_cached_data($reset = FALSE) {  global $language;  $langcode = $language->langua...
Resource CachingMany different types of resourcesEach can be cached by different toolsExample:- Drupals CSS/JS Cache- Drup...
Resource CachingDrupal default: CSS/JS and Page(taken from digital006.com - ooppss!)
Resource Caching - BoostBoost - http://drupal.org/project/boostStatic caching for all Drupal AssetsAnonymous users onlyIde...
Resource Caching - VarnishApplication AcceleratorLinux OnlyCaches resources based on initial viewAnonymous only - very pic...
Resource Caching - VarnishAdvanced setup - requires additional moduleand VCL for Drupal- http://drupal.org/project/varnish...
Resource Caching - VarnishAnatomy of a varnish hit:HTTP/1.1 200 OKServer: ApacheX-Drupal-Cache: MISSExpires: Sun, 19 Nov 1...
Resource Caching - VarnishCache Stores- RAM- DiskRam - super fast - requires a lot of ramDisk - fast - disk space is rarel...
Data Caching - memcachedData and Database caching layerCaches cache...(and things!)
Data Caching - memcachedExample:Core cache - core cache tables (cache_*)Moved into memcached and shared betweenmultiple in...
3rd Party Caching - AkamaiOne of the biggest caching providersA high chance youve experienced Akamai 4-5times today.Static...
3rd Party Caching - AkamaiCoverage for DDOS attacks- Active monitoring and defenseAdvanced redirection rules through contr...
3rd Party Caching - CloudflareSimilar to AkamaiSmaller scaleEmphasis on security and DDOS protection
Content Distribution NetworksCDNs for shortOffload resource handling to external provider- Images- Video- Audio- Large fil...
Content Distribution NetworkWhy use a CDN?- Content is delivered quicker-- Latency cut downPricing is granular - pay as yo...
How does it all fit together?                                       Apache /    Mysql /                             Varnis...
What happens when it all goeswrong?
Caching to the rescueEach layer provides protection3rd Party - shield 99% of anon trafficVarnish - the restAnonymous users...
Debugging TipsConfigure different urls for different platforms- Easier to identify what cache layer has anissueDont be afr...
Debugging TipsMonitor each layer- Icinga- Pingdom
Any Questions?
Upcoming SlideShare
Loading in...5
×

Cache all the things - A guide to caching Drupal

4,454

Published on

Presented at nwdug on 09/01/13

1 Comment
3 Likes
Statistics
Notes
  • I'm using http://jingbox.com/five/ for my slideshares
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
4,454
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
23
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Cache all the things - A guide to caching Drupal

  1. 1. Cache All The Things
  2. 2. About MeMike BellDrupal Developer@mikebell_http://drupal.org/user/189605
  3. 3. Exactly what things?erm...everything!
  4. 4. No really...Frontend:- HTML- CSS- Images- JavascriptBackend:- PHP- MySQLSo pretty much everything then!
  5. 5. Where to start?Ask yourself: Why is my site slow?a) Bad codeb) Bad serverc) Its not!
  6. 6. Bad CodeStop.Caching will help... but it wont solve yourbigger issues.There are tools to help - xhprof (not coveredhere)
  7. 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. 8. Its not!I like you!Caching is always a good thing, except whenyou have to debug it...
  9. 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. 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. 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 - http://drupal.org/node/145279)
  12. 12. Resource CachingMany different types of resourcesEach can be cached by different toolsExample:- Drupals CSS/JS Cache- Drupals Page Cache- Boost- Varnish
  13. 13. Resource CachingDrupal default: CSS/JS and Page(taken from digital006.com - ooppss!)
  14. 14. Resource Caching - BoostBoost - http://drupal.org/project/boostStatic caching for all Drupal AssetsAnonymous users onlyIdeal for shared hosting environmentsPotentially the easiest to setup
  15. 15. Resource Caching - VarnishApplication AcceleratorLinux OnlyCaches resources based on initial viewAnonymous only - very picky with sessions andcookiesInsanely Powerful
  16. 16. Resource Caching - VarnishAdvanced setup - requires additional moduleand VCL for Drupal- http://drupal.org/project/varnishGranular ttls (time to live)Specific resource (image/css) and pageelement! All available through VCL config.
  17. 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 (http://drupal.org)Link: </node/5468>; rel="shortlink",<http://badgerfakedomain.com/>; 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. 18. Resource Caching - VarnishCache Stores- RAM- DiskRam - super fast - requires a lot of ramDisk - fast - disk space is rarely an issue
  19. 19. Data Caching - memcachedData and Database caching layerCaches cache...(and things!)
  20. 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 - http://drupal.org/project/memcache and pecl extension
  21. 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. 22. 3rd Party Caching - AkamaiCoverage for DDOS attacks- Active monitoring and defenseAdvanced redirection rules through controlpanelIts not cheap!Its also pretty magical.
  23. 23. 3rd Party Caching - CloudflareSimilar to AkamaiSmaller scaleEmphasis on security and DDOS protection
  24. 24. Content Distribution NetworksCDNs for shortOffload resource handling to external provider- Images- Video- Audio- Large filesSyndicate content around the world
  25. 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. 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. 27. What happens when it all goeswrong?
  28. 28. Caching to the rescueEach layer provides protection3rd Party - shield 99% of anon trafficVarnish - the restAnonymous users should never know your sitehas issues.
  29. 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. 30. Debugging TipsMonitor each layer- Icinga- Pingdom
  31. 31. Any Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×