Slide 1

Slide 1 text

DEVELOPING CACHEABLE PHP APPLICATIONS Thijs Feryn

Slide 2

Slide 2 text

Slow websites SUCK

Slide 3

Slide 3 text

WEB PERFORMANCE IS AN ESSENTIAL PART OF THE USER EXPERIENCE

Slide 4

Slide 4 text

SLOW ~ DOWN

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

THROWING SERVERS AT THE PROBLEM

Slide 9

Slide 9 text

MO' MONEY MO' SERVERS MO' PROBLEMS

Slide 10

Slide 10 text

IDENTIFY SLOWEST PARTS

Slide 11

Slide 11 text

OPTIMIZE

Slide 12

Slide 12 text

AFTER A WHILE YOU HIT THE LIMITS

Slide 13

Slide 13 text

CACHE

Slide 14

Slide 14 text

HI, I'M THIJS

Slide 15

Slide 15 text

I'M AN EVANGELIST AT

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

4,800,000 WEBSITES 19% OF THE TOP 10K WEBSITES

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

I'M @THIJSFERYN

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

USER SERVER

Slide 23

Slide 23 text

REVERSE CACHING PROXY

Slide 24

Slide 24 text

USER PROXY SERVER

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

HTTP CACHING MECHANISMS Expires: Sat, 23 July 2019 00:30:00 GMT Cache-control: public, max-age=3600, s-maxage=86400 Cache-control: private, no-cache, no-store

Slide 28

Slide 28 text

IN AN IDEAL WORLD

Slide 29

Slide 29 text

✓STATELESS ✓WELL-DEFINED TTL ✓CACHE / NO-CACHE PER RESOURCE ✓CACHE VARIATIONS ✓CONDITIONAL REQUESTS ✓PLACEHOLDERS FOR NON-CACHEABLE CONTENT ✓EDGE-SIDE LOGIC FOR PERSONALIZED CACHING IN AN IDEAL WORLD

Slide 30

Slide 30 text

REALITY SUCKS

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

FOR YOUR EYES ONLY NOT CACHED

Slide 33

Slide 33 text

TIME TO LIVE

Slide 34

Slide 34 text

CACHING PURGING LOGIC

Slide 35

Slide 35 text

CACHE VARIATIONS

Slide 36

Slide 36 text

LEGACY

Slide 37

Slide 37 text

WHAT IF WE COULD DESIGN OUR SOFTWARE WITH HTTP CACHING IN MIND?

Slide 38

Slide 38 text

CACHE-CONTROL

Slide 39

Slide 39 text

Cache-Control: public, s-maxage=500

Slide 40

Slide 40 text

render('index.twig') ->setSharedMaxAge(500) ->setPublic(); } }

Slide 41

Slide 41 text

Cache-Control: private, no-store

Slide 42

Slide 42 text

/** * @Route("/private", name="private") */ public function private() { $response = $this ->render('private.twig') ->setPrivate(); $response->headers->addCacheControlDirective('no-store'); return $response; }

Slide 43

Slide 43 text

CONDITIONAL REQUESTS

Slide 44

Slide 44 text

ONLY FETCH PAYLOAD THAT HAS CHANGED

Slide 45

Slide 45 text

HTTP/1.1 200 OK

Slide 46

Slide 46 text

OTHERWISE: HTTP/1.1 304 NOT MODIFIED

Slide 47

Slide 47 text

CONDITIONAL REQUESTS HTTP/1.1 200 OK Host: localhost Etag: 7c9d70604c6061da9bb9377d3f00eb27 Content-type: text/html; charset=UTF-8 Hello world output GET / HTTP/1.1 Host: localhost

Slide 48

Slide 48 text

CONDITIONAL REQUESTS HTTP/1.1 304 Not Modified Host: localhost Etag: 7c9d70604c6061da9bb9377d3f00eb27 GET / HTTP/1.1 Host: localhost If-None-Match: 7c9d70604c6061da9bb9377d3f00eb27

Slide 49

Slide 49 text

CONDITIONAL REQUESTS HTTP/1.1 200 OK Host: localhost Last-Modified: Fri, 22 Jul 2016 10:11:16 GMT Content-type: text/html; charset=UTF-8 Hello world output GET / HTTP/1.1 Host: localhost

Slide 50

Slide 50 text

CONDITIONAL REQUESTS HTTP/1.1 304 Not Modified Host: localhost Last-Modified: Fri, 22 Jul 2016 10:11:16 GMT GET / HTTP/1.1 Host: localhost If-Last-Modified: Fri, 22 Jul 2016 10:11:16 GMT

Slide 51

Slide 51 text

VARNISH SUPPORTS CONDITIONAL REQUESTS FOR CLIENTS & BACKENDS

Slide 52

Slide 52 text

AND WILL SERVE STALE DATA WHILE IT ASYNCHRONOUSLY REVALIDATES CONTENT

Slide 53

Slide 53 text

Cache-Control: public, max-age=100, s-maxage=500, stale-while-revalidate=20

Slide 54

Slide 54 text

QUICKLY

Slide 55

Slide 55 text

EARLY

Slide 56

Slide 56 text

Store & retrieve Etag

Slide 57

Slide 57 text

redis = $redis; } protected function isModified(Request $request, $etag) { if ($etags = $request->getETags()) { return in_array($etag, $etags) || in_array('*', $etags); } return true; } ... src/EventListener/ConditionalRequestListener.php

Slide 58

Slide 58 text

{ $this->redis = $redis; } protected function isModified(Request $request, $etag) { if ($etags = $request->getETags()) { return in_array($etag, $etags) || in_array('*', $etags); } return true; } public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); $etag = $this->redis->get('etag:'.md5($request->getUri())); if(!$this->isModified($request,$etag)) { $event->setResponse(Response::create('Not Modified',Response::HTTP_NOT_MODIFIED)); } } public function onKernelResponse(FilterResponseEvent $event) { $response = $event->getResponse(); $request = $event->getRequest(); $etag = md5($response->getContent()); $response->setEtag($etag); if($this->isModified($request,$etag)) { $this->redis->set('etag:'.md5($request->getUri()),$etag); } } } src/EventListener/ConditionalRequestListener.php

Slide 59

Slide 59 text

CONTENT COMPOSITION & PLACEHOLDERS

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

NO CACHE

Slide 62

Slide 62 text

PLACEHOLDERS

Slide 63

Slide 63 text

SEPARATE HTTP REQUEST

Slide 64

Slide 64 text

AJAX

Slide 65

Slide 65 text

✓CLIENT-SIDE ✓COMMON KNOWLEDGE ✓PARALLEL PROCESSING ✓GRACEFUL DEGRADATION -PROCESSED BY THE BROWSER -EXTRA ROUNDTRIPS -SOMEWHAT SLOWER AJAX

Slide 66

Slide 66 text

EDGE-SIDE INCLUDES ESI

Slide 67

Slide 67 text

Slide 68

Slide 68 text

ESI ✓ PLACEHOLDER ✓ PARSED BY VARNISH ✓ OUTPUT IS A COMPOSITION OF BLOCKS ✓ STATE PER BLOCK ✓ TTL PER BLOCK

Slide 69

Slide 69 text

VARNISH Surrogate-Capability: key="ESI/1.0" Surrogate-Control: content="ESI/1.0" BACKEND Parse ESI placeholders VARNISH

Slide 70

Slide 70 text

WRITE SOME VCL CODE FOR AUTOMATIC ESI DETECTION

Slide 71

Slide 71 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; } }

Slide 72

Slide 72 text

EDGE SIDE INCLUDES ✓ SERVER-SIDE ✓ STANDARDIZED ✓ PROCESSED ON THE “EDGE”, NOT IN THE BROWSER ✓ GENERALLY FASTER - SEQUENTIAL (ONLY PARALLEL IN ENTERPRISE VERSION) - ONE FAILS, ALL FAIL - LIMITED IMPLEMENTATION IN VARNISH

Slide 73

Slide 73 text

ESI VS AJAX

Slide 74

Slide 74 text

COMPOSITION AT THE VIEW LAYER

Slide 75

Slide 75 text

/** * @Route("/", name="home") */ public function index() { return $this ->render('index.twig') ->setPublic() ->setSharedMaxAge(500); } /** * @Route("/header", name="header") */ public function header() { $response = $this ->render('header.twig') ->setPrivate(); $response->headers->addCacheControlDirective('no-store'); return $response; } /** * @Route("/footer", name="footer") */ public function footer() { $response = $this->render('footer.twig'); $response ->setSharedMaxAge(500) ->setPublic(); return $response; } /** * @Route("/nav", name="nav") */ public function nav() { $response = $this->render('nav.twig'); $response ->setVary('X-Login',false) ->setSharedMaxAge(500) ->setPublic(); return $response; } Controller action per fragment

Slide 76

Slide 76 text

{{ include('header.twig') }}
{{ include('nav.twig') }}
{% block content %}{% endblock %}
{{ include('footer.twig') }}
{{ render_esi(url('header')) }}
{{ render_esi(url('nav')) }}
{% block content %}{% endblock %}
{{ render_esi(url('footer')) }}

Slide 77

Slide 77 text

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris consequat orci eget libero sollicitudin,…

Slide 78

Slide 78 text

CACHE VARIATIONS

Slide 79

Slide 79 text

HOW DO YOU IDENTIFY AN OBJECT IN CACHE?

Slide 80

Slide 80 text

THE URL IDENTIFIES THE OBJECT IN CACHE

Slide 81

Slide 81 text

WHAT IF THE CONTENT OF A URL VARIES BASED ON THE VALUE OF A REQUEST HEADER?

Slide 82

Slide 82 text

CACHE VARIATIONS HTTP/1.1 200 OK Host: localhost Content-Language: en Content-type: text/html; charset=UTF-8 Hello world output GET / HTTP/1.1 Host: localhost Accept-Language: en, nl, de

Slide 83

Slide 83 text

Vary: Accept-Language Request header value Response header

Slide 84

Slide 84 text

WHAT IF YOU CAN'T LEVERAGE HTTP?

Slide 85

Slide 85 text

WRITE VCL CODE

Slide 86

Slide 86 text

sub vcl_recv { if (req.url ~ "^/status\.php$" || req.url ~ "^/update\.php$" || req.url ~ "^/admin$" || req.url ~ "^/admin/.*$" || req.url ~ "^/flag/.*$" || req.url ~ "^.*/ajax/.*$" || req.url ~ "^.*/ahah/.*$") { return (pass); } } URL BLACKLIST EXAMPLE

Slide 87

Slide 87 text

sub vcl_recv { if (req.http.Cookie) { set req.http.Cookie = ";" + req.http.Cookie; set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";"); set req.http.Cookie = regsuball(req.http.Cookie, ";(SESS[a-z0-9]+|SSESS[a-z0-9]+|NO_CACHE)=", "; \1="); set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", ""); set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", ""); if (req.http.Cookie == "") { unset req.http.Cookie; } else { return (pass); } } } ONLY KEEP CERTAIN COOKIES

Slide 88

Slide 88 text

NON-CACHEABLE ROUTES WILL STILL BE A TARGET FOR LOAD/LATENCY

Slide 89

Slide 89 text

CACHING PERSONALIZED DATA

Slide 90

Slide 90 text

DECISION MAKING AT THE EDGE

Slide 91

Slide 91 text

SYNTHETIC HTTP HTTP OUTPUT THAT DIDN'T ORIGINATE FROM THE BACKEND SERVER

Slide 92

Slide 92 text

{USERNAME: "THIJS"} GENERATED IN VCL BASED ON REDIS DATA

Slide 93

Slide 93 text

vcl 4.1; import cookieplus; import redis; import synthbackend; sub vcl_init { new db = redis.db( location="redis:6379", type=master, connection_timeout=500, shared_connections=false, max_connections=1); } sub vcl_recv { set req.http.X-Login = "false"; set req.http.x-session = cookieplus.get("PHPSESSID","guest"); if(req.http.x-session != "guest") { db.command("EXISTS"); db.push("sf_s"+cookieplus.get("PHPSESSID")); db.execute(); if(db.get_integer_reply() == 1) { set req.http.X-Login = "true"; } } } SYNTHETIC HTTP

Slide 94

Slide 94 text

sub vcl_backend_fetch { if(bereq.url == "/session") { if(bereq.http.X-Login != "true") { set bereq.backend = synthbackend.from_string("{}"); return(fetch); } db.command("EVAL"); db.push({" local session = redis.call('GET', KEYS[1]) if session == nil then return '{}' end local result = string.gsub(session, '[%c]', '') local username = string.gsub(result,'.+Userusername\";s:[0-9]+:\"([^\"]+)\";.+','%1') if username == nil then return '{}' end return '{"username":"'.. username ..'"}' "}); db.push(1); db.push("sf_s"+cookieplus.get("PHPSESSID")); db.execute(); set bereq.backend = synthbackend.from_string(db.get_string_reply()); } } sub vcl_backend_response { if(bereq.url == "/session") { set beresp.http.Content-Type = "application/json; charset=utf-8"; set beresp.ttl = 3600s; set beresp.http.vary = "x-session"; } } REDIS LUA CODE

Slide 95

Slide 95 text

EDGESTASH VARNISH MODULE FOR MUSTACHE PROCESSING ON THE EDGE

Slide 96

Slide 96 text

{{ USERNAME }} VARNISH MODULE FOR MUSTACHE PROCESSING ON THE EDGE REPLACES PLACEHOLDERS WITH JSON VALUES

Slide 97

Slide 97 text

Surrogate-Control: edgestash="EDGESTASH/2.1" Link: ; rel=edgestash Welcome {{ username }} Welcome Thijs + =

Slide 98

Slide 98 text

vcl 4.1; import edgestash; import std; backend default { .host = "1.1.1.1"; } sub vcl_recv { set req.http.Surrogate-Capability={"edgestash="EDGESTASH/2.1""}; } sub vcl_backend_response { if(beresp.http.Link) { std.collect(beresp.http.Link,","); } if(beresp.http.Link ~ "<([^>]+)>; rel=edgestash") { set beresp.http.x-edgestash-json-urls = regsuball(beresp.http.Link,"(?(?=<[^>]+>; rel=edgestash)<([^>]+)>; rel=edgestash|<([^>]+)>; rel=[a-z]+, )","\1"); } if(beresp.http.Surrogate-Control) { std.collect(beresp.http.Surrogate-Control); } if(beresp.http.Surrogate-Control ~ {".*="EDGESTASH/2\.[0-9]+".*"}) { edgestash.parse_response(); } } EDGESTASH

Slide 99

Slide 99 text

sub vcl_deliver { if(edgestash.is_edgestash() && resp.http.x-edgestash-json-urls) { edgestash.add_json_url_csv(resp.http.x-edgestash-json-urls); edgestash.execute(); } unset resp.http.Link; unset resp.http.x-edgestash-json-urls; unset resp.http.surrogate-control; } EDGESTASH

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

COMPOSER REQUIRE THIJSFERYN/EDGESTASH-TWIG-BUNDLE

Slide 102

Slide 102 text

{{ edgestash('username','/session') }}
{{ username | edgestash('username','/session') }}

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

14 DAYS FREE TRIAL

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

HTTPS://FERYN.EU HTTPS://TWITTER.COM/THIJSFERYN HTTPS://INSTAGRAM.COM/THIJSFERYN