SlideShare une entreprise Scribd logo
1  sur  36
Varnish– A brief introduction Nicolas A. Bérard-Nault June 15, 2011
Regular page view
Reverse proxy cached page view
So whatisVarnish ? ,[object Object]
Designedfrom the ground up to be an HTTP accelerator solutionWewillcover ,[object Object]
ESI
HTTP headers
Keezmovies.com  - Benchmarks  - Use case  - Problems & solutions
ConfiguringVarnish Varnish uses a configuration file compiled to C on the fly and included as a sharedlibrary. The configuration format iscalled the VCL (Varnish Configuration Language), a domainspecificlanguagereminescent of Perl. If the VCL is not enough, youcan configure usinginline C and the VRT (VarnishRun Time) library. For a full reference:  http://www.varnish-cache.org/docs/2.1/tutorial/vcl.html
Step by stepthrough the configuration Back end definitions backend www {  .host = "www.example.com";  .port = "http";  .connect_timeout = 1s;  .first_byte_timeout = 5s;  .between_bytes_timeout = 2s; 	.probe = {  	.url = "/test.jpg";  	.timeout = 0.3 s;  	.window = 8;  	.threshold = 3;  }  } You can have as manybackends as youwant
Step by stepthrough the configuration Directordefinitions director www_director random {  { .backend = www1; .weight = 2; }  { .backend = www2; .weight = 1; }  } director www_directorround-robin	 {  	{ .backend = www1; }  	{ .backend = www2; }  } You can have as manydirectors as youwant
Highlysimplified flow chartof Varnish operations
Step by stepthrough the configuration recv: connectionisreceived 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.http.Authorization || req.http.Cookie) {         /* Not cacheable by default */         return (pass);     }     if (req.request != "GET" && req.request != "HEAD") {         /* We only deal with GET and HEAD by default */         return (pass);     }    return (lookup); }
Be carefulwithyour HTTP verbs… But wealwayscheat…
vcl_hash
vcl_hash: createobject hash for request sub vcl_hash { hash_data(req.url); 	if (req.http.host) { hash_data(req.http.host); 	} else { hash_data(server.ip); 	} 	return (hash); }
vcl_hit, vcl_miss
vcl_pass: request not cacheable sub vcl_pass{ 	return (pass); } vcl_hit: post-lookup, objectexists in cache sub vcl_hit { 	return (deliver); } vcl_miss: post-lookupobjectdoes not exist in cache sub vcl_miss { 	return (fetch); }
vcl_fetch
vcl_fetch: post objectfetchedfrom back-end sub vcl_fetch { 	if (beresp.ttl <= 0s ||  beresp.http.Set-Cookie ||  beresp.http.Vary== "*") { 		set beresp.ttl = 120 s; 	    return (hit_for_pass); 	} 	return (deliver); }
vcl_fetch
Step by stepthrough the configuration vcl_deliver: objectis to bedelivered to client sub vcl_deliver { 	return (deliver); }
ESI (edge-sideinclude) Invented by Akamai, only a subsetissupported by Varnish Varnish supports include: <div> Hello: <esi:includesrc=“/getname.php“ /> </div> Will beprocessedinto: <div> Hello: Roger Cyr </div>
ESI (edge-sideinclude) To enable ESI processing, used the esikeyword in vcl_fetch. ESI and gzip VarnishWILL NOT be able to do ESI processing on gzip’edbackendresponses. It willalso not be able to do ungzip an ESI response. In all cases, ESIs and gzip are not a good mix. Better support isplanned for Varnish 3.0.
HTTP headers Varnish relies on HTTP headers to know what to cache and for how long. This isdonethrough the Cache-Control HTTP header. Cache-Control: 30 Cache-Control: max-age=900 Cache-Control: no-cache Cache-Control: must-revalidate Read the HTTP RFC ! http://tools.ietf.org/html/rfc2616#section-14.9
keezmovies.com
keezmovies.com ,[object Object]
Homepagegets a large part of the hits (~35%, ~53 queries per second)
Logged in trafficis a very, very, verysmallminorityPerfect candidate for full page caching
Someresults for KM Tested four configurations: Apache + PHP Apache + PHP + APC Lighttpd + PHP + APC Varnish - Homepage (size = 90k, gzipped = 10k). - Testedusing Apache Benchmark with Increasingconcurrency.
But… Content differsslightly for certain countries (notoriously, Germany) Google Analytics cookies And of course, not all GETrequests are nullipotent The good news is, two of thesethreeproblems are easilytackable !
Problem #1: Geolocalization Essentially, each page has 2 versions: Germanvisitor & disclaimer not accepted Rest of the world & Germanvisitorwhoaccepteddisclaimer __attribute__((constructor)) void load_module() {     /* … */ handle = dlopen(“/usr/lib/varnish/geoip.so”, RTLD_NOW); if (handle != NULL) { get_country_code= dlsym(handle, “get_country_code”); } } }C
The following code isadded to vcl_recv subvcl_recv {   C{     char *cc = (*get_country_code)(VRT_IP_string(sp, VRT_r_client_ip(sp))); VRT_SetHdr(sp, HDR_REQ, "17X-Country-Code:", cc,  vrt_magic_string_end);   }C   if (req.http.Cookie ~ "age_verified.*" ) {       set req.http.X-Age-Verified = "1";   } else {       set req.http.X-Age-Verified = "0";   } } The PHP page isresponsible for setting the age_verified cookie once the disclaimerisaccepted

Contenu connexe

Tendances

Developing cacheable PHP applications - Confoo 2018
Developing cacheable PHP applications - Confoo 2018Developing cacheable PHP applications - Confoo 2018
Developing cacheable PHP applications - Confoo 2018Thijs Feryn
 
VUG5: Varnish at Opera Software
VUG5: Varnish at Opera SoftwareVUG5: Varnish at Opera Software
VUG5: Varnish at Opera SoftwareCosimo Streppone
 
Bootstrapping multidc observability stack
Bootstrapping multidc observability stackBootstrapping multidc observability stack
Bootstrapping multidc observability stackBram Vogelaar
 
Developing cacheable PHP applications - PHPLimburgBE 2018
Developing cacheable PHP applications - PHPLimburgBE 2018Developing cacheable PHP applications - PHPLimburgBE 2018
Developing cacheable PHP applications - PHPLimburgBE 2018Thijs Feryn
 
Lightning fast with Varnish
Lightning fast with VarnishLightning fast with Varnish
Lightning fast with VarnishVarnish Software
 
How we use and deploy Varnish at Opera
How we use and deploy Varnish at OperaHow we use and deploy Varnish at Opera
How we use and deploy Varnish at OperaCosimo Streppone
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newYiwei Ma
 
Zend Server Data Caching
Zend Server Data CachingZend Server Data Caching
Zend Server Data CachingEl Taller Web
 
DEVIEW - 오픈소스를 활용한 분산아키텍처 구현기술
DEVIEW - 오픈소스를 활용한 분산아키텍처 구현기술DEVIEW - 오픈소스를 활용한 분산아키텍처 구현기술
DEVIEW - 오픈소스를 활용한 분산아키텍처 구현기술John Kim
 
Caching the Uncacheable: Leveraging Your CDN to Cache Dynamic Content
Caching the Uncacheable: Leveraging Your CDN to Cache Dynamic ContentCaching the Uncacheable: Leveraging Your CDN to Cache Dynamic Content
Caching the Uncacheable: Leveraging Your CDN to Cache Dynamic ContentFastly
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统yiditushe
 
Caching and tuning fun for high scalability @ PHPTour
Caching and tuning fun for high scalability @ PHPTourCaching and tuning fun for high scalability @ PHPTour
Caching and tuning fun for high scalability @ PHPTourWim Godden
 
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHPNortheast PHP - High Performance PHP
Northeast PHP - High Performance PHPJonathan Klein
 
Varnish @ Velocity Ignite
Varnish @ Velocity IgniteVarnish @ Velocity Ignite
Varnish @ Velocity IgniteArtur Bergman
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalabilityWim Godden
 
Socket programming with php
Socket programming with phpSocket programming with php
Socket programming with phpElizabeth Smith
 
Perl at SkyCon'12
Perl at SkyCon'12Perl at SkyCon'12
Perl at SkyCon'12Tim Bunce
 
Lua tech talk
Lua tech talkLua tech talk
Lua tech talkLocaweb
 

Tendances (20)

Developing cacheable PHP applications - Confoo 2018
Developing cacheable PHP applications - Confoo 2018Developing cacheable PHP applications - Confoo 2018
Developing cacheable PHP applications - Confoo 2018
 
VUG5: Varnish at Opera Software
VUG5: Varnish at Opera SoftwareVUG5: Varnish at Opera Software
VUG5: Varnish at Opera Software
 
Bootstrapping multidc observability stack
Bootstrapping multidc observability stackBootstrapping multidc observability stack
Bootstrapping multidc observability stack
 
Developing cacheable PHP applications - PHPLimburgBE 2018
Developing cacheable PHP applications - PHPLimburgBE 2018Developing cacheable PHP applications - PHPLimburgBE 2018
Developing cacheable PHP applications - PHPLimburgBE 2018
 
Lightning fast with Varnish
Lightning fast with VarnishLightning fast with Varnish
Lightning fast with Varnish
 
How we use and deploy Varnish at Opera
How we use and deploy Varnish at OperaHow we use and deploy Varnish at Opera
How we use and deploy Varnish at Opera
 
Varnish
VarnishVarnish
Varnish
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 new
 
Zend Server Data Caching
Zend Server Data CachingZend Server Data Caching
Zend Server Data Caching
 
DEVIEW - 오픈소스를 활용한 분산아키텍처 구현기술
DEVIEW - 오픈소스를 활용한 분산아키텍처 구현기술DEVIEW - 오픈소스를 활용한 분산아키텍처 구현기술
DEVIEW - 오픈소스를 활용한 분산아키텍처 구현기술
 
Caching the Uncacheable: Leveraging Your CDN to Cache Dynamic Content
Caching the Uncacheable: Leveraging Your CDN to Cache Dynamic ContentCaching the Uncacheable: Leveraging Your CDN to Cache Dynamic Content
Caching the Uncacheable: Leveraging Your CDN to Cache Dynamic Content
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
 
Caching and tuning fun for high scalability @ PHPTour
Caching and tuning fun for high scalability @ PHPTourCaching and tuning fun for high scalability @ PHPTour
Caching and tuning fun for high scalability @ PHPTour
 
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHPNortheast PHP - High Performance PHP
Northeast PHP - High Performance PHP
 
Varnish @ Velocity Ignite
Varnish @ Velocity IgniteVarnish @ Velocity Ignite
Varnish @ Velocity Ignite
 
Memcached Study
Memcached StudyMemcached Study
Memcached Study
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
Socket programming with php
Socket programming with phpSocket programming with php
Socket programming with php
 
Perl at SkyCon'12
Perl at SkyCon'12Perl at SkyCon'12
Perl at SkyCon'12
 
Lua tech talk
Lua tech talkLua tech talk
Lua tech talk
 

En vedette

Synopsis Pride And Prejudice
Synopsis Pride And PrejudiceSynopsis Pride And Prejudice
Synopsis Pride And PrejudiceArmthorpe Media
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentationnicobn
 
Tv drama tech terms handout
Tv drama tech terms handoutTv drama tech terms handout
Tv drama tech terms handoutArmthorpe Media
 
Tangelo an Open Image Tagging Platform
Tangelo an Open Image Tagging PlatformTangelo an Open Image Tagging Platform
Tangelo an Open Image Tagging Platformtmannik
 
Media representations handout
Media representations  handoutMedia representations  handout
Media representations handoutArmthorpe Media
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitUnit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitMichelangelo van Dam
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnitvaruntaliyan
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitMichelangelo van Dam
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to heroJeremy Cook
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnitMindfire Solutions
 

En vedette (14)

Synopsis Pride And Prejudice
Synopsis Pride And PrejudiceSynopsis Pride And Prejudice
Synopsis Pride And Prejudice
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
 
Tv drama tech terms handout
Tv drama tech terms handoutTv drama tech terms handout
Tv drama tech terms handout
 
Editing cutting
Editing   cuttingEditing   cutting
Editing cutting
 
Reciclam paper
Reciclam paperReciclam paper
Reciclam paper
 
Tangelo an Open Image Tagging Platform
Tangelo an Open Image Tagging PlatformTangelo an Open Image Tagging Platform
Tangelo an Open Image Tagging Platform
 
Media representations handout
Media representations  handoutMedia representations  handout
Media representations handout
 
Media12charity
Media12charityMedia12charity
Media12charity
 
Onomata
OnomataOnomata
Onomata
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitUnit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnit
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
 

Similaire à June8 presentation

Going crazy with Varnish and Symfony
Going crazy with Varnish and SymfonyGoing crazy with Varnish and Symfony
Going crazy with Varnish and SymfonyDavid de Boer
 
VCLをTDDで書いてデプロイする
VCLをTDDで書いてデプロイするVCLをTDDで書いてデプロイする
VCLをTDDで書いてデプロイするKengo HAMASAKI
 
Boost your website by running PHP on Nginx
Boost your website by running PHP on NginxBoost your website by running PHP on Nginx
Boost your website by running PHP on NginxHarald Zeitlhofer
 
Caching with Varnish
Caching with VarnishCaching with Varnish
Caching with Varnishschoefmax
 
Caching the Uncacheable
Caching the UncacheableCaching the Uncacheable
Caching the Uncacheabledanrot
 
Varnish Cache Plus. Random notes for wise web developers
Varnish Cache Plus. Random notes for wise web developersVarnish Cache Plus. Random notes for wise web developers
Varnish Cache Plus. Random notes for wise web developersCarlos Abalde
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalabilityWim Godden
 
Varnish, the high performance valhalla?
Varnish, the high performance valhalla?Varnish, the high performance valhalla?
Varnish, the high performance valhalla?Jeroen van Dijk
 
HTML5: huh, what is it good for?
HTML5: huh, what is it good for?HTML5: huh, what is it good for?
HTML5: huh, what is it good for?Remy Sharp
 
Caching and tuning fun for high scalability @ FOSDEM 2012
Caching and tuning fun for high scalability @ FOSDEM 2012Caching and tuning fun for high scalability @ FOSDEM 2012
Caching and tuning fun for high scalability @ FOSDEM 2012Wim Godden
 
Varnish and Drupal- Accelerating Website Performance and Flexibility with Var...
Varnish and Drupal- Accelerating Website Performance and Flexibility with Var...Varnish and Drupal- Accelerating Website Performance and Flexibility with Var...
Varnish and Drupal- Accelerating Website Performance and Flexibility with Var...Acquia
 
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...Codemotion
 
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...Codemotion
 
Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!Jeff Jones
 
Solving anything in VCL
Solving anything in VCLSolving anything in VCL
Solving anything in VCLFastly
 
Less and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developersLess and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developersSeravo
 

Similaire à June8 presentation (20)

Going crazy with Varnish and Symfony
Going crazy with Varnish and SymfonyGoing crazy with Varnish and Symfony
Going crazy with Varnish and Symfony
 
VCLをTDDで書いてデプロイする
VCLをTDDで書いてデプロイするVCLをTDDで書いてデプロイする
VCLをTDDで書いてデプロイする
 
Boost your website by running PHP on Nginx
Boost your website by running PHP on NginxBoost your website by running PHP on Nginx
Boost your website by running PHP on Nginx
 
Caching with Varnish
Caching with VarnishCaching with Varnish
Caching with Varnish
 
Caching the Uncacheable
Caching the UncacheableCaching the Uncacheable
Caching the Uncacheable
 
Varnish Cache Plus. Random notes for wise web developers
Varnish Cache Plus. Random notes for wise web developersVarnish Cache Plus. Random notes for wise web developers
Varnish Cache Plus. Random notes for wise web developers
 
Varnish qconsp 2011
Varnish qconsp 2011Varnish qconsp 2011
Varnish qconsp 2011
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
Varnish, the high performance valhalla?
Varnish, the high performance valhalla?Varnish, the high performance valhalla?
Varnish, the high performance valhalla?
 
HTML5: huh, what is it good for?
HTML5: huh, what is it good for?HTML5: huh, what is it good for?
HTML5: huh, what is it good for?
 
Caching and tuning fun for high scalability @ FOSDEM 2012
Caching and tuning fun for high scalability @ FOSDEM 2012Caching and tuning fun for high scalability @ FOSDEM 2012
Caching and tuning fun for high scalability @ FOSDEM 2012
 
Varnish
VarnishVarnish
Varnish
 
Varnish and Drupal- Accelerating Website Performance and Flexibility with Var...
Varnish and Drupal- Accelerating Website Performance and Flexibility with Var...Varnish and Drupal- Accelerating Website Performance and Flexibility with Var...
Varnish and Drupal- Accelerating Website Performance and Flexibility with Var...
 
hacking with node.JS
hacking with node.JShacking with node.JS
hacking with node.JS
 
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
 
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
 
Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!
 
Solving anything in VCL
Solving anything in VCLSolving anything in VCL
Solving anything in VCL
 
Less and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developersLess and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developers
 
Nginx + PHP
Nginx + PHPNginx + PHP
Nginx + PHP
 

June8 presentation

  • 1. Varnish– A brief introduction Nicolas A. Bérard-Nault June 15, 2011
  • 4.
  • 5.
  • 6. ESI
  • 8. Keezmovies.com - Benchmarks - Use case - Problems & solutions
  • 9. ConfiguringVarnish Varnish uses a configuration file compiled to C on the fly and included as a sharedlibrary. The configuration format iscalled the VCL (Varnish Configuration Language), a domainspecificlanguagereminescent of Perl. If the VCL is not enough, youcan configure usinginline C and the VRT (VarnishRun Time) library. For a full reference: http://www.varnish-cache.org/docs/2.1/tutorial/vcl.html
  • 10. Step by stepthrough the configuration Back end definitions backend www { .host = "www.example.com"; .port = "http"; .connect_timeout = 1s; .first_byte_timeout = 5s; .between_bytes_timeout = 2s; .probe = { .url = "/test.jpg"; .timeout = 0.3 s; .window = 8; .threshold = 3; } } You can have as manybackends as youwant
  • 11. Step by stepthrough the configuration Directordefinitions director www_director random { { .backend = www1; .weight = 2; } { .backend = www2; .weight = 1; } } director www_directorround-robin { { .backend = www1; } { .backend = www2; } } You can have as manydirectors as youwant
  • 12. Highlysimplified flow chartof Varnish operations
  • 13. Step by stepthrough the configuration recv: connectionisreceived 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.http.Authorization || req.http.Cookie) { /* Not cacheable by default */ return (pass); } if (req.request != "GET" && req.request != "HEAD") { /* We only deal with GET and HEAD by default */ return (pass); } return (lookup); }
  • 14. Be carefulwithyour HTTP verbs… But wealwayscheat…
  • 16. vcl_hash: createobject hash for request sub vcl_hash { hash_data(req.url); if (req.http.host) { hash_data(req.http.host); } else { hash_data(server.ip); } return (hash); }
  • 18. vcl_pass: request not cacheable sub vcl_pass{ return (pass); } vcl_hit: post-lookup, objectexists in cache sub vcl_hit { return (deliver); } vcl_miss: post-lookupobjectdoes not exist in cache sub vcl_miss { return (fetch); }
  • 20. vcl_fetch: post objectfetchedfrom back-end sub vcl_fetch { if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary== "*") { set beresp.ttl = 120 s; return (hit_for_pass); } return (deliver); }
  • 22. Step by stepthrough the configuration vcl_deliver: objectis to bedelivered to client sub vcl_deliver { return (deliver); }
  • 23. ESI (edge-sideinclude) Invented by Akamai, only a subsetissupported by Varnish Varnish supports include: <div> Hello: <esi:includesrc=“/getname.php“ /> </div> Will beprocessedinto: <div> Hello: Roger Cyr </div>
  • 24. ESI (edge-sideinclude) To enable ESI processing, used the esikeyword in vcl_fetch. ESI and gzip VarnishWILL NOT be able to do ESI processing on gzip’edbackendresponses. It willalso not be able to do ungzip an ESI response. In all cases, ESIs and gzip are not a good mix. Better support isplanned for Varnish 3.0.
  • 25. HTTP headers Varnish relies on HTTP headers to know what to cache and for how long. This isdonethrough the Cache-Control HTTP header. Cache-Control: 30 Cache-Control: max-age=900 Cache-Control: no-cache Cache-Control: must-revalidate Read the HTTP RFC ! http://tools.ietf.org/html/rfc2616#section-14.9
  • 27.
  • 28. Homepagegets a large part of the hits (~35%, ~53 queries per second)
  • 29. Logged in trafficis a very, very, verysmallminorityPerfect candidate for full page caching
  • 30. Someresults for KM Tested four configurations: Apache + PHP Apache + PHP + APC Lighttpd + PHP + APC Varnish - Homepage (size = 90k, gzipped = 10k). - Testedusing Apache Benchmark with Increasingconcurrency.
  • 31.
  • 32.
  • 33.
  • 34. But… Content differsslightly for certain countries (notoriously, Germany) Google Analytics cookies And of course, not all GETrequests are nullipotent The good news is, two of thesethreeproblems are easilytackable !
  • 35. Problem #1: Geolocalization Essentially, each page has 2 versions: Germanvisitor & disclaimer not accepted Rest of the world & Germanvisitorwhoaccepteddisclaimer __attribute__((constructor)) void load_module() { /* … */ handle = dlopen(“/usr/lib/varnish/geoip.so”, RTLD_NOW); if (handle != NULL) { get_country_code= dlsym(handle, “get_country_code”); } } }C
  • 36. The following code isadded to vcl_recv subvcl_recv { C{ char *cc = (*get_country_code)(VRT_IP_string(sp, VRT_r_client_ip(sp))); VRT_SetHdr(sp, HDR_REQ, "17X-Country-Code:", cc, vrt_magic_string_end); }C if (req.http.Cookie ~ "age_verified.*" ) { set req.http.X-Age-Verified = "1"; } else { set req.http.X-Age-Verified = "0"; } } The PHP page isresponsible for setting the age_verified cookie once the disclaimerisaccepted
  • 37. The following code isadded to vcl_hash sub vcl_hash{ if (req.http.x-country-code=="DE" && req.http.x-age-verified == "0") { set req.hash += req.http.x-age-verified; set req.hash += req.http.x-country-code; } } You candownload the VarnishGeoIPlibraryhere: http://www.varnish-cache.org/trac/wiki/GeoipUsingInlineC It uses the MaxmindGeoIPlibrary.
  • 38. Problem #2: Google Analyticscookie sub vcl_recv { if (req.http.Cookie) { if (req.http.Cookie ~ "user_cookie.*" ) { return( pass); } remove req.http.Cookie; } } This removes all cookies except the oneswe know to beuseful
  • 39. Problem #3: GET requestswithsideeffects JSON UDP packets
  • 40.
  • 42. Can handle and aggregaterequestsfrommanyVarnish servers at the same time
  • 43. Bonus: canbeused for many, many, manyotherthings….Core: http://github.com/nicobn/AlysObserver Varnish module: http://github.com/nicobn/AlysVarnish
  • 44. Side note: YourTTL istoohigh KeezMovies: 53qps on home page Rapidlydecreasing marginal utility Dr. Strangelove or how I learned to stop worrying and love lowTTLs