Varnish more than a cache

25
1 Varnish – More than a cache Bernd Löffeld Head of Platform Development Magic Internet GmbH Email: [email protected] Magic Internet entwickelt und betreut www.myvideo.de Deutschlands großes Videoportal!

description

An introduction into the features of the Varnish cache and an example for that.

Transcript of Varnish more than a cache

Page 1: Varnish more than a cache

1  

Varnish  –  More  than  a  cache  

Bernd Löffeld Head of Platform Development Magic Internet GmbH Email: [email protected] Magic Internet entwickelt und betreut www.myvideo.de Deutschlands großes Videoportal!

Page 2: Varnish more than a cache

Varnish  Features  

2  

o  Caching  

o  Loadbalancing  and  Backend-­‐Selec3on  

o  Header  analysis  and  manipula3on  

o  DSL  for  handling  all  that  

o  Edge  Side  Includes  

Page 3: Varnish more than a cache

Varnish  Subrou5nes  

3  

Page 4: Varnish more than a cache

Varnish  Configura5on  Language  in  example  

4  

sub vcl_recv {! if (req.restarts == 0) {! if (req.http.x-forwarded-for) {! set req.http.X-Forwarded-For =! req.http.X-Forwarded-For + ", " + client.ip;! } else {! set req.http.X-Forwarded-For = client.ip;! }! }! if (req.request != "GET" &&! req.request != "HEAD" &&! req.request != "PUT" &&! req.request != "POST" &&! req.request != "TRACE" &&! req.request != "OPTIONS" &&! req.request != "DELETE") {! return (pipe);! }! if (req.request != "GET" && req.request != "HEAD") {! /* We only deal with GET and HEAD by default */! return (pass);! }! if (req.http.Authorization || req.http.Cookie) {! return (pass);! }! return (lookup);!}!

Page 5: Varnish more than a cache

The  Setup  –  A  vision  by  now  

5  

Page 6: Varnish more than a cache

Start:  Just  a  simple  Web  Applica5on  

6  

Page 7: Varnish more than a cache

Start:  Just  a  simple  Web  Applica5on  

7  

Page 8: Varnish more than a cache

Step  1:  Ac5vate  the  cache  

8  

Page 9: Varnish more than a cache

Step  1:  Ac5vate  the  cache  

9  

include "bwb/backends.vcl“;

Varnish à /etc/varnish/main.vcl

backend default { .host = "nginx-1"; .port = "80"; }

Varnish à /etc/varnish/bwb/backends.vcl

server { expires 1m; }

nginx-1 à /etc/nginx/sites-available/bwb

Page 10: Varnish more than a cache

Step  1:  Ac5vate  the  cache  

10  

Page 11: Varnish more than a cache

Step  2:  Introduce  new  Backend  

11  

Page 12: Varnish more than a cache

Step  2:  Introduce  new  Backend  

12  

include "bwb/backends.vcl"; sub vcl_recv { if (req.url ~ "/fancy/") { set req.backend = fancy; } }

Varnish à /etc/varnish/main.vcl

backend default { .host = "nginx-1"; .port = "80"; } backend fancy { .host = "nginx-2"; .port = "80"; }

Varnish à /etc/varnish/bwb/backends.vcl

Page 13: Varnish more than a cache

Step  2:  Introduce  new  Backend  

13  

<html> <head> <title>Legacy Web Application</title> <link href="/fancy/css/default.css" rel="stylesheet" type="text/css" media="all" /> </head> ...

nginx-1 à /srv/www/bwb/page1.html

Page 14: Varnish more than a cache

Step  2:  Introduce  new  Backend  

14  

Page 15: Varnish more than a cache

Step  2:  Introduce  new  Backend  

15  

Page 16: Varnish more than a cache

Step  3:  Connect  the  Applica5ons  with  ESI  

16  

Page 17: Varnish more than a cache

Step  3:  Connect  the  Applica5ons  with  ESI  

17  

<body> <!-- Old Navigation HTML was removed!--> <esi:include src="/fancy/nav/navigation.html" /> <h1>Just a robust and experienced application</h1> <div>Page 1 is mostly empty.</div> <esi:include src="/fancy/news/abox.html" /> </body>

nginx-1 à /srv/www/bwb/page1.html

include "bwb/backends.vcl"; sub vcl_recv { if (req.url ~ "/fancy/") { set req.backend = fancy; } } sub vcl_fetch { set beresp.do_esi = true; }

Varnish à /etc/varnish/main.vcl

Page 18: Varnish more than a cache

Step  3:  Connect  the  Applica5ons  with  ESI  

18  

<div class="container newsbox"> <h1>News</h1> <div>really hot new stuff to read</div> </div>

nginx-2 à /srv/www/bwb-fancy/fancy/news/abox.html

server { ... location /fancy/news/ { expires 10s; } }

nginx-2 à /etc/nginx/sites-available/bwb-fancy

Page 19: Varnish more than a cache

Step  3:  Connect  the  Applica5ons  with  ESI  

19  

Page 20: Varnish more than a cache

Step  3:  Connect  the  Applica5ons  with  ESI  

20  

Page 21: Varnish more than a cache

Step  4:  Simple  balancing  between  two  backends  

21  

Page 22: Varnish more than a cache

Step  4:  Simple  balancing  between  two  backends  

22  

backend default { … } backend fancy_1 { .host = "nginx-2"; .port = "80"; } backend fancy_2 { .host = "nginx-3"; .port = "80"; }

varnish à /etc/varnish/bwb/backends.vcl

director fancy_round round-robin { { .backend = fancy_1; } { .backend = fancy_2; } }

Varnish à /etc/varnish/bwb/director.vcl

Page 23: Varnish more than a cache

Step  4:  Simple  balancing  between  two  backends  

23  

include "bwb/backends.vcl"; include "bwb/director.vcl"; sub vcl_recv { if(req.url ~ "/fancy/") { set req.backend = fancy_round; } }

varnish à /etc/varnish/main.vcl

Page 24: Varnish more than a cache

Step  4:  Simple  balancing  between  two  backends  

24  

192.168.56.5 - - [28/Sep/2013:19:51:57 +0200] "GET /fancy/news/abox.html HTTP/1.1" 200 100 “ 192.168.56.5 - - [28/Sep/2013:19:52:28 +0200] "GET /fancy/news/abox.html HTTP/1.1" 200 100 “ 192.168.56.5 - - [28/Sep/2013:19:52:51 +0200] "GET /fancy/news/abox.html HTTP/1.1" 200 100 “ 192.168.56.5 - - [28/Sep/2013:19:53:17 +0200] "GET /fancy/news/abox.html HTTP/1.1" 200 100

access-log nginx-1

192.168.56.5 - - [28/Sep/2013:19:52:14 +0200] "GET /fancy/news/abox.html HTTP/1.1" 200 100 “ 192.168.56.5 - - [28/Sep/2013:19:52:39 +0200] "GET /fancy/news/abox.html HTTP/1.1" 200 100 “ 192.168.56.5 - - [28/Sep/2013:19:53:05 +0200] "GET /fancy/news/abox.html HTTP/1.1" 200 100 “ 192.168.56.5 - - [28/Sep/2013:19:53:31 +0200] "GET /fancy/news/abox.html HTTP/1.1" 200 100

access-log nginx-2

Page 25: Varnish more than a cache

Want  to  know  more?  

25  

[email protected]  

hCp://www.myvideo.de/karriere  

Work  with  us!  o  Architect  

o  Developer  

o  Sysadmin