Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

WHAT WE'LL COVER ✅ HTTP Caching Basics ✅ Expiration And Validation ✅ Caching With Symfony ✅ Edge side includes

Slide 3

Slide 3 text

OUT OF SCOPE ❌ Varnish, Squid etc. ❌ Application Caching ❌ Specific Techniques (eg Russian Doll Caching)

Slide 4

Slide 4 text

WHO DIS? ! Marco Petersen " Software Developer @ SensioLabs # @ocrampete16

Slide 5

Slide 5 text

ONCE UPON A CACHE

Slide 6

Slide 6 text

HOW TO MAKE A WEB APPLICATION FASTER > Optimize database queries > Use more efficient algorithms > Rewrite Your Backend in a faster language > . . .

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

SYMFONY — HTTP* * the HTTP/1.1 specification (RFC2616)

Slide 9

Slide 9 text

TYPES OF CACHES > Browser Caches > Chrome, Firefox > Proxy Caches > Squid > Gateway Caches > Varnish, Nginx, Squid, Cloudflare

Slide 10

Slide 10 text

Without Caching

Slide 11

Slide 11 text

Browser Caches cache the sites you surf

Slide 12

Slide 12 text

Proxy Caches cache incoming traffic

Slide 13

Slide 13 text

Gateway Caches cache outgoing traffic

Slide 14

Slide 14 text

LET'S GET CACHING

Slide 15

Slide 15 text

CACHING MODELS > Expiration > Expires > Cache-Control > Validation > ETag / If-None-Match > Last-Modified / If-Modified-Since

Slide 16

Slide 16 text

EXPIRATION

Slide 17

Slide 17 text

THE GAME PLAN 1. Client requests a resource and receives a response. 2. Cache intercepts and stores resouce with expiration date. 3. Client requests same resource. 4. Cached resource is returned if it is still fresh. > The server is not hit.

Slide 18

Slide 18 text

EXPIRES SPECIFIES A SPECIFIC DATE AND TIME AFTER WHICH THE RESOURCE BECOMES STALE.

Slide 19

Slide 19 text

/** * @Cache(expires="+600 seconds") */ public function bar() { return $response; } // or public function foo() { $response->setExpires(new \DateTime('+600 seconds')); return $response; }

Slide 20

Slide 20 text

HTTP/1.1 200 OK Expires: Wed, 21 Oct 2015 07:28:00 GMT Lorem ipsum dolor sit amet

Slide 21

Slide 21 text

CACHE-CONTROL THE MULTI-TALENT! > defines several caching directives > for now, we're only interested in max-age and s-maxage

Slide 22

Slide 22 text

MAX-AGE AND S-MAXAGE ARE LIKE EXPIRES, BUT THEY SPECIFY THE NUMBER OF SECONDS UNTIL A RESOURCE BECOMES STALE

Slide 23

Slide 23 text

/** * @Cache(smaxage=600) */ public function bar() { return $response; } // or public function foo() { $response->setSharedMaxAge(600); return $response; }

Slide 24

Slide 24 text

HTTP/1.1 200 OK Cache-Control: s-maxage=600 Lorem ipsum dolor sit amet

Slide 25

Slide 25 text

MAX-AGE AND S-MAXAGE > Browser caches only look for max-age. > Shared caches look for s-maxage if it exists, otherwise it follows max-age.

Slide 26

Slide 26 text

VALIDATION

Slide 27

Slide 27 text

THE GAME PLAN 1. Client requests a resource. 2. Server returns a response with validation information. 3. Client requests same resource with the validation information. 4. 304 (Not Modified) is returned if the resource is still valid. > The server is hit, but the response is much smaller.

Slide 28

Slide 28 text

ETAG?

Slide 29

Slide 29 text

AN ETAG IS A STRING THAT REPRESENTS A SPECIFIC VERSION OF A RESOURCE.

Slide 30

Slide 30 text

/** * @Cache(Etag="post.getId() ~ post.getUpdatedAt().getTimestamp()") */ public function bar(Post $post) { return $response; } // or public function foo(Request $request) { $reponse = new Response(); // Compute the ETag $response->setEtag($etag) if ($response->isNotModified($request)) { return $response; } return $this->render('foo.html.twig', [], $response); }

Slide 31

Slide 31 text

// server responds HTTP/1.1 200 OK ETag: "qwerasdfyxcv" Lorem ipsum dolor sit amet // client requests same resource again GET /foo HTTP/1.1 If-None-Match: "qwerasdfyxcv" // server responds HTTP/1.1 304 NOT MODIFIED

Slide 32

Slide 32 text

LAST-MODIFIED SPECIFIES WHEN A RESOURCE WAS LAST MODIFIED

Slide 33

Slide 33 text

/** * @Cache(lastModified="post.getUpdatedAt()") */ public function bar(Post $post) { return $response; } // or public function foo(Request $request, Post $post) { $reponse = new Response(); $response->setLastModified($post->getUpdatedAt()) if ($response->isNotModified($request)) { return $response; } return $this->render('foo.html.twig', [], $response); }

Slide 34

Slide 34 text

// server responds HTTP/1.1 200 OK Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT Lorem ipsum dolor sit amet // client requests same resource again GET /foo HTTP/1.1 If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT // server responds HTTP/1.1 304 NOT MODIFIED

Slide 35

Slide 35 text

CAN'T DECIDE WHICH MODEL TO PICK?

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

BOTH WORK WELL TOGETHER, ALTHOUGH EXPIRATION TRUMPS VALIDATION. // max_age > etag $response->setCache([ 'max_age' => 20, 'etag' => $etag, ]);

Slide 38

Slide 38 text

NEED A GATEWAY CACHE FOR DEVELOPMENT? NO PROBLEM!

Slide 39

Slide 39 text

// public/index.php use App\Kernel; use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache; // ... $kernel = new Kernel($env, $debug); if ('prod' === $env) { $kernel = new HttpCache($kernel); }

Slide 40

Slide 40 text

// public/index.php use App\Kernel; use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache; // ... $kernel = new Kernel($env, $debug); if ('prod' === $env) { $kernel = new HttpCache($kernel); }

Slide 41

Slide 41 text

// src/CacheKernel.php namespace App; use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache; final class CacheKernel extends HttpCache { protected function getOptions(): array { return [ 'default_ttl' => 0, ]; } }

Slide 42

Slide 42 text

// src/CacheKernel.php namespace App; use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache; final class CacheKernel extends HttpCache { protected function getOptions(): array { return [ 'default_ttl' => 0, ]; } }

Slide 43

Slide 43 text

DEMO TIME

Slide 44

Slide 44 text

RECAP

Slide 45

Slide 45 text

RECAP > Expiration asks "When does it become stale?" > Expires specifies a future point in time > Cache-Control is like Expires, but relative > Validation asks "Is it still fresh?" > Etag specifies an identifier to compare > Last-Modified specifies a point in time in the past > All work well with Symfony!

Slide 46

Slide 46 text

EDGE SIDE INCLUDES

Slide 47

Slide 47 text

DEMO TIME

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

FINAL RECAP

Slide 50

Slide 50 text

FINAL RECAP > Cache to bypass your application. > Expiration - "When does this expire?" > Validation - "Is this still valid?" > ESI caches fragments of pages separately.

Slide 51

Slide 51 text

WHERE TO GO FROM HERE > Set up your own gateway cache > Read the HTTP/1.1 spec (RFC-2616) > Check out HTTP/2's new features (RFC-7540) > Server Push > Multiplexing

Slide 52

Slide 52 text

$kernel->terminate($request, $response);

Slide 53

Slide 53 text

No content