Download pdf - HTTP caching with Varnish

Transcript
Page 1: HTTP caching with Varnish

HTTPcaching

withVarnish

DPC,June27th2015

DavidBuchmann&DaviddeBoer

Page 2: HTTP caching with Varnish
Page 3: HTTP caching with Varnish

ThisthingcalledVarnish

Page 4: HTTP caching with Varnish

# apt-get install varnish

Edit/etc/default/varnish

Replaceport6081with80

# service varnish restart

Page 5: HTTP caching with Varnish

Webserveronport8080

# service apache2 restart

Page 6: HTTP caching with Varnish
Page 7: HTTP caching with Varnish

Whatcouldpossiblygo

wrong?

Page 8: HTTP caching with Varnish

Nothinggetscached

Toomuchgetscached

Editorsseenochanges

Cachesgetmixedup

Page 9: HTTP caching with Varnish

Let’stakeastepback

Page 10: HTTP caching with Varnish
Page 11: HTTP caching with Varnish

HTTPissimple

GET /path

HTTP/1.1 200 OKContent-Type: text/html

...

Page 12: HTTP caching with Varnish

Defaultbehaviour

OnlyGETandHEADarecached

Cachefor2minutes

RequestswithCookie/Authorizationarenotcached

ResponseswithSet-Cookiearenotcached

Page 13: HTTP caching with Varnish

Cachecontrolheaders

Cache-Control: s-maxage=3600, max-age=900Expires: Sat, 27 June 2015 09:58:00 GMT

Page 14: HTTP caching with Varnish

Cachecontrolheaders

Settheminyourapp,notinVarnish

Cache-Control: s-maxage=3600, max-age=900Expires: Sat, 27 June 2015 09:58:00 GMT

Page 15: HTTP caching with Varnish

Donotcache

Cache-Control: s-maxage=0, private, no-cache

Page 16: HTTP caching with Varnish

Cachevalidation

Page 17: HTTP caching with Varnish

ETag

Uniqueresourcefingerprint

ClientsendsETag

Stillthesame?304Not-Modified

PreferETagoverLast-Modified

Page 18: HTTP caching with Varnish

curl -o /dev/null -sD - http://fos.lo/path

Page 19: HTTP caching with Varnish

curl -o /dev/null -sD - http://fos.lo/path

HTTP/1.1 200 OKDate: Fri, 22 Jun 2015 19:23:02 GMTCache-Control: s-maxage=30Vary: Accept-EncodingContent-Type: application/jsonAge: 23ETag: 4423466221248

Page 20: HTTP caching with Varnish
Page 21: HTTP caching with Varnish

PlainPHP

header('Cache-Control: s-maxage=600, max-age=60');header('Etag: ' . sha1($data));

Page 22: HTTP caching with Varnish

PlainPHP

Dothisbeforeanyoutputissenttotheclient

header('Cache-Control: s-maxage=600, max-age=60');header('Etag: ' . sha1($data));

Page 23: HTTP caching with Varnish

Symfony

// DefaultController::indexAction$response = $this->render('::index.html.twig');$response->setSharedMaxAge(600);$response->setMaxAge(60);$response->setEtag(sha1($response->getContent()));

return $response;

Page 24: HTTP caching with Varnish

Symfony

Addaboveorannotationtoeveryaction

// DefaultController::indexAction$response = $this->render('::index.html.twig');$response->setSharedMaxAge(600);$response->setMaxAge(60);$response->setEtag(sha1($response->getContent()));

return $response;

Page 25: HTTP caching with Varnish

FOSHttpCacheBundle

app/config/config.yml:

match: path: "^/articles"headers: cache_control: s_maxage: 600 max_age: 60 etag: true

Page 26: HTTP caching with Varnish

Overview

VCL

Invalidation

Tagging

Page 27: HTTP caching with Varnish
Page 28: HTTP caching with Varnish
Page 29: HTTP caching with Varnish

VCL

Page 30: HTTP caching with Varnish

Cookies

sub vcl_recv { if (req.http.Authorization || req.http.Cookie) { return (pass); }

return (hash);}

Page 31: HTTP caching with Varnish

Removecookies

Page 32: HTTP caching with Varnish

Removecookies

sub vcl_recv { if (req.http.Cookie) { if (req.url ~ "^/static") { unset req.http.Cookie; } }}

Page 33: HTTP caching with Varnish

Removecookies

Addstodefaultbehaviour

sub vcl_recv { if (req.http.Cookie) { if (req.url ~ "^/static") { unset req.http.Cookie; } }}

Page 34: HTTP caching with Varnish

Invalidation

Page 35: HTTP caching with Varnish

Thereareonlytwohardthingsincomputerscience:

Cacheinvalidation

Namingthings

Offbyoneerrors

Page 36: HTTP caching with Varnish

Invalidation

Activelyremoveresponsefromcache

LongTTL

Morehits

Page 37: HTTP caching with Varnish

Invalidation

Activelyremoveresponsefromcache

LongTTL

Morehits

Remember:bustyourassets!

Page 38: HTTP caching with Varnish

Invalidationflavours

Purge:URLandvariants

Refresh:exactrequest,warmcache

Ban:batchwithregularexpressions

Page 39: HTTP caching with Varnish

Invalidationflavours

Purge:URLandvariants

Ban:batchwithregularexpressions

Page 40: HTTP caching with Varnish

Purge

Page 41: HTTP caching with Varnish

ConfigureVarnish

acl invalidators { "localhost";}

vcl_recv { if (req.method == "PURGE") { if (!client.ip ~ invalidators) { return (synth(405, "Not allowed")); } return (purge); }}

Page 42: HTTP caching with Varnish

FOSHttpCache

$cacheInvalidator->invalidatePath('/my/path');...$cacheInvalidator->flush();

Page 43: HTTP caching with Varnish

Ban

Page 44: HTTP caching with Varnish

Banispowerful

Regularexpressions

URL

Andanyrequestheader

CustomVarnishconfiguration

Page 45: HTTP caching with Varnish

CachetaggingCachetagging

Page 46: HTTP caching with Varnish

Settags

Whencomment-1changes:

BANrequestforX-Cache-Tagsthatmatchon:comment-1(,.+)?

X-Cache-Tags: comment-1, comment-2

Page 47: HTTP caching with Varnish

Invalidatetags

sub vcl_recv { # ... ban("obj.http.x-cache-tags ~ " + req.http.x-cache-tags ); # ...}

Page 48: HTTP caching with Varnish

FOSHttpCacheBundle

$tagHandler->addTag(['comment-1', 'comment-2']);

$tagHandler->invalidateTags(['comment-1']);

use FOS\HttpCacheBundle\Configuration\Tag;

class CommentController extends Controller { /** * @Tag({"comments", "'comment-'~id"}) */ public function commentAction($id) {

Page 49: HTTP caching with Varnish

Wrap-up

Page 50: HTTP caching with Varnish

IncreaseyourhitsIncreaseyourhits

Page 51: HTTP caching with Varnish

Removecookieswherepossible

Cacheauthenticatedcontent

IncreaseTTL

Inspectcachemisses:

varnishtop -i BereqURL

Page 52: HTTP caching with Varnish

FOSHttpCache

Well-testedVarnishintegrationforPHPapps

Providestestingtools

Cacheauthenticatedcontentbygroup

AlsoforNginx&SymfonyHttpCache

Extensivedocumentation

Symfonybundle

Version2.0:PSR-7,VCLfunctions

Page 53: HTTP caching with Varnish
Page 54: HTTP caching with Varnish

KISSVCL

Applicationperformance

Frontendperformance

Page 55: HTTP caching with Varnish

Questions,input,feedback?Twitter:

@dbu@ddeboer_nl

https://joind.in/14240

Page 56: HTTP caching with Varnish

Varnish4

Client/backendseparation

SimplerVCL

FinallytakesCache-Controlseriously

Page 57: HTTP caching with Varnish

Usercontextcaching

Cacheauthenticatedresponses

Cacheforgroupsofusers

Letbackenddecideongroups

Page 58: HTTP caching with Varnish

ESI

<html> <body> Main body. <esi:include src="/esi-fragment.php" /> </body></html>

Page 59: HTTP caching with Varnish

Content-negotation

Needtokeepvariantsapart

Request:

Response:

Accept: application/json

Vary: Accept


Recommended