speed up your site with varnish

17
Speed up your site with Varnish PHP Cambridge, 21st March 2017 Simon Jones, Studio 24

Upload: simon-jones

Post on 15-Apr-2017

643 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Speed up your site with Varnish

Speed up your site with Varnish

PHP Cambridge, 21st March 2017 Simon Jones, Studio 24

Page 2: Speed up your site with Varnish

Typical web application response

Apache DatabasePHP

Public internet

Webserver

Incoming web request

Returningweb response

Page 3: Speed up your site with Varnish

• 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

Page 4: Speed up your site with Varnish

• 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

Page 5: Speed up your site with Varnish
Page 6: Speed up your site with Varnish

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)

Page 7: Speed up your site with Varnish

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

Page 8: Speed up your site with Varnish

• 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?

Page 9: Speed up your site with Varnish

• 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

Page 10: Speed up your site with Varnish

• 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

Page 11: Speed up your site with Varnish

Downsides

• Does not support SSL

• Does not support unique user state (e.g. cookies)

Page 12: Speed up your site with Varnish

• 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

Page 13: Speed up your site with Varnish

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)

Page 14: Speed up your site with Varnish

• Hitch (TLS proxy)

• Nginx (pass-thru requests)

• Load balancer (terminates SSL)

SSL termination options

Page 15: Speed up your site with Varnish

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

Page 16: Speed up your site with Varnish

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/

Page 17: Speed up your site with Varnish

Thanks!

@simonrjones @studio24

www.studio24.net