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

I'M @THIJSFERYN

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

WE'RE HOSTING A MEETUP ✓ THURSDAY JULY 25TH ✓ 6PM - 9PM ✓ BARCADE, CHELSEA, NYC ✓ RSVP ! https://info.varnish-software.com/varnish-nyc-meetup-2019

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

DON’T RECOMPUTE IF THE DATA HASN’T CHANGED

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

REVERSE CACHING PROXY

Slide 26

Slide 26 text

NORMALLY USER SERVER

Slide 27

Slide 27 text

WITH REVERSE CACHING PROXY USER PROXY SERVER

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 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 32

Slide 32 text

IN AN IDEAL WORLD

Slide 33

Slide 33 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 34

Slide 34 text

REALITY SUCKS

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

TIME TO LIVE

Slide 38

Slide 38 text

CACHE VARIATIONS

Slide 39

Slide 39 text

LEGACY

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

CACHING STATE OF MIND ✓ PORTABILITY ✓ DEVELOPER EMPOWERMENT ✓ CONTROL ✓ CONSISTENT CACHING BEHAVIOR

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

CACHE-CONTROL

Slide 44

Slide 44 text

Cache-Control: public, s-maxage=500

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

Cache-Control: private, no-store

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

CONDITIONAL REQUESTS

Slide 49

Slide 49 text

ONLY FETCH PAYLOAD THAT HAS CHANGED

Slide 50

Slide 50 text

HTTP/1.1 200 OK

Slide 51

Slide 51 text

OTHERWISE: HTTP/1.1 304 NOT MODIFIED

Slide 52

Slide 52 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 53

Slide 53 text

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

Slide 54

Slide 54 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 55

Slide 55 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 56

Slide 56 text

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

Slide 57

Slide 57 text

QUICKLY

Slide 58

Slide 58 text

EARLY

Slide 59

Slide 59 text

Store & retrieve Etag

Slide 60

Slide 60 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 61

Slide 61 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 62

Slide 62 text

CONTENT COMPOSITION & PLACEHOLDERS

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Shopping cart or account information

Slide 65

Slide 65 text

SESSION COOKIE NO CACHE

Slide 66

Slide 66 text

CODE RENDERS SINGLE HTTP RESPONSE

Slide 67

Slide 67 text

LOWEST COMMON DENOMINATOR: NO CACHE

Slide 68

Slide 68 text

PLACEHOLDERS

Slide 69

Slide 69 text

AJAX

Slide 70

Slide 70 text

Non-cached AJAX call

Slide 71

Slide 71 text

EDGE SIDE INCLUDES

Slide 72

Slide 72 text

EDGE SIDE INCLUDES ✓ PLACEHOLDER ✓ W3C STANDARD ✓ PROCESSED "ON THE EDGE" (E.G. VARNISH) ✓ OUTPUT IS A COMPOSITION OF BLOCKS ✓ STATE PER BLOCK ✓ TTL PER BLOCK

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

Non-cached ESI placeholder

Slide 75

Slide 75 text

ESI VS AJAX

Slide 76

Slide 76 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 77

Slide 77 text

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

Slide 78

Slide 78 text

COMPOSITION AT THE VIEW LAYER

Slide 79

Slide 79 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 80

Slide 80 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 81

Slide 81 text

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

Slide 82

Slide 82 text

CACHE VARIATIONS

Slide 83

Slide 83 text

HOW DO YOU IDENTIFY AN OBJECT IN CACHE?

Slide 84

Slide 84 text

THE URL IDENTIFIES THE OBJECT IN CACHE

Slide 85

Slide 85 text

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

Slide 86

Slide 86 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 87

Slide 87 text

Vary: Accept-Language Request header value Response header

Slide 88

Slide 88 text

WHAT IF YOU CAN'T LEVERAGE HTTP?

Slide 89

Slide 89 text

WRITE VCL CODE

Slide 90

Slide 90 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 91

Slide 91 text

vcl 4.0; 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); } } } Cookie example

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

VARNISH ENTERPRISE ✓ CLIENT SSL TERMINATION ✓ BACKEND SSL CONNECTIONS ✓ PARALLEL ESI ✓ MASSIVE STORAGE ENGINE ✓ ENCRYPTION ✓ THROTTLING ✓ RATE LIMITING ✓ PREFETCHING ✓ GEOLOCATION ✓ AUTHENTICATION ✓ EDGESTASH ✓ CUSTOM STATISTICS ✓ ADMIN MODULE ✓ SUPPORT ✓ HIGH AVAILABILITY

Slide 94

Slide 94 text

EDGE CONTENT TRANSFORMATION

Slide 95

Slide 95 text

HTTP/1.1 200 OK Accept-Ranges: bytes Age: 11 Cache-Control: max-age=30, public, s-maxage=100 Content-Type: text/html; charset=UTF-8 Date: Thu, 11 Jul 2019 13:47:57 GMT X-Varnish: 262420 262415 Hello Thijs Hello {{username}} USER VARNISH ENTERPRISE ORIGIN GET /liked HTTP/1.1 Cookie:PHPSESSID=21779b3a7a75fca1851954b9810faa26 GET sf_s21779b3a7a75fca1851954b9810faa26

Slide 96

Slide 96 text

HTTPS://AWS.AMAZON.COM/QUICKSTART/ARCHITECTURE/VARNISH/

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

WE'RE HOSTING A MEETUP ✓ THURSDAY JULY 25TH ✓ 6PM - 9PM ✓ BARCADE, CHELSEA, NYC ✓ RSVP ! https://info.varnish-software.com/varnish-nyc-meetup-2019

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

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