29
var∙nish: A deceptively attractive external appearance; an outward show. var∙nished, var∙nish∙ing: To give a smooth and glossy finish to.

Caching with Varnish

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Caching with Varnish

var∙nish:Adeceptivelyattractiveexternalappearance;anoutwardshow.

var∙nished,var∙nish∙ing:Togiveasmoothandglossyfinishto.

Page 2: Caching with Varnish

Wewilltalkabout...

  WhatisaReverseProxyCache?

  ArchitectureofVarnish

  Installation&BasicConfiguration

  VCLbyexample

  Tools

  Varnish&Rails

  Misctips&tricks

Page 3: Caching with Varnish

ehcaCyxorPesreveR

RPC

APP

APP

APP

Page 4: Caching with Varnish

What?  =

  Reverse‐Proxy...àlaHAProxy,Pound,mod_proxy_balanceretc.

  +Cache...onlyproxytobackendifnecessary

a.k.a.:„HTTPAccelerator“(=BSBingo)

Other„HTTPAccelerators“:BIG‐IP

Web Cache 10g

Page 5: Caching with Varnish

Users

  search.twitter.com

  hulu.com

  wikia.com

  pcwelt.de

  creativecommons.org

  ...

Page 6: Caching with Varnish

Architecture:CacheStore

Squid

Mem‐Store Disk‐Store

VMM(OS)

RAM HDD

http://varnish.projects.linpro.no/wiki/ArchitectNotes

Varnish

VMM(OS)

RAM HDD

•  onebigfilemappedtoVM•  onefileperobject(pre2.7)•  bookkeeping(diskvs.memory)•  VMMoften„smarter“

Page 7: Caching with Varnish

Architecture:VCL  VarnishConfigurationLanguage

  DSL,compiledtoCcode(srsly!)

  allowsinlineCcode

  hooksintoarequestslifecycle

  Backends,ACLs,LB‐strategiesdefinedhere

  canbehot‐loadedintoarunningvarnishd

  hot‐switchingbetweenmultipleversions/profiles

C{ syslog(LOG_INFO, “Just served the 1000000th page. Hooray!"); }C

Page 8: Caching with Varnish

Architecture:Logging

  Notyourdaddy‘slogfile

  Logsstraighttosharedmemory

  Enablesallkindsoffancytools:  varnishtop  varnishstat  varnishhist(= geek pr0n)

  Usevarnishlog/varnishncsatogenerateoldschoollogs

Page 9: Caching with Varnish

Installation

  Debian/Ubuntu:apt-get –t unstable install varnish

  OSXviaMacPorts:sudo port install varnish

  Fromsource:./configure && make && make install

Interestingfiles:

  /etc/default/varnish

  /etc/varnish/*.vcl

Page 10: Caching with Varnish

Configuration  Zeroconfigurationinaperfectworld

(=alloriginserversperfectHTTPcitizens,settingcorrectcachecontrolheaders,conservativeuseofcookies)

  Varnishwon'tcacheanything"private"orcarryingacookiebydefault

  Therealworldsucks:  Trackingcookies(GoogleAnalytics)  Sessioncookiesalthoughnodatainsession  "Cache‐control:private"bydefault(Rails)*  ...

(*whichisasensibledefault,btw.)

Page 11: Caching with Varnish

VCL:Backends&Probesbackend default {

.host = "10.0.0.12"; .port = "80";

}

backend slow_j2ee_app { .host = "10.0.0.13"; .port = "8080"; .connect_timeout = 1s; .first_byte_timeout = 10s; .between_bytes_timeout = 5s; .probe = { .url = "/check.jsp"; .timeout = 1s; }

}

Page 12: Caching with Varnish

VCL:Directors

director d1 random { .retries = 3; { .backend = "default"; .weight = 10; } { .backend = "other_host"; .weight = 5; }

}

director d2 round-robin { ... }

forsimpleload‐balancingrequirements

Page 13: Caching with Varnish

VCL:ACLs

acl admins { "localhost"; "10.0.0.0"/24; ! "10.0.0.3"; # intern's laptop }

...

if (client.ip ~ admins) { set req.http.x-magic-auth = "1"; } else { unset req.http.x-magic-auth; }

customizebehaviourfordifferentclients

Page 14: Caching with Varnish

VCL:HooksMostimportant:

  vcl_recv Requestcomesin,decidewhattodo

  vcl_fetch Fetchedobjfrombackend,allowstweaking

  vcl_deliver Objectisabouttobedeliveredtoclient

  vcl_hash Calculatehashkeyforlookup,defaultstofullURL

Otherhooks:

vcl_miss,vcl_hit,vcl_error,vcl_discard,vcl_timeout,vcl_pipe,vcl_pass

http://varnish.projects.linpro.no/wiki/VCL

Page 15: Caching with Varnish

VCL:Functions&Variables  regsub(), regsuball(), purge_hash(), purge_url()

  ownsubroutines(notfunctions)withsub foo { ... }

  include "other.vcl"; tosplitfilesintoparts

  req.* Request

  resp.* Response

  bereq.* BackendRequest

  obj.* requestedObject

  client.*, server.*

  set / unset forvariables, remove additionallyforheadershttp://varnish.projects.linpro.no/wiki/VCL

Page 16: Caching with Varnish

Example:Choosebackend

sub vcl_recv { if (req.host ~ "slowapp.com$") { set req.backend = slow_j2ee_app; } else { set req.backend = other_backend; } }

Page 17: Caching with Varnish

Example:Servestaticassets

sub vcl_recv { if (req.url ~ "^/(images|javascripts|styles)/") { remove req.http.cookie; } }

sub vcl_fetch { if (req.url ~ "^/(images|javascripts|styles)/") { remove obj.http.set-cookie; } }

Page 18: Caching with Varnish

Example:Removecertaincookies

sub vcl_recv { set req.http.cookie = regsuball( req.http.cookie, "__utm.=[^;]+(; )?", "" ); set req.http.cookie = regsub(req.http.cookie, "; $", ""); if (req.http.cookie ~ "^ *$") { remove req.http.cookie; } }

Page 19: Caching with Varnish

Example:"Stalewhilerevalidate"

http://www.rfc‐editor.org/internet‐drafts/draft‐nottingham‐http‐stale‐controls‐00.txt

sub vcl_recv { set req.grace = 2m; }

sub vcl_fetch { set obj.grace = 2m; }

Serveslightlystalecontentwhileafreshversionisfetched=>betteruserexperience+nothreadpileup

Page 20: Caching with Varnish

Example:Backendisdown

sub_recv { if (req.backend.healthy) { set req.grace = 30s; } else { set req.grace = 1h; } }

sub_fetch { set obj.grace = 1h; }

Servecachable(outdated)contentevenwhenthebackendisonfire

Page 21: Caching with Varnish

Tools:varnishtop

varnishtop -i RxHeader -I \^User-Agent

2667.43 RxHeader User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9 459.54 RxHeader User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9 372.66 RxHeader User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.9 369.90 RxHeader User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1) 353.06 RxHeader User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www 341.84 RxHeader User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; 323.87 RxHeader User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 317.88 RxHeader User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.9 250.55 RxHeader User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 231.82 RxHeader User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; 173.69 RxHeader User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;

varnishtop –i RxUrl

varnishtop –i RxHeader –I \^Referer

MostpopularBrowser/Agent:

MostpopularURLs:

Trafficsources:

Page 22: Caching with Varnish

Tools:varnishhist | | | | | | | | | | | | | | | | || ||| ||| ||| ||| ||| ||| ||| ||| ||| |||| |||| |||| ||||| ||||| |||||| ## # #| +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+------------- |1e-6 |1e-5 |1e-4 |1e-3 |1e-2 |1e-1 |1e0 |1e1 |1e2

Hits

Misses

Page 23: Caching with Varnish

MoreTools:

  varnishlog: Generate(customized)logs

  varnishncsa: GenerateApachecompatiblelogs

  varnishadm: Manipulatearunningvarnishd

varnishadm -T localhost:6082 purge.url "^/images/" varnishadm –T localhost:6082 vcl.load newconf /etc/my.vcl

  varnishreplay: Parsesaloggeneratedbyvarnishlog andreplaysthetraffic!

Page 24: Caching with Varnish

Varnish&Rails  Properuseofexpires_in insteadofpagecaching

  Onlyusesession ifreallynecessary

  Purgingofcontentpossiblewith:  `varnishadm –T #{hostport} purge.url #{url2purge}`

  net/telnet

  klarlack:http://github.com/schoefmax/klarlack

  !securetheconnectiontovarnish'sadmininterface!(sshtunnel,iptablesetc.)

Page 25: Caching with Varnish

Varnish&Rails:Sweepers# environment.rb config.gem "schoefmax-klarlack", :lib => 'klarlack', :source => 'http://gems.github.com' VARNISH = Varnish::Client.new('1.2.3.4:6082')

# app/sweepers/blog_sweeper.rb class BlogSweeper < ActionController::Caching::Sweeper observe Post include ActionController::UrlWriter

after_save(post) expire_post(post) end

after_destroy(post) expire_post(post) end

private

def expire_post(post) VARNISH.purge :url, post_path(post) VARNISH.purge :url, latest_posts_path end end

Page 26: Caching with Varnish

Misc:EdgeSideIncludes(ESI)  InventedbyAkamai&Co.

  <esi:include src="http://example.com/friend_feed"/>

  http://www.w3.org/TR/esi‐lang

  fragment_fu‐pluginforRails(partofmongrel‐esi)

Header,TTL:15min

Activity‐Feed,TTL:2min

Nav,TTL:

60min

Article,TTL:5min

Page 27: Caching with Varnish

Misc:Finetuningyoursetup

  Useanon‐journalingfilesystem(e.g.ext2)forstoragefile

  Pre‐createstoragefile(minimizesfragmentation).4GB:

dd if=/dev/zero of=storage.bin bs=4M count=1024

  Tweakvarnish'svariousstartupsettings–Twittersare:

http://projects.linpro.no/pipermail/varnish‐dev/2009‐February/000968.html

Page 28: Caching with Varnish

Misc:Monitoringwithmunin

Page 29: Caching with Varnish

•  http://www.varnish‐cache.org

•  http://github.com/schoefmax/klarlack

•  http://varnish.projects.linpro.no/wiki/VCL

•  http://varnish.projects.linpro.no/wiki/ArchitectNotes

•  http://www.rfc‐editor.org/internet‐drafts/draft‐nottingham‐http‐stale‐controls‐00.txt

•  http://projects.linpro.no/pipermail/varnish‐dev/2009‐February/000968.html

•  http://www.w3.org/TR/esi‐lang

Thankyou.