Professional Documents
Culture Documents
* https://www.varnish-cache.org
Tuesday, 25 October 11
WHY? FAST!
If
Theoretical
* http://kristianlyng.wordpress.com/2010/10/23/275k-req/
Tuesday, 25 October 11
cache control?
Expensive
Not
CDNs? 20/GB?
enough hardware?
Backend
Tuesday, 25 October 11
struggling?
IT WONT
Do
Tuesday, 25 October 11
/ CentOS
Debian
apt-get install varnish
Tuesday, 25 October 11
USE 64BIT*
Its all about virtual memory: Things like
stack size suddenly matter on 32bit. If you
must use Varnish on 32-bit, youre somewhat
on your own. However, try to fit it within
2GB. I wouldnt recommend a cache larger
than 1GB, and no more than a few hundred
threads (Why are you on 32bit again?)
* http://kristianlyng.wordpress.com/2010/01/26/varnish-best-practices/
Tuesday, 25 October 11
SYSTEM CONFIG
The
minimum.
Do
Enable
Tuesday, 25 October 11
THREADS
Adjust
thread_pool_add_delay=2
thread_pools = <Number of cpu cores>
thread_pool_min = <800/number of cpu cores>
thread_pool_max = 4000
session_linger = 50
sess_workspace = <16k to 5m>
The only thing that made a real difference while tuning Varnish
was the number of threads. And making sure it actually caches.
Beyond that, it really doesnt matter much. Our defaults are
good.
* http://kristianlyng.wordpress.com/2010/01/26/varnish-best-practices/
Tuesday, 25 October 11
STORAGE
Memory
Got
is fast, duh.
Tuesday, 25 October 11
VCL
Varnish
Configuration Language
...
Defined
Tuesday, 25 October 11
VCL
Default VCL is defined in
/etc/varnish/default.vcl
Then create your custom VCL
-f /var/custom.vcl
in
* https://www.varnish-cache.org/trac/wiki/VCLExampleDefault
Tuesday, 25 October 11
request
lookup
fetch
deliver
Tuesday, 25 October 11
to client
recv
hash
hit
pipe
miss
fetch
deliver
pass
request
lookup
recv
hash
hit
pipe
miss
fetch
deliver
Tuesday, 25 October 11
to client
deliver
pass
recv
hash
pass
fetch
through to backend
response
deliver
Tuesday, 25 October 11
to client
hit
pipe
miss
fetch
deliver
pass
PIPE
request
pipe
recv
hash
hit
pipe
miss
fetch
deliver
Tuesday, 25 October 11
pass
PROBES
Define
common health
checks for multiple
backends.
probe health {
.url = "/healthCheck?onFailure=true";
.expected_response = 200;
.interval= 15s;
.timeout = 5s;
.window= 4;
.threshold = 4;
}
BACKENDS
Define
Or
named.
backend mysite {
.host = "www.example.com";
.port = "http";
.probe = "health";
}
Tuesday, 25 October 11
DIRECTORS
Group
Tuesday, 25 October 11
YMMV
I
Random
Tuesday, 25 October 11
DESIGN DECISIONS
Share
nothing.
Easy to scale horizontally.
Great if your site is not transactional.
Backend session affinity via LB set cookie.
LB
Tuesday, 25 October 11
Set-Cookie=BackendID
varnish
varnish
varnish
app
app
app
DESIGN DECISIONS
Share
everything.
Varnish loves RAM, scale vertically.
Varnish cache becomes the shared source of truth.
LB
varnish
app
Tuesday, 25 October 11
app
app
Tuesday, 25 October 11
Set-Cookie=VarnishID
varnish
varnish
varnish
app
app
app
TERMINATE SSL.
Get
Set-Cookie=VarnishID
Port=https
varnish
app
:80
:443
if (req.http.Port ~ "https") {
set req.backend = appa_80;
} else {
set req.backend = appa_443;
}
Tuesday, 25 October 11
VCL TRICKS
Rewrite headers to/from production.com.au
testdomain.com
to
vcl_recv
set req.http.Host = regsuball(req.http.Host, "testdomain\.com", "com\.au");
set req.http.Referer = regsuball(req.http.Host, "testdomain\.com", "com\.au");
vcl_deliver
set
set
set
set
Tuesday, 25 October 11
VCL TRICKS
Add some debug
vcl_deliver
if (obj.hits > 0) {
set resp.http.X-Varnish-Hit = "++";
set resp.http.X-Varnish-Hits = obj.hits;
set resp.http.X-Varnish-ServerID = regsub(req.http.Cookie, ".*ServerID=(\d+).*", "\1");
} else {
set resp.http.X-Varnish-Hit = "--";
set resp.http.X-Varnish-ServerID = regsub(req.http.Cookie, ".*ServerID=(\d+).*", "\1");
}
Tuesday, 25 October 11
VCL TRICKS
Normalize the Accept-Encoding
usage in vcl_recv
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
remove req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
remove req.http.Accept-Encoding;
}
}
Tuesday, 25 October 11
VCL TRICKS
Remove
Remember, varnish
Tuesday, 25 October 11
VCL TRICKS
Pass
on the client IP to the backend servers using the XForwarded-For header in vcl_recv
remove req.http.X-Forwarded-For;
setreq.http.X-Forwarded-For = client.ip;
Tuesday, 25 October 11
VCL TRICKS
Let varnish keep
vcl_fetch
Let
if (! req.backend.healthy) {
set req.grace = 2h;
} else {
set req.grace = 30s;
}
Can
Tuesday, 25 October 11
VCL TRICKS
Force
Tuesday, 25 October 11
TTL
Varnish
Cache-Control: s-maxage
Cache-Control: max-age
Can
Tuesday, 25 October 11
Content-Length on backends
Serve
Pipe
Tuesday, 25 October 11
MONITORING VARNISH
Handy command
varnishstat
varnishlog
varnishtop
varnishadm
Tuesday, 25 October 11
line tools
VARNISHSTAT
varnishstat
Filtered
varnishstat -f \
client_conn,client_drop,client_req,cache_hit,cache_hitpass,cache_miss,backend_conn,backend_fail
Tuesday, 25 October 11
VARNISHSTAT
Log
#!/bin/bash
logdir=/var/log/perf/`hostname`/latest
mkdir -p $logdir
while true
do
varnishstat -x >> "$logdir/varnishstat.xml"
sleep 60
done
Then
Tuesday, 25 October 11
VARNISHLOG
BackendOpen
TxRequest
TxURL
TxProtocol
TxHeader
TxHeader
TxHeader
TxHeader
TxHeader
TxHeader
RxProtocol
RxStatus
RxResponse
RxHeader
RxHeader
RxHeader
RxHeader
RxHeader
RxHeader
RxHeader
Fetch_Body
Length
BackendClose
Tuesday, 25 October 11
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
VARNISHTOP
Monitor top URLs hitting
varnishtop -i txurl
backend
Tuesday, 25 October 11
VARNISHADM
# varnishadm
CLI connected to 127.0.0.1 6082
200
----------------------------Varnish Cache CLI 1.0
----------------------------Linux,2.6.32-131.12.1.el6.x86_64,x86_64,-sfile,-smalloc,-hcritbit
Type 'help' for command list.
Type 'quit' to close CLI session.
varnish> help
200
help [command]
ping [timestamp]
auth response
quit
banner
status
start
stop
vcl.load <configname> <filename>
vcl.inline <configname> <quoted_VCLstring>
vcl.use <configname>
vcl.discard <configname>
vcl.list
Tuesday, 25 October 11
MORE HELP?
Talk
Get
Consider
Tuesday, 25 October 11