speed up your site with varnish
Post on 15-Apr-2017
646 Views
Preview:
TRANSCRIPT
Speed up your site with Varnish
PHP Cambridge, 21st March 2017 Simon Jones, Studio 24
Typical web application response
Apache DatabasePHP
Public internet
Webserver
Incoming web request
Returningweb response
• Can be slow
• Each requests boots up Apache + PHP
• Example memory footprint
• 60MB per request
• 100 concurrent users
• = 6GB memory usage
• Response time 300-900ms for a typical CMS application
Problems
• CMS-level caching
• E.g. cache content fragments to file / memcached
• Still boots Apache, PHP and CMS application
• Full-page caching
• E.g. cache whole page to HTML
• Faster, still boots Apache + PHP
Caching options
Add Varnish
Public internet
Webserver
Incoming web request
(port 80)
Returningweb response
Public internet
Webserver
Incoming web request
(port 80)
Returningweb response Varnish Cache
HTTP reverse proxy
(port 8080)
How does Varnish work?
Public internet
Webserver
1) Incoming web request
(port 80)
Varnish Cache
Yes
4) Return HTTP response to user
3) Varnish returns page from memory
2) Varnish asks is this page cached?
Public internet
Webserver
1) Incoming web request
(port 80)
6) Return HTTP response to user
Varnish Cache
2) Varnish asks is this page cached?
No
3) Pass through to webserver (port 8080)
4) Return HTTP response
5) Varnish caches page
• Caches HTTP response in memory
• Skips Apache, PHP & MySQL
• Each request for same URL = the same for all users(headers & contents)
• Very, very fast
How does Varnish work?
• Crossrail on standard LAMP stack
• Traffic spikes of 20,000 hits/day caused issues
• Response time 500-800ms
• Implemented Nginx + Varnish
• Response time sub 100ms
• Traffic peaks of 150,000 hits/day cause no issues
Case study
• Use standard HTTP headers, Varnish will do the rest
• Cache GET & HEAD only
• Cache-Control: public, max-age=3600
• Cache-Control: private, no-cache, no-store
HTTP responses
Downsides
• Does not support SSL
• Does not support unique user state (e.g. cookies)
• If POST, works fine
• Exclude areas of your site (e.g. cart/*)
• Use AJAX / JS / LocalStorage to manage functionality (e.g. Logged in message, cart contents)
• Use Varnish ESI (Edge Side Includes)
• Use Edgestash in Varnish Plus (mustache templates & JSON data)
Strategies for dealing with state
Add SSL termination
Public internet
Webserver
Incoming web request
(port 443)
Returningweb response Varnish Cache
HTTP reverse proxy
After SSL termination
(port 80) (port 8080)
• Hitch (TLS proxy)
• Nginx (pass-thru requests)
• Load balancer (terminates SSL)
SSL termination options
Summary
• Brilliant at dealing with high traffic spikes
• Serves the same page for everyone
• Use HTTP headers to control caching
• Consider how to deal with unique content for users
• SSL termination strategy
Resources
• Varnish HTTP Cache https://www.varnish-cache.org/
• Getting started with Varnish cache bookhttps://info.varnish-software.com/getting-started-with-varnish-cache-oreilly-book
• The Varnish book https://info.varnish-software.com/the-varnish-book
• Hitch TLS proxy http://hitch-tls.org/
top related