Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A taste of C.R.E.A.M. - Cache Rules Everything Around Me - infoShare 2016

A taste of C.R.E.A.M. - Cache Rules Everything Around Me - infoShare 2016

This talk is called "Cache Rules Everything Around Me" and was presented at InfoShare 2016 in Gdansk.

See https://talks.feryn.eu/talks/147/cache-rules-everything-around-me-infoshare-gdansk for more info.

Ca901ddcea38854b9783781c91fc87c9?s=128

Thijs Feryn

May 19, 2016
Tweet

More Decks by Thijs Feryn

Other Decks in Technology

Transcript

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

    TASTE OF
  2. I’m doing an extended version of this talk at 2PM

    on the Tech Plus stage
  3. Hi, I’m Thijs

  4. I’m @ThijsFeryn on Twitter

  5. I’m an Evangelist At

  6. I’m a at board member

  7. Slow websites suck

  8. Web performance is an essential part of the user experience

  9. Infrastructure

  10. Code

  11. Slow database

  12. Browser rendering

  13. User location

  14. Down Slowdown ~ downtime

  15. Identify slowest parts

  16. Optimize code

  17. Optimize database

  18. Optimize runtime

  19. After a while you hit the limits

  20. Optimize database Optimize runtime A void A void

  21. Cache

  22. Don’t recompute if the data hasn’t changed

  23. 3 x 2 = ?

  24. What can you cache? Byte code Database output External services

    Files from disk Pages
  25. Caching is not a compensation for poor code

  26. Caching is an essential architectural strategy

  27. Caching toolkit

  28. ✓Varnish ✓Redis ✓ElasticSearch * Caching toolkit

  29. Quick overview

  30. Varnish

  31. Normally User Server

  32. With Varnish User Varnish Server

  33. None
  34. Stores HTTP output in memory

  35. Respects cache-control headers

  36. Varnish Configuration Language

  37. 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); }
  38. 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); }
  39. ✓Caching ✓Proxying ✓Loadbalancing ✓Edge Side Includes ✓Streaming ✓Compression ✓Invalidation ✓VMODS

    ✓Logging tools Varnish features
  40. It’s all about state

  41. Cookies everywhere

  42. Edge Side Includes

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

    TTL 2s
  44. 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
  45. <?php header("Cache-Control: public,must-revalidate,s-maxage=10"); echo "Date in the ESI tag: ".date('Y-m-d

    H:i:s').'<br />'; <?php header("Cache-Control: no-store"); header(“Surrogate-Control: content='ESI/1.0'"); echo '<esi:include src="/esi.php" />'.PHP_EOL; echo "Date in the main page: ".date('Y-m-d H:i:s').'<br />'; Main page ESI frame: esi.php Cached for 10 seconds Not cached
  46. ESI vs AJAX

  47. HTTP accelerator

  48. None
  49. ✓ Key-value store ✓ Fast ✓ Lightweight ✓ Data stored

    in RAM ✓ ~Memcached ✓ Data types ✓ Data persistance ✓ Replication ✓ Clustering Redis
  50. ✓ Strings ✓ Hashes ✓ Lists ✓ Sets ✓ Sorted

    sets ✓ Geo ✓ … Redis data types
  51. 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
  52. $ 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
  53. $ 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
  54. None
  55. Not really a cache

  56. ✓Full-text search engine ✓Analytics engine ✓NoSQL database ✓Lucene based ✓Built-in

    clustering, replication, sharding ✓RESTful interface ✓JSON output ✓Schemaless ElasticSearch
  57. Fast retrieval Fast search All REST

  58. 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" }
  59. 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
  60. POST /blog/post/_search { "fields": ["title"], "query": { "match": { "title":

    "working" } } }
  61. What can we cache (reminder) Byte code Database output External

    services Files from disk Pages
  62. Where does all of this fit in?

  63. ✓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?
  64. ✓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?
  65. ✓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?
  66. None
  67. https://blog.feryn.eu https://talks.feryn.eu https://youtube.com/thijsferyn https://soundcloud.com/thijsferyn https://twitter.com/thijsferyn http://itunes.feryn.eu