Cache all the things - A guide to caching Drupal

31
Cache All The Things

description

Presented at nwdug on 09/01/13

Transcript of Cache all the things - A guide to caching Drupal

Page 1: Cache all the things - A guide to caching Drupal

Cache All The Things

Page 2: Cache all the things - A guide to caching Drupal

About Me

Mike BellDrupal Developer

@mikebell_http://drupal.org/user/189605

Page 3: Cache all the things - A guide to caching Drupal

Exactly what things?

erm...

everything!

Page 4: Cache all the things - A guide to caching Drupal

No really...

Frontend:- HTML- CSS- Images- JavascriptBackend:- PHP- MySQL

So pretty much everything then!

Page 5: Cache all the things - A guide to caching Drupal

Where to start?

Ask yourself:Why is my site slow?

a) Bad codeb) Bad serverc) It's not!

Page 6: Cache all the things - A guide to caching Drupal

Bad Code

Stop.

Caching will help... but it won't solve your bigger issues.

There are tools to help - xhprof (not covered here)

Page 7: Cache all the things - A guide to caching Drupal

Bad Server?

So how's £5.99 a month working out for you?

Caching will help... but you still have bigger issues.

Scale horizontally/vertically for a quick fix then work on additional caching.

Page 8: Cache all the things - A guide to caching Drupal

It's not!

I like you!

Caching is always a good thing, except when you have to debug it...

Page 9: Cache all the things - A guide to caching Drupal

Caching in Drupal

- Code Level (cache_get/cache_set)- APC (not covering)- Resource caching (this get's tricky!)- DB Layer- CDNs (related to resource caching)

Page 10: Cache all the things - A guide to caching Drupal

Code Level

Drupal has it's own caching api.

Module Developers use it!

cache_set - store data in Drupals cache tablecache_get - get data from Drupals cache table

You can even define your own cache tables!

Page 11: Cache all the things - A guide to caching Drupal

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)

Page 12: Cache all the things - A guide to caching Drupal

Resource Caching

Many different types of resources

Each can be cached by different tools

Example:- Drupals CSS/JS Cache- Drupals Page Cache- Boost- Varnish

Page 13: Cache all the things - A guide to caching Drupal

Resource Caching

Drupal default: CSS/JS and Page

(taken from digital006.com - ooppss!)

Page 14: Cache all the things - A guide to caching Drupal

Resource Caching - Boost

Boost - http://drupal.org/project/boost

Static caching for all Drupal Assets

Anonymous users only

Ideal for shared hosting environments

Potentially the easiest to setup

Page 15: Cache all the things - A guide to caching Drupal

Resource Caching - Varnish

Application Accelerator

Linux Only

Caches resources based on initial view

Anonymous only - very picky with sessions and cookies

Insanely Powerful

Page 16: Cache all the things - A guide to caching Drupal

Resource Caching - Varnish

Advanced setup - requires additional module and VCL for Drupal- http://drupal.org/project/varnish

Granular ttls (time to live)

Specific resource (image/css) and page element! All available through VCL config.

Page 17: Cache all the things - A guide to caching Drupal

Resource Caching - Varnish

Anatomy of a varnish hit:HTTP/1.1 200 OK

Server: Apache

X-Drupal-Cache: MISS

Expires: Sun, 19 Nov 1978 05:00:00 GMT

Last-Modified: Tue, 08 Jan 2013 21:20:17 +0000

Cache-Control: public, max-age=900

ETag: "1357680017-1"

Content-Language: en

X-Generator: Drupal 7 (http://drupal.org)

Link: </node/5468>; rel="shortlink",<http://badgerfakedomain.com/>; rel="canonical"

Vary: Accept-Encoding

Content-Type: text/html; charset=utf-8

Date: Tue, 08 Jan 2013 21:20:22 GMT

X-Varnish: 979772308 979772303

Age: 3

Via: 1.1 varnish

Connection: keep-alive

X-Cache: HIT

Page 18: Cache all the things - A guide to caching Drupal

Resource Caching - Varnish

Cache Stores- RAM- Disk

Ram - super fast - requires a lot of ramDisk - fast - disk space is rarely an issue

Page 19: Cache all the things - A guide to caching Drupal

Data Caching - memcached

Data and Database caching layer

Caches cache...(and things!)

Page 20: Cache all the things - A guide to caching Drupal

Data Caching - memcachedExample:Core cache - core cache tables (cache_*)

Moved into memcached and shared between multiple instances across multiple servers

Simple api makes getting and setting easy

Drupal requires - http://drupal.org/project/memcache and pecl extension

Page 21: Cache all the things - A guide to caching Drupal

3rd Party Caching - Akamai

One of the biggest caching providers

A high chance you've experienced Akamai 4-5 times today.

Static cache of resourcesContent Distribution Network100+ Edge servers all over the planet

Page 22: Cache all the things - A guide to caching Drupal

3rd Party Caching - Akamai

Coverage for DDOS attacks- Active monitoring and defense

Advanced redirection rules through control panel

It's not cheap!

It's also pretty magical.

Page 23: Cache all the things - A guide to caching Drupal

3rd Party Caching - Cloudflare

Similar to Akamai

Smaller scale

Emphasis on security and DDOS protection

Page 24: Cache all the things - A guide to caching Drupal

Content Distribution Networks

CDNs for short

Offload resource handling to external provider- Images- Video- Audio- Large files

Syndicate content around the world

Page 25: Cache all the things - A guide to caching Drupal

Content Distribution Network

Why use a CDN?- Content is delivered quicker-- Latency cut down

Pricing is granular - pay as you go type deal

Amazon + Rackspace Cloud Files

Page 26: Cache all the things - A guide to caching Drupal

How does it all fit together?

Akamai Load Balancer

Varnish

Varnish

Apache / Nginx

Apache / Nginx

Apache / Nginx

Apache / Nginx

Mysql / memcache

Mysql / memcache

Mysql / memcache

Mysql / memcache

Page 27: Cache all the things - A guide to caching Drupal

What happens when it all goes wrong?

Page 28: Cache all the things - A guide to caching Drupal

Caching to the rescue

Each layer provides protection

3rd Party - shield 99% of anon trafficVarnish - the rest

Anonymous users should never know your site has issues.

Page 29: Cache all the things - A guide to caching Drupal

Debugging Tips

Configure different urls for different platforms- Easier to identify what cache layer has an issue

Don't be afraid to flush caches- Your backend should be able to cope with it

Analyse headers! Drupal and Varnish provide a lot of info

Page 30: Cache all the things - A guide to caching Drupal

Debugging Tips

Monitor each layer- Icinga- Pingdom

Page 31: Cache all the things - A guide to caching Drupal

Any Questions?