Upload
schoefmax
View
53.735
Download
3
Embed Size (px)
DESCRIPTION
Citation preview
var∙nish:Adeceptivelyattractiveexternalappearance;anoutwardshow.
var∙nished,var∙nish∙ing:Togiveasmoothandglossyfinishto.
Wewilltalkabout...
WhatisaReverseProxyCache?
ArchitectureofVarnish
Installation&BasicConfiguration
VCLbyexample
Tools
Varnish&Rails
Misctips&tricks
ehcaCyxorPesreveR
RPC
APP
APP
APP
What? =
Reverse‐Proxy...àlaHAProxy,Pound,mod_proxy_balanceretc.
+Cache...onlyproxytobackendifnecessary
a.k.a.:„HTTPAccelerator“(=BSBingo)
Other„HTTPAccelerators“:BIG‐IP
Web Cache 10g
Users
search.twitter.com
hulu.com
wikia.com
pcwelt.de
creativecommons.org
...
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“
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
Architecture:Logging
Notyourdaddy‘slogfile
Logsstraighttosharedmemory
Enablesallkindsoffancytools: varnishtop varnishstat varnishhist(= geek pr0n)
Usevarnishlog/varnishncsatogenerateoldschoollogs
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
Configuration Zeroconfigurationinaperfectworld
(=alloriginserversperfectHTTPcitizens,settingcorrectcachecontrolheaders,conservativeuseofcookies)
Varnishwon'tcacheanything"private"orcarryingacookiebydefault
Therealworldsucks: Trackingcookies(GoogleAnalytics) Sessioncookiesalthoughnodatainsession "Cache‐control:private"bydefault(Rails)* ...
(*whichisasensibledefault,btw.)
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; }
}
VCL:Directors
director d1 random { .retries = 3; { .backend = "default"; .weight = 10; } { .backend = "other_host"; .weight = 5; }
}
director d2 round-robin { ... }
forsimpleload‐balancingrequirements
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
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
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
Example:Choosebackend
sub vcl_recv { if (req.host ~ "slowapp.com$") { set req.backend = slow_j2ee_app; } else { set req.backend = other_backend; } }
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; } }
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; } }
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
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
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:
Tools:varnishhist | | | | | | | | | | | | | | | | || ||| ||| ||| ||| ||| ||| ||| ||| ||| |||| |||| |||| ||||| ||||| |||||| ## # #| +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+------------- |1e-6 |1e-5 |1e-4 |1e-3 |1e-2 |1e-1 |1e0 |1e1 |1e2
Hits
Misses
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!
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.)
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
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
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
Misc:Monitoringwithmunin
• 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.