varnish - cache mal was!

51
1 Christian Nölle Varnish Varnish 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.

Upload: christian-noelle

Post on 11-May-2015

1.561 views

Category:

Technology


4 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Varnish - Cache mal was!

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.

Page 2: Varnish - Cache mal was!

2 Christian Nölle

Für drinnen UND draussen!

Page 3: Varnish - Cache mal was!

3 Christian Nölle

wtf?

Page 4: Varnish - Cache mal was!

4 Christian Nölle

Varnish (software)Yeah!

Page 5: Varnish - Cache mal was!

5 Christian Nölle

Christian Nölle

Wer spricht denn da?

Page 6: Varnish - Cache mal was!

6 Christian Nölle

Was tut er denn?

StudiumAbschlussSelbständigMitarbeiterTätigLebt

Page 7: Varnish - Cache mal was!

7 Christian Nölle

Wuppertal für Kölner

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

Page 8: Varnish - Cache mal was!

8 Christian Nölle

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

Page 9: Varnish - Cache mal was!

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

Page 10: Varnish - Cache mal was!

10 Christian Nölle

Server64bit OS

Multi coreSpeicher

Page 11: Varnish - Cache mal was!

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

Page 12: Varnish - Cache mal was!

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

Page 13: Varnish - Cache mal was!

13 Christian Nölle

Konfigurieren!Varnish Configuration Language

VCL/etc/varnish/default.vcl

-f /etc/varnish/meine.vcl

Page 14: Varnish - Cache mal was!

14 Christian Nölle

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

Varnish gelinkt!Fallback auf default VCL

Page 15: Varnish - Cache mal was!

15 Christian Nölle

And Now for Something Completely Different

Page 16: Varnish - Cache mal was!

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

Page 17: Varnish - Cache mal was!

17 Christian Nölle

Was geht?!

vcl_pipe

vcl_fetch

vcl_pass

vcl_miss

vcl_recv

vcl_hash

vcl_hit

vcl_deliver

Page 18: Varnish - Cache mal was!

18 Christian Nölle

Treffer!

vcl_pipe

vcl_fetch

vcl_pass

vcl_miss

vcl_recv

vcl_hash

vcl_hit

vcl_deliver

Page 19: Varnish - Cache mal was!

19 Christian Nölle

Daneben!

vcl_pipe

vcl_fetch

vcl_pass

vcl_miss

vcl_recv

vcl_hash

vcl_hit

vcl_deliver

Page 20: Varnish - Cache mal was!

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

Page 21: Varnish - Cache mal was!

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

Page 22: Varnish - Cache mal was!

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

Page 23: Varnish - Cache mal was!

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

Page 24: Varnish - Cache mal was!

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

Page 25: Varnish - Cache mal was!

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 ;)

Page 26: Varnish - Cache mal was!

27 Christian Nölle

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

Erfahrung gesammeltEINIGES geändert!

;)

Page 27: Varnish - Cache mal was!

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

Page 28: Varnish - Cache mal was!

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!

Page 29: Varnish - Cache mal was!

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

Page 30: Varnish - Cache mal was!

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.

Page 31: Varnish - Cache mal was!

32 Christian Nölle

Mobile Webseiten!gleiche URL

Cache!Mhhhhh…

Page 32: Varnish - Cache mal was!

33 Christian Nölle

Geräteerkennung durch varnishvcl_hash für verschiedene Caches je

GerätegruppeTYPO3 liefert je nach X-Header

verschiedene Templates.

Page 33: Varnish - Cache mal was!

34 Christian Nölle

Geräteerkennung durch varnishvcl_hash für verschiedene Caches je

GerätegruppeTYPO3 liefert je nach X-Header

verschiedene Templates.

Page 34: Varnish - Cache mal was!

35 Christian Nölle

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

devicedetect

Page 35: Varnish - Cache mal was!

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

Page 36: Varnish - Cache mal was!

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

Page 37: Varnish - Cache mal was!

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…

Page 38: Varnish - Cache mal was!

39 Christian Nölle

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

Page 39: Varnish - Cache mal was!

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.

Page 40: Varnish - Cache mal was!

41 Christian Nölle

Page 41: Varnish - Cache mal was!

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…

Page 42: Varnish - Cache mal was!

43 Christian Nölle

FE-User in TYPO3TYPO3 sendet IMMER Cookies!

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

Page 43: Varnish - Cache mal was!

44 Christian Nölle

Lösung? Ideen?

Page 44: Varnish - Cache mal was!

45 Christian Nölle

Unsere Lösung:cache-control nutzen

Page 45: Varnish - Cache mal was!

46 Christian Nölle

Aber:Viele Wege führen nach Rom!

Einzelfall betrachten!

Page 46: Varnish - Cache mal was!

47 Christian Nölle

Als da wären…

moc_varnishcacheinfo

[eureLösung]

Page 47: Varnish - Cache mal was!

48 Christian Nölle

Page 48: Varnish - Cache mal was!

49 Christian Nölle

Und wenn der Cache nur 60% Hits macht?

Page 49: Varnish - Cache mal was!

50 Christian Nölle

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

lwp-requestHTTP Header ansehen

Page 50: Varnish - Cache mal was!

51 Christian Nölle

Essential

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

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

Page 51: Varnish - Cache mal was!

52 Christian Nölle

Christian Nö[email protected]

Twitter: @stbc