varnish cache - linuxema/slides/varnish.pdfvarnish 101 i web server accelerator aka caching http...
TRANSCRIPT
![Page 1: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/1.jpg)
Varnish Cache
Emanuele Rocca
ZenMate DevOps Day 2
![Page 2: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/2.jpg)
Web performance
300x - 1000x speedup
![Page 3: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/3.jpg)
Outline
I Introduction
I Design principles
I Object storage
I Architecture
I Conclusions
![Page 4: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/4.jpg)
Varnish 101
I Web server accelerator AKA caching HTTP reverse proxy
I Really fast. Delivery speedup 300x - 1000x
I Client <-> Varnish <-> Backend
![Page 5: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/5.jpg)
Caching
I Cached responses are two order of magnitude faster
I Maximize cache hits
I Contents are stored in cache according to the backendresponse (Cache-Control header)
I Caching behavior can be changed using policies written inVCL, the Varnish Configuration Language
![Page 6: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/6.jpg)
Basic config
/etc/default/varnish
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/example.vcl \
-S /etc/varnish/secret \
-s malloc,256m"
![Page 7: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/7.jpg)
Basic config
/etc/varnish/example.vcl:
vcl 4.0;
backend example {
.host = "www.varnish-cache.org";
.port = "80";
}
![Page 8: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/8.jpg)
VCL vs. Settings
I The configuration is written in VCL
I Not switching settings on or off
I It is transformed into C code, built, loaded and executed uponvarnish startup
I Writing policies on how incoming traffic should be handled
![Page 9: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/9.jpg)
varnishadm
I Stopping and starting the cache processI Loading VCLI Adjusting the built-in load balancerI Invalidating cached content
![Page 10: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/10.jpg)
varnishlog
I Varnish does not log to diskI Logs are streamed to a chunk of memoryI varnishlog allows to connect to the stream and inspect the logs
![Page 11: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/11.jpg)
Design principles
I Focus on performance and flexibility
I Design for today
![Page 12: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/12.jpg)
Performance and flexibility
I Multithreaded
I Log to memory to reduce lock-contention between threads
I Binary search tree to quickly store and retrieve cached items
![Page 13: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/13.jpg)
Design for today
I 64-bit architectures, multi-core scalability, advanced OSfeatures
I Leave it to the OS to decide where memory is. Just request alarge chunk of memory
I epoll instead of select(2), poll(2)
![Page 14: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/14.jpg)
epoll
I On high loads the one process/thread per connectionarchitecture does not provide good performance
I epoll(7), introduced in Linux 2.6
I O(1) instead of O(n) to monitor n file descriptors
I http://kovyrin.net/2006/04/13/epoll-asynchronous-network-programming/
![Page 15: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/15.jpg)
Object storage
I Objects are stored in memory. References are kept in a tree,not in a hash table. Each node has a key
I Keys are potentially arbitrarily long. Users can choose what touse as a key
![Page 16: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/16.jpg)
Default key
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (lookup);
}
![Page 17: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/17.jpg)
Problems with long keys
I Storage requirements
I The tree can quickly become unbalanced
![Page 18: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/18.jpg)
Solution
I Keys are cryptographically hashed with SHA256 to ensurecompression and randomness
I Anything can be used as a key (user identification, cookies. . . )
I Simple tree implementations can be used without worringabout inbalance
![Page 19: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/19.jpg)
Architecture
The varnishd program spawns two processes: manager and worker.
root 14730 Ss 17:59 0:00 /usr/sbin/varnishd
nobody 14731 Sl 17:59 0:00 \_ /usr/sbin/varnishd
![Page 20: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/20.jpg)
Varnish manager
I Talks to the administrator
I Runs as root in order to open privileged ports
I Compiles the VCL program to be executed by the worker
![Page 21: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/21.jpg)
Varnish worker
I Child of manager with minimal permissions
I Does all the actual work with HTTP traffic
I Restarted by the manager if it dies
![Page 22: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/22.jpg)
VCL programs
I Can be compiled and executed at any time
I No need to restart the worker
I No missed HTTP requests
![Page 23: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/23.jpg)
Shared memory
I One segment of shared memory used to report and logactivities and status
I Another segment for statistics and counters. Real-time, downto microsecond monitoring of cache hit-rate, resource usageand performance indicating metrics
![Page 24: Varnish Cache - Linuxema/slides/varnish.pdfVarnish 101 I Web server accelerator AKA caching HTTP reverse proxy I Really fast. Delivery speedup 300x - 1000x I Client Varnish](https://reader036.vdocuments.mx/reader036/viewer/2022070922/5fbab864e66ee11a2d6eafc9/html5/thumbnails/24.jpg)
Conclusions
I Varnish is a very efficient and flexible web server accelerator
I Configured through a language called VCL. Configurationchanges do not require restarts
I Data is stored in virtual memory
I Designed for today
I Next steps: learn VCL and play with it!