Successfully reported this slideshow.
Accelerate your app with a 
layer of Varnish
Who?
●

●

●

Ex-pat Englishman, now living in Southern
Ontario.
Web developer for 5 years, mostly PHP.
(Almost) Senior So...
Varnish can do incredible 
things for your app...
What is Varnish?
●

●

Open source caching reverse proxy or
HTTP accelerator.
Mature software:
–
–

●

Current version: 3....
Varnish basics
●

Listens on a port (normally 80).

●

When a request arrives:
–

–

●

If content is already in cache it ...
Varnish features
●

●

●

Can cache any kind of content
delivered via HTTP.
Blazingly fast with incredible
performance.
Su...
Varnish features (cont)
●

●
●

●

Configuration through Varnish
Configuration Language (VCL).
Support for Edge Side Inclu...
Show us the demo!

https://github.com/JCook21/VarnishDemo
Apache results
Varnish Results
Is Varnish always the right 
tool?
●

What kind of content are you serving?

●

Cookies.

●

SSL.

●

Varnish is like a la...
Getting started with Varnish
Setting cache times
●

Two possible methods
–
–

●

HTTP cache headers
Directly in VCL

Prefer HTTP cache headers wherever...
Supported OS's
●

Fully supported:
–
–

●

Linux
FreeBSD

'Try not to break':
–
–

OpenBSD

–
●

NetBSD
OS X

Needs a 64 b...
Installing Varnish
●

Use system package manager.
–

●

●

For Debian, Ubuntu and RedHat/CentOS
Varnish software maintains...
Basic configuration
●

●

Ships with an excellent, 'sane' default
VCL configuration.
To begin using Varnish:
–

Configure ...
Adding VCL backends
●
●

●

Must add at least one backend in VCL.
If more than one, first will be the
default.
Backends ca...
Sample backend definition
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Advanced configuration
Introduction to VCL
●

Provides a window into Varnish's state
during the request/response cycle.

●

Not a full programmin...
VCL Subroutines
●

●

Dispatched at different times in the
request/response cycle.
Each routine has default code:
–

Varni...
VCL Subroutines (cont)
●

vcl_init

●

vcl_miss

●

vcl_recv

●

vcl_fetch

●

vcl_pipe

●

vcl_deliver

●

vcl_pass

●

v...
VCL Processing Cycle
VCL Functions
●

Functions Varnish makes available to you:
–
–

regsub(str, regex, sub)

–

regsuball(str, regex, sub)

–
...
vcl_recv
●

●

Called once the OS kernel has given
the complete request to Varnish.
Typical uses:
–

Set the backend to us...
vcl_fetch
●

●

Called when a response is received
from a backend, before it is considered
for inclusion in the cache.
Typ...
Select a backend
backend default {
.host = "192.168.2.1";
.port = "8080";
}
backend bar {
.host = "192.168.2.2";
.port = "...
Remove cookies
sub vcl_recv {
if (req.url !~ "^/admin/.*") {
unset req.http.cookie;
}
}
sub vcl_fetch {
if (req.url !~ "^/...
Set default TTL
sub vcl_fetch {
if (! beresp.http.cache-control &&
beresp.http.content-type ~ "text/html|
text/xml|applica...
VMOD's
●

Allow you to extend Varnish's features.

●

Written in C.

●

●

Community VMOD's at
https://www.varnish-cache.o...
Grace mode
●

●

●

Graced content is an object that has
expired but is kept in cache.
Grace mode is when a graced object ...
Configuring grace
sub vcl_recv {
if (req.backend.healthy) {
set req.grace = 30s;
} else {
set req.grace = 1h;
}
}
sub vcl_...
Health checks
backend foo {
.host = "192.168.2.1";
.port = "8080";
.max_connections = 10;
.probe = {
.url = "/test";
.inte...
Saint mode
●

●

●

Allows you to say that a backend is
'sick' for one url.
Varnish will not make further requests
to that...
Saint mode example
sub vcl_fetch {
if (beresp.status ~ “^5d{2}$”) {
set beresp.saintmode = 10s;
return(restart);
}
}
Edge Side Includes
●

●

Allows different content on the same
page to be cached for different times.
Request flow:
–

–

●...
Configuring ESI's
●

Done in vcl_fetch:
–

●

●

set beresp.do_esi = true;

Need to configure app to support
'layouts' and...
ESI Example
<!DOCTYPE html>
<html>
<head>
<title>ESI example</title>
</head>
<body>
<esi:include src="/route/to/content"/>...
Thanks for listening!
●

Varnish docs:
https://www.varnish-cache.org/docs/3.0/tutorial/inde

●

Any questions?

●

Feel fr...
Upcoming SlideShare
Loading in …5
×

Accelerate your web app with a layer of Varnish

3,963 views

Published on

Slides from a talk given at Confoo in 2014 on the Varnish caching reverse proxy server.

Published in: Technology
  • Be the first to comment

Accelerate your web app with a layer of Varnish

  1. 1. Accelerate your app with a  layer of Varnish
  2. 2. Who? ● ● ● Ex-pat Englishman, now living in Southern Ontario. Web developer for 5 years, mostly PHP. (Almost) Senior Software Engineer at TribeHR. ● Co-organiser of Guelph PHP User Group. ● Ex-professional musician.
  3. 3. Varnish can do incredible  things for your app...
  4. 4. What is Varnish? ● ● Open source caching reverse proxy or HTTP accelerator. Mature software: – – ● Current version: 3.0.5 Varnish 4 currently in tech preview. Used by 5% (500) of the top 10,000 sites on the internet.
  5. 5. Varnish basics ● Listens on a port (normally 80). ● When a request arrives: – – ● If content is already in cache it is served directly. If not Varnish forwards the request to a 'backend' server, delivering the content when it is received. Only caches GET and HEAD requests.
  6. 6. Varnish features ● ● ● Can cache any kind of content delivered via HTTP. Blazingly fast with incredible performance. Support for multiple backends and basic load balancing.
  7. 7. Varnish features (cont) ● ● ● ● Configuration through Varnish Configuration Language (VCL). Support for Edge Side Includes (ESI). Behaviour can be extended through Varnish Mods (VMODs). Includes tools to monitor and tune (varnishtop, varnishlog, varnishhist, varnishstat, etc).
  8. 8. Show us the demo! https://github.com/JCook21/VarnishDemo
  9. 9. Apache results
  10. 10. Varnish Results
  11. 11. Is Varnish always the right  tool? ● What kind of content are you serving? ● Cookies. ● SSL. ● Varnish is like a layer of... varnish. ● Do you really need Varnish?
  12. 12. Getting started with Varnish
  13. 13. Setting cache times ● Two possible methods – – ● HTTP cache headers Directly in VCL Prefer HTTP cache headers wherever possible.
  14. 14. Supported OS's ● Fully supported: – – ● Linux FreeBSD 'Try not to break': – – OpenBSD – ● NetBSD OS X Needs a 64 bit OS ideally.
  15. 15. Installing Varnish ● Use system package manager. – ● ● For Debian, Ubuntu and RedHat/CentOS Varnish software maintains repositories with up to date versions. To obtain up to date version to install on non-core supported platforms compile from source. More info at the Varnish website.
  16. 16. Basic configuration ● ● Ships with an excellent, 'sane' default VCL configuration. To begin using Varnish: – Configure runtime options (/etc/default/varnish). – Add one or more backends to VCL file (/etc/varnish/default.vcl).
  17. 17. Adding VCL backends ● ● ● Must add at least one backend in VCL. If more than one, first will be the default. Backends can be grouped into 'directors' to apply load balancing.
  18. 18. Sample backend definition backend default { .host = "127.0.0.1"; .port = "8080"; }
  19. 19. Advanced configuration
  20. 20. Introduction to VCL ● Provides a window into Varnish's state during the request/response cycle. ● Not a full programming language. ● C/Perl-like syntax. ● Compiled into a C shared object at runtime and loaded into Varnish.
  21. 21. VCL Subroutines ● ● Dispatched at different times in the request/response cycle. Each routine has default code: – Varnish appends this to the end of any code you add. – Can be short-circuited by using the return keyword. Be careful! ● Allowed return values documentation. ● You can also define your own.
  22. 22. VCL Subroutines (cont) ● vcl_init ● vcl_miss ● vcl_recv ● vcl_fetch ● vcl_pipe ● vcl_deliver ● vcl_pass ● vcl_error ● vcl_hash ● vcl_fini ● vcl_hit
  23. 23. VCL Processing Cycle
  24. 24. VCL Functions ● Functions Varnish makes available to you: – – regsub(str, regex, sub) – regsuball(str, regex, sub) – ban(ban expression) – ban_url(regex) (Deprecated) – purge() – ● hash_data(str) return() Also have set and unset keywords to manipulate variables and call to dispatch your own subroutines.
  25. 25. vcl_recv ● ● Called once the OS kernel has given the complete request to Varnish. Typical uses: – Set the backend to use. – Amend the request. – Add extra information to the request. – Force a bypass of cache lookups (pass or pipe).
  26. 26. vcl_fetch ● ● Called when a response is received from a backend, before it is considered for inclusion in the cache. Typical uses: – Amend or set a TTL. – Add or remove HTTP headers. – Detect errors (more later). – Turn on ESI processing.
  27. 27. Select a backend backend default { .host = "192.168.2.1"; .port = "8080"; } backend bar { .host = "192.168.2.2"; .port = "8080": } sub vcl_recv { if (req.http.host ~ "bar.com") { set req.backend = bar; } // etc }
  28. 28. Remove cookies sub vcl_recv { if (req.url !~ "^/admin/.*") { unset req.http.cookie; } } sub vcl_fetch { if (req.url !~ "^/admin/.*") { unset beresp.http.set-cookie; } }
  29. 29. Set default TTL sub vcl_fetch { if (! beresp.http.cache-control && beresp.http.content-type ~ "text/html| text/xml|application/json") { set beresp.ttl = 5m; } }
  30. 30. VMOD's ● Allow you to extend Varnish's features. ● Written in C. ● ● Community VMOD's at https://www.varnish-cache.org/vmods Examples: – Geo-IP lookup (BBC) – Query string normalisation – Memcache
  31. 31. Grace mode ● ● ● Graced content is an object that has expired but is kept in cache. Grace mode is when a graced object is used. When this can happen: – No healthy backends are available. – Deliver old content while fetching new content is pending.
  32. 32. Configuring grace sub vcl_recv { if (req.backend.healthy) { set req.grace = 30s; } else { set req.grace = 1h; } } sub vcl_fetch { set beresp.grace = 1h; }
  33. 33. Health checks backend foo { .host = "192.168.2.1"; .port = "8080"; .max_connections = 10; .probe = { .url = "/test"; .interval = 5s; .timeout = 1s; .window = 5; .threshold = 3; } }
  34. 34. Saint mode ● ● ● Allows you to say that a backend is 'sick' for one url. Varnish will not make further requests to that backend for the url for the time specified. Gives you a chance to detect errors in responses.
  35. 35. Saint mode example sub vcl_fetch { if (beresp.status ~ “^5d{2}$”) { set beresp.saintmode = 10s; return(restart); } }
  36. 36. Edge Side Includes ● ● Allows different content on the same page to be cached for different times. Request flow: – – ● External request returns a page with special ESI tags in it. For each ESI tag Varnish issues a sub request which generates the content. Both 'master' and sub requests can be cached.
  37. 37. Configuring ESI's ● Done in vcl_fetch: – ● ● set beresp.do_esi = true; Need to configure app to support 'layouts' and 'components'. Various frameworks have support for ESI's.
  38. 38. ESI Example <!DOCTYPE html> <html> <head> <title>ESI example</title> </head> <body> <esi:include src="/route/to/content"/> </body> </html>
  39. 39. Thanks for listening! ● Varnish docs: https://www.varnish-cache.org/docs/3.0/tutorial/inde ● Any questions? ● Feel free to contact me: – @JCook21 – jeremycook0@gmail.com

×