Varnish - Cache mal was!

Post on 11-May-2015

1,555 views 4 download

Tags:

description

Vortrag im Rahmen der TYPO3 Usergroup Köln. Vorstellung von varnish als http accelerator

transcript

1 Christian Nölle

VarnishVarnish is a transparent, hard, protective finish or film primarily used in wood finishing but also for other materials. Varnish is traditionally a combination of a drying oil, a resin, and a thinner or solvent. Varnish finishes are usually glossy but may be designed to produce

satin or semi-gloss sheens by the addition of "flatting" agents. Varnish has little or no colour, is transparent, and has no added pigment, as opposed to paints or wood stains, which

contain pigment and generally range fromopaque to translucent. Varnishes are also applied over wood stains as a final step to achieve a film for gloss and protection. Some products

are marketed as a combined stain and varnish.

2 Christian Nölle

Für drinnen UND draussen!

3 Christian Nölle

wtf?

4 Christian Nölle

Varnish (software)Yeah!

5 Christian Nölle

Christian Nölle

Wer spricht denn da?

6 Christian Nölle

Was tut er denn?

StudiumAbschlussSelbständigMitarbeiterTätigLebt

7 Christian Nölle

Wuppertal für Kölner

Quelle: flickr.com – User Metro Centric – unter CC BY 2.0

8 Christian Nölle

Varnish =(HTTP|web) (proxy|accelerator)

9 Christian Nölle

Sag JA zu Performance!

Varnish works on 32-bit, but was designed for 64bit. It's all about virtual memory: Things like stack size suddenly matter on 32bit. If you must use Varnish on 32-bit, you're somewhat on your own. However, try to fit it within 2GB. I wouldn't recommend a cache larger than 1GB, and no more than a few hundred threads... (Why are you on 32bit again?)

http://kly.no/posts/2010_01_26__Varnish_best_practices__.html

10 Christian Nölle

Server64bit OS

Multi coreSpeicher

11 Christian Nölle

Speicher satt?-s malloc,5G

Öh, Speicher?-s file,/tmp/varnish_storage.bin,5G

Lauschen lassen!-a 0.0.0.0:80

12 Christian Nölle

Tuning?

After working with Varnish I've realized that you do not really need to tune Varnish much: The defaults are tuned. The only real exception I've found to this is number of threads and possibly work spaces.

http://kly.no/posts/2010_01_26__Varnish_best_practices__.html

13 Christian Nölle

Konfigurieren!Varnish Configuration Language

VCL/etc/varnish/default.vcl

-f /etc/varnish/meine.vcl

14 Christian Nölle

Definiert die Caching-RegelnWird geparst, in C compiliert und in

Varnish gelinkt!Fallback auf default VCL

15 Christian Nölle

And Now for Something Completely Different

16 Christian Nölle

Ich distanziere mich von der nun folgenden Darsstellung:

Quelle: commons.wikimedia.org / Creative Commons CC0 1.0 Universal Public Domain Dedication

17 Christian Nölle

Was geht?!

vcl_pipe

vcl_fetch

vcl_pass

vcl_miss

vcl_recv

vcl_hash

vcl_hit

vcl_deliver

18 Christian Nölle

Treffer!

vcl_pipe

vcl_fetch

vcl_pass

vcl_miss

vcl_recv

vcl_hash

vcl_hit

vcl_deliver

19 Christian Nölle

Daneben!

vcl_pipe

vcl_fetch

vcl_pass

vcl_miss

vcl_recv

vcl_hash

vcl_hit

vcl_deliver

20 Christian Nölle

Haben wir bedingt mit zu tun!

vcl_pipe

vcl_fetch

vcl_pass

vcl_miss

vcl_recv

vcl_hash

vcl_hit

vcl_deliver

21 Christian Nölle

Wollen wir nie was mit zu tun haben!

vcl_pipe

vcl_fetch

vcl_pass

vcl_miss

vcl_recv

vcl_hash

vcl_hit

vcl_deliver

23 Christian Nölle

backend default {.host = "12.34.56.78";.port = "80";.probe = {

.url = "/probe.txt"; .timeout = 1s; .interval = 3s; .window = 10; .threshold = 5; }}

• Backend(s) definieren• Probes = Health checks• .window = Wieviele der

letzten Anfragen werden geprüft

• .threshold = Wieviele müssen „gesund“ sein?

• Obacht beim .timeout!

Backend & Probes

24 Christian Nölle

director test round-robin {{ .backend = server1; }{ .backend = server2; }{ .backend = {

.host = "12.34.56.78";

.port = "82"; }

}}

• Definition eines Directors

• round-robin oder random

• random mit Gewichtung: .weight

• Sowie hash, client, fallback und dns Directors

Directors

25 Christian Nölle

sub vcl_recv { if (req.backend.healthy) { set req.grace = 30s; } else { set req.grace = 24h; }}

sub vcl_fetch { set beresp.grace = 24h;}

• Objekt veraltet, aber noch im Cache

• Grace mode: Varnish nutzt ein solches Objekt

• Bedingungen: – Existenz -> beresp.grace– Erlaubnis -> req.grace

Grace mode

26 Christian Nölle

set beresp.ttl=1m;set req.grace = 30s;set beresp.grace = 1h;

• Was geht wann?• 50 Sekunden?

– normale Auslieferung

• 63 Sekunden?– Cache miss, aber grace

möglich

• 80 Sekunden?– Cache miss, aber grace

möglich

• 94 Sekunden?– Cache miss, grace nicht

erlaubt

• 3660 Sekunden (1H + 1m)– Objekt wird aus Cache

entfernt!

Übung ;)

27 Christian Nölle

Start mit einer .vcl von moc.netAnpassung an varnish 3.0

Erfahrung gesammeltEINIGES geändert!

;)

28 Christian Nölle

sub vcl_recv {if (req.url ~

"/typo3/index.php$") { return (pipe); }

if(req.url ~ "/typo3/") {return (pass);

}}

• Logins direkt durch die Pipe schicken

• Aufruf von vcl_pipe

• Backends nicht cachen• Aufruf von vcl_pass

Einiges Wenig an TYPO3 Kram

29 Christian Nölle

sub vcl_fetch {if (beresp.http.cache-control ~ "(no-cache|private)" ||

beresp.http.pragma ~ "no-cache") {set beresp.ttl = 0s;#Make sure that We remove alle cache headers, so the Browser does not cache it for us!remove beresp.http.Cache-Control;remove beresp.http.Expires;remove beresp.http.Last-Modified;remove beresp.http.ETag;remove beresp.http.Pragma;

return (deliver);}

}

• Was tun, wenn Seiten IP basiert ausgeliefert werden?• vcl_fetch veranlassen, sowas nicht zu cachen!

30 Christian Nölle

sub vcl_fetch {if (beresp.status >= 400) {

return (hit_for_pass);}

}

• Fehlerseiten cachen ist ne blöde Idee!

• Also direkt durch?– Direkt wäre pass oder

gar pipe!

• hit_for_pass = Pass in fetch!

• Request pile up wird vermieden

31 Christian Nölle

acl refresh { „123.456.0.0"/16;}

sub vcl_hit {

if (obj.hits == 500) {set obj.ttl = 1d;

} elsif (obj.hits == 10000) {set obj.ttl = 3d;

} #internal hit vcl for CTRL F5if (req.http.pragma ~ "no-cache" || req.http.Cache-Control ~ "no-cache") {

if (client.ip ~ refresh) {purge;return (pass);

}}

}

• Objekte je nach obj.hits gestaffelt länger cachen• Redakteure sollen einzelne Seiten aus dem Cache

aktualisieren können durch STRG & F5.

32 Christian Nölle

Mobile Webseiten!gleiche URL

Cache!Mhhhhh…

33 Christian Nölle

Geräteerkennung durch varnishvcl_hash für verschiedene Caches je

GerätegruppeTYPO3 liefert je nach X-Header

verschiedene Templates.

34 Christian Nölle

Geräteerkennung durch varnishvcl_hash für verschiedene Caches je

GerätegruppeTYPO3 liefert je nach X-Header

verschiedene Templates.

35 Christian Nölle

Besser:https://github.com/varnish/varnish-

devicedetect

36 Christian Nölle

include "devicedetect.vcl";sub vcl_recv { call

devicedetect; }

unset req.http.X-UA-Device;set req.http.X-UA-Device =

"pc";

if (req.http.Cookie ~ "(?i)X-UA-Device-force") {

…}

• fertige Routine inkludieren

• Aufrufen in vcl_recv

• Setzt http X-UA-Device Header

• Cookie zum Abschalten möglich!

Auschnitte der Erkennungsroutine

37 Christian Nölle

sub vcl_fetch { if (req.http.X-UA-Device) { if (!beresp.http.Vary) { # no Vary at all set beresp.http.Vary = "X-UA-Device"; } elseif (beresp.http.Vary !~ "X-UA-Device") { # add to existing Vary set beresp.http.Vary = beresp.http.Vary + ", X-UA-Device"; } } # comment this out if you don't want the client to know your classification set beresp.http.X-UA-Device = req.http.X-UA-Device;}

• Prüft und ergänzt den X-Header• und nutzt Vary um den Content zu unterscheiden• Purging besser möglich als mit vcl_hash

38 Christian Nölle

sub vcl_deliver { if ((req.http.X-UA-Device) && (resp.http.Vary)) { set resp.http.Vary = regsub(resp.http.Vary, "X-UA-Device",

"User-Agent"); }}

• der Vary Header muss allerdings auf dem Weg zum Client zurück wieder normalisiert werden.

• Gefahr: Andere Caches da draussen…

39 Christian Nölle

Quelle: Bob Smith, http://sxc.hu/browse.phtml?f=profile&l=brainloc

40 Christian Nölle

Aus der Doku…

Varnish will, in the default configuration, not cache a object

coming from the backend with a Set-Cookie header present. Also, if the

client sends a Cookie header, Varnish will bypass the cache and go directly

to the backend.

41 Christian Nölle

42 Christian Nölle

if (req.http.Authorization || req.http.Cookie) {

/* Not cacheable by default */

return (pass);}

if (req.http.Authorization) {return (pass);

}

• default.vcl

• Obacht! Simple (und gefährliche) Lösung!

mhhhhh…

43 Christian Nölle

FE-User in TYPO3TYPO3 sendet IMMER Cookies!

Caching ist aber nicht gewünscht, wenn per User Content vorliegt

44 Christian Nölle

Lösung? Ideen?

45 Christian Nölle

Unsere Lösung:cache-control nutzen

46 Christian Nölle

Aber:Viele Wege führen nach Rom!

Einzelfall betrachten!

47 Christian Nölle

Als da wären…

moc_varnishcacheinfo

[eureLösung]

48 Christian Nölle

49 Christian Nölle

Und wenn der Cache nur 60% Hits macht?

50 Christian Nölle

varnishtop -i txurlvarnishlog -c-m 'RxURL:^/meine/url

lwp-requestHTTP Header ansehen

51 Christian Nölle

Essential

https://www.varnish-cache.org/docs/3.0/

https://www.varnish-software.com/static/book/

52 Christian Nölle

Christian Nöllenoelle@uni-wuppertal.de

Twitter: @stbc