Slide 1

Slide 1 text

C.R.E.A.M CASH RULES EVERYTHING AROUND ME CACHE Thijs Feryn A TASTE OF

Slide 2

Slide 2 text

I’m doing an extended version of this talk at 2PM on the Tech Plus stage

Slide 3

Slide 3 text

Hi, I’m Thijs

Slide 4

Slide 4 text

I’m @ThijsFeryn on Twitter

Slide 5

Slide 5 text

I’m an Evangelist At

Slide 6

Slide 6 text

I’m a at board member

Slide 7

Slide 7 text

Slow websites suck

Slide 8

Slide 8 text

Web performance is an essential part of the user experience

Slide 9

Slide 9 text

Infrastructure

Slide 10

Slide 10 text

Code

Slide 11

Slide 11 text

Slow database

Slide 12

Slide 12 text

Browser rendering

Slide 13

Slide 13 text

User location

Slide 14

Slide 14 text

Down Slowdown ~ downtime

Slide 15

Slide 15 text

Identify slowest parts

Slide 16

Slide 16 text

Optimize code

Slide 17

Slide 17 text

Optimize database

Slide 18

Slide 18 text

Optimize runtime

Slide 19

Slide 19 text

After a while you hit the limits

Slide 20

Slide 20 text

Optimize database Optimize runtime A void A void

Slide 21

Slide 21 text

Cache

Slide 22

Slide 22 text

Don’t recompute if the data hasn’t changed

Slide 23

Slide 23 text

3 x 2 = ?

Slide 24

Slide 24 text

What can you cache? Byte code Database output External services Files from disk Pages

Slide 25

Slide 25 text

Caching is not a compensation for poor code

Slide 26

Slide 26 text

Caching is an essential architectural strategy

Slide 27

Slide 27 text

Caching toolkit

Slide 28

Slide 28 text

✓Varnish ✓Redis ✓ElasticSearch * Caching toolkit

Slide 29

Slide 29 text

Quick overview

Slide 30

Slide 30 text

Varnish

Slide 31

Slide 31 text

Normally User Server

Slide 32

Slide 32 text

With Varnish User Varnish Server

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Stores HTTP output in memory

Slide 35

Slide 35 text

Respects cache-control headers

Slide 36

Slide 36 text

Varnish Configuration Language

Slide 37

Slide 37 text

sub vcl_recv { if (req.method == "PRI") { /* We do not support SPDY or HTTP/2.0 */ return (synth(405)); } if (req.method != "GET" && req.method != "HEAD" && req.method != "PUT" && req.method != "POST" && req.method != "TRACE" && req.method != "OPTIONS" && req.method != "DELETE") { /* Non-RFC2616 or CONNECT which is weird. */ return (pipe); } if (req.method != "GET" && req.method != "HEAD") { /* We only deal with GET and HEAD by default */ return (pass); } if (req.http.Authorization || req.http.Cookie) { /* Not cacheable by default */ return (pass); } return (hash); }

Slide 38

Slide 38 text

sub vcl_hash { hash_data(req.url); if (req.http.host) { hash_data(req.http.host); } else { hash_data(server.ip); } return (lookup); } sub vcl_backend_response { if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Surrogate-control ~ "no-store" || (!beresp.http.Surrogate-Control && beresp.http.Cache-Control ~ "no-cache|no-store|private") || beresp.http.Vary == "*") { /* * Mark as "Hit-For-Pass" for the next 2 minutes */ set beresp.ttl = 120s; set beresp.uncacheable = true; } return (deliver); }

Slide 39

Slide 39 text

✓Caching ✓Proxying ✓Loadbalancing ✓Edge Side Includes ✓Streaming ✓Compression ✓Invalidation ✓VMODS ✓Logging tools Varnish features

Slide 40

Slide 40 text

It’s all about state

Slide 41

Slide 41 text

Cookies everywhere

Slide 42

Slide 42 text

Edge Side Includes

Slide 43

Slide 43 text

header.php menu.php main.php footer.php TTL 5s No caching TTL 10s TTL 2s

Slide 44

Slide 44 text

sub vcl_recv { set req.http.Surrogate-Capability = "key=ESI/1.0"; } sub vcl_backend_response { if (beresp.http.Surrogate-Control ~ "ESI/1.0") { unset beresp.http.Surrogate-Control; set beresp.do_esi = true; } } Edge Side Includes

Slide 45

Slide 45 text

'; '.PHP_EOL; echo "Date in the main page: ".date('Y-m-d H:i:s').'
'; Main page ESI frame: esi.php Cached for 10 seconds Not cached

Slide 46

Slide 46 text

ESI vs AJAX

Slide 47

Slide 47 text

HTTP accelerator

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

✓ Key-value store ✓ Fast ✓ Lightweight ✓ Data stored in RAM ✓ ~Memcached ✓ Data types ✓ Data persistance ✓ Replication ✓ Clustering Redis

Slide 50

Slide 50 text

✓ Strings ✓ Hashes ✓ Lists ✓ Sets ✓ Sorted sets ✓ Geo ✓ … Redis data types

Slide 51

Slide 51 text

Redis $ redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379> set mykey somevalue OK 127.0.0.1:6379> get mykey "somevalue

Slide 52

Slide 52 text

$ redis-cli 127.0.0.1:6379> hset customer_1234 id 1234 (integer) 1 127.0.0.1:6379> hset customer_1234 items_in_cart 2 (integer) 1 127.0.0.1:6379> hmset customer_1234 firstname Thijs lastname Feryn OK 127.0.0.1:6379> hgetall customer_1234 1) "id" 2) "1234" 3) "items_in_cart" 4) "2" 5) "firstname" 6) "Thijs" 7) "lastname" 8) "Feryn" 127.0.0.1:6379> Redis

Slide 53

Slide 53 text

$ redis-cli 127.0.0.1:6379> lpush products_for_customer_1234 5 (integer) 1 127.0.0.1:6379> lpush products_for_customer_1234 345 (integer) 2 127.0.0.1:6379> lpush products_for_customer_1234 78 12 345 (integer) 5 127.0.0.1:6379> llen products_for_customer_1234 (integer) 5 127.0.0.1:6379> lindex products_for_customer_1234 1 "12" 127.0.0.1:6379> lindex products_for_customer_1234 2 "78" 127.0.0.1:6379> rpop products_for_customer_1234 "5" 127.0.0.1:6379> rpop products_for_customer_1234 "345" 127.0.0.1:6379> rpop products_for_customer_1234 "78" 127.0.0.1:6379> rpop products_for_customer_1234 "12" 127.0.0.1:6379> rpop products_for_customer_1234 "345" 127.0.0.1:6379> rpop products_for_customer_1234 (nil) 127.0.0.1:6379> Redis

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Not really a cache

Slide 56

Slide 56 text

✓Full-text search engine ✓Analytics engine ✓NoSQL database ✓Lucene based ✓Built-in clustering, replication, sharding ✓RESTful interface ✓JSON output ✓Schemaless ElasticSearch

Slide 57

Slide 57 text

Fast retrieval Fast search All REST

Slide 58

Slide 58 text

POST/blog/post/6160 { "language": "en-US", "title": "WordPress 4.4 is available! And these are the new features…", "date": "Tue, 15 Dec 2015 13:28:23 +0000", "author": "Romy", "category": [ "News", "PHP", "Sector news", "Webdesign & development", "CMS", "content management system", "wordpress", "WordPress 4.4" ], "guid": "6160" }

Slide 59

Slide 59 text

GET /blog/post/6160 { "_index": "blog", "_type": "post", "_id": "6160", "_version": 1, "found": true, "_source": { "language": "en-US", "title": "WordPress 4.4 is available! And these are the new features…", "date": "Tue, 15 Dec 2015 13:28:23 +0000", "author": "Romy", "category": [ "News", "PHP", "Sector news", "Webdesign & development", "CMS", "content management system", "wordpress", "WordPress 4.4" ], "guid": "6160" } } Retrieve document by id Document & meta data

Slide 60

Slide 60 text

POST /blog/post/_search { "fields": ["title"], "query": { "match": { "title": "working" } } }

Slide 61

Slide 61 text

What can we cache (reminder) Byte code Database output External services Files from disk Pages

Slide 62

Slide 62 text

Where does all of this fit in?

Slide 63

Slide 63 text

✓Cache all images, js, css, woff, … ✓Cache dynamic pages ✓ESI or AJAX for user-specific content ✓Sanitize HTTP input/output ✓Gateway to your application Where does Varnish fit in?

Slide 64

Slide 64 text

✓Secondary database (NoSQL) ✓RDBMS can remain the source of truth ✓Store in fixed format (no joins) ✓Full-text search ✓Fast retrieval of data projections ✓Aggregations Where does ElasticSearch fit in?

Slide 65

Slide 65 text

✓Real-time information ✓Key-value gets, not searches ✓Volatile data ✓When data changes a lot ✓RDMBS is still source of truth Where does Redis fit in?

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

https://blog.feryn.eu https://talks.feryn.eu https://youtube.com/thijsferyn https://soundcloud.com/thijsferyn https://twitter.com/thijsferyn http://itunes.feryn.eu