Varnish bof

58
Varnish BOF!

description

#drupaldevdays birds of a feather talk on Varnish and ESI - Edge Side Includes - at Brussels.

Transcript of Varnish bof

Page 1: Varnish bof

VarnishBOF!

Page 2: Varnish bof

What's this talk about?

• A basic intro to Varnish and reverse proxies• Some cool ESI stuff• How to setup, implement + configure

Varnish/ESI in Drupal• Teh Awesomeness of speed

Page 3: Varnish bof

Why Varnish?

Page 4: Varnish bof

Drupal performs…just…

Page 5: Varnish bof

How much faster?

20 reqs/sec?

200 reqs/sec?

Drupal

Varnish

Page 6: Varnish bof

When personalisation goes wrong

Page 7: Varnish bof

Dries logs in

Page 8: Varnish bof

Drupal creates a personalised page

Logged in as dries

Page 9: Varnish bof

The page gets cached

Varnish

Page 10: Varnish bof

When webchick hits the site

Page 11: Varnish bof

The page is fetched from the cache

Varnish

Page 12: Varnish bof

The personalisation is incorrect

Logged in as dries

Page 13: Varnish bof

WTF?

WTF?

Page 14: Varnish bof

No-cache headers can prevent this

Varnish

Page 15: Varnish bof

But now it's slow :-(

Page 16: Varnish bof

EdgeSideIncludes

Page 17: Varnish bof

ESI: Tales from the past

• The idea originated in SSI• SSI, but moved to the edge• ESI is a W3C standard• ESI is a bunch of xml tags/attributes.• Supported by:– Varnish (partial support – no ESI variables)– Akamai– Some Nginx support– ???

Page 18: Varnish bof

Dries is visiting the site

Page 19: Varnish bof

The page is requested

Varnishhttp://example.com/node/1

Page 20: Varnish bof

The page includes an ESI tag

<esi src= "http://example.com/esi/user/0" />

Page 21: Varnish bof

The proxy sends a second request

Varnish

http://example.com/esi/user/0

http://example.com/node/1

Page 22: Varnish bof

The ESI URL sends a HTML snip

<div id="userinfo"> <a title="View &amp; edit your user profile">Logged in as dries</a> <a href="/logout">Logout</a></div>

Page 23: Varnish bof

The ESI code is merged with the page

Page 24: Varnish bof

Proxy forwards the page on

Varnish

http://example.com/esi/user/0

http://example.com/node/1

Page 25: Varnish bof

This doesn’t sound quicker

Page 26: Varnish bof

The Importance of Tests

• It's important to measure the results(I'll come to measuring tools later!)

• The ESI snippets can be cached too

Page 27: Varnish bof

Cached ESIs

Page 28: Varnish bof

1st Page = 2 Requests

Varnishhttp://example.com/node/1

http://example.com/esi/user/0

Page 29: Varnish bof

2nd Page = 1 request

Varnishhttp://example.com/node/2

Page 30: Varnish bof

1 request for each new page

• 10 pages = 11 requests• Each page is cached• The ESI tag is cached

Page 31: Varnish bof

The next visitor gets the page for free

VarnishPage fetched from cache

http://example.com/node/1

Page 32: Varnish bof

1st page = 1 request (for the ESI)

Varnish

http://example.com/esi/user/0

Page fetched from cachehttp://example.com/node/1

Page 33: Varnish bof

Following pages all served from cache

Varnish

Page 34: Varnish bof

1 request for each new page

• 10 pages = 11 requests• The ESI tag is cached• Each page is cached• Subsequent requests have 1 request for the

ESI tag• 20 pages = 12 requests (10 pages + 2 ESI tags)

Page 35: Varnish bof

Fast enough yet?

Warm the cache

Page 36: Varnish bof

A modern infrastructure

Varnish HA Proxy

TehIntertubes

Service webheadadmin/cron/emails/??

Page 37: Varnish bof

Spidering the site

Varnish HA Proxy

TehIntertubes

Service webheadadmin/cron/emails/??

Link-checker /spider app.

Page 38: Varnish bof

Dries is back for more

Page 39: Varnish bof

Each user generates 1 request

Varnish

http://example.com/esi/user/0

Page fetched from cachehttp://example.com/node/1

Page 40: Varnish bof

5 users, 40 pages

• 5 backend requests!

Page 41: Varnish bof

Theory Over

Page 42: Varnish bof

Basics Varnish

+Drupal

Page 43: Varnish bof

Installing Varnish

• sudo apt-get install varnishsudo yum install varnish

• Edit /etc/default/varnish• Edit /etc/varnish/default.vcl• Usual config:– Varnish listens on port 80– Apache listens on port 8080 (or any available port)– SSL is more complex – sometimes Pound is used

for SSL decryption

Page 44: Varnish bof

VCLs are awesome!

• Varnish configuration is controlled by VCL: Varnish Configuration Language.

• Documented athttp://www.varnish-cache.org/trac/wiki/VCL

• Drupal VCL examples can be found in:– Mercury (Amazon AMI)– ESI module

• You can write C code in VCLs. Oh my.

Page 45: Varnish bof

Keeping the cache current

Varnish

Varnish admin port

Page 46: Varnish bof

Varnish Management Port

• The Varnish admin port uses a plain-text protocol to command Varnish

• Test by:telnet localhost 6093

• List commands by typing help• Purge cached pages: url.purge <regexp>

E.g. url.purge .*• The Varnish module provides an API.

Page 47: Varnish bof

Use with caution!

The default behaviour of the Varnish module is to

clear the entire cachewhen you edit a node.

Page 48: Varnish bof

Enter…the Expires module

• Came from the Boost module• Clears specific cache pages – not the entire

cache• Supports path-alias URLs as well as node/1

paths.• Provides hook_expire_cache_alter to add URLs

(e.g. nodes displayed in a page-view)• Supports other cache modules, not just Varnish.

Page 49: Varnish bof

I can haz ESI?

• The ESI module provides block integration

Page 50: Varnish bof

Configuring ESI

• Download the module• Configure the blocks to serve via ESI, and their

TTL• Modify the Varnish VCL (the ESI module comes

with example VCLs to use• Coming soon:– D7– Panels integration

Page 51: Varnish bof

Benchmarking

• Some tools:– J-meter– ab (apache-benchmark)– Seige

• Approaches:– Hammer your URL(s)

ab –n 100 –c5 http://example.com/– Replay your log files

Page 52: Varnish bof

Pitfalls await

• VCLs are complex• Per-user cache rules are

even scarier!• Varnish may be slower• Risk exposing private

data

Page 53: Varnish bof

Alternatives

• Boost module– No need to install a new service on the server– Static file cache

• Advanced cache module• Authcache module• Other proxies:– Squid– Nginx– Akamai?

Page 54: Varnish bof

Summary

Page 55: Varnish bof

How hard?

Page 56: Varnish bof

Teh Awesomeness

Varnish

Varnish module

Expire module

ESI module

Page 57: Varnish bof

Thanks!

Marcus Deglose: [email protected]: @manarthirc: manarth

Page 58: Varnish bof

Picture creditsThanks to these CC-licenced flickr users• Mac Book Pro - http://www.flickr.com/photos/wicho/• F1 car - http://www.flickr.com/photos/w3i_yu/• Banger car - http://www.flickr.com/photos/photoplod• Dries - http://www.flickr.com/photos/boris• Dries+laptop - http://www.flickr.com/photos/azugaldia• Webchick - http://www.flickr.com/photos/mortendk • Webchick - http://www.flickr.com/photos/puregin• Fire - http://www.flickr.com/photos/benwatts• Hourglass - http://www.flickr.com/photos/opendemocracy• Stopwatch -

http://www.flickr.com/photos/purplemattfish