Varnish Cache - step by step

41

description

 

Transcript of Varnish Cache - step by step

Page 1: Varnish Cache - step by step
Page 2: Varnish Cache - step by step

Varnish CacheStep by step

Page 3: Varnish Cache - step by step
Page 4: Varnish Cache - step by step

Insert business logic here

Page 5: Varnish Cache - step by step

About Varnish Cache

● Web app accelerator

● Fast

● Flexible

Page 6: Varnish Cache - step by step

VCL

• Varnish Configuration Language

• Applies to transactions that flow through the cache

• Hook custom logic into the flow

• Main hooks used: vcl_recv and vcl_fetch

Page 7: Varnish Cache - step by step
Page 8: Varnish Cache - step by step

About Varnish Software● The company behind Varnish Cache

● Offers “Varnish Plus” - consisting of:

○ Additional software

○ 24/7 support

○ Professional services

Page 9: Varnish Cache - step by step

GoalKnowing what to do to speed up your website.

Page 10: Varnish Cache - step by step
Page 11: Varnish Cache - step by step

Designing a web app for Varnish

● Build apps from the ground up for Varnish

● Intelligent cache usage

● Proper cache invalidation

● Monitoring

Page 12: Varnish Cache - step by step
Page 13: Varnish Cache - step by step

Incremental implementations

● Deploy Varnish on existing apps

● Gradually increase caching

● “Low hanging fruits” first

Page 14: Varnish Cache - step by step

Requirements

● Linux or FreeBSD

● Virtualization is OK, not awesome

● Tuning is only for high traffic

Page 15: Varnish Cache - step by step

Step 1: Installing VarnishDifficulty level: Easy

Payoff: Minor

Page 16: Varnish Cache - step by step

Step 1: Pass through only● Passive Varnish

○ return(pass) in vcl_recv

● Powerful load balancing

● Health checks built in

● Excellent for debugging webapps

Page 17: Varnish Cache - step by step
Page 18: Varnish Cache - step by step

Step 2: Cache static assets

Difficulty level: Trivial

Payoff: Minor

Page 19: Varnish Cache - step by step

Step 2: Cache static assets

● Cache images, CSS, Javascript

● Backend concurrency decrease

● Backend bandwidth decrease

● Minor CPU usage decrease

Page 20: Varnish Cache - step by step

Step 3: CompressionDifficulty level: Trivial

Payoff: Medium

Page 21: Varnish Cache - step by step
Page 22: Varnish Cache - step by step

Step 3: Compression● GZip compress CSS/Javascript

○ JQuery standard: 263KB

○ JQuery minified: 91KB

○ JQuery gzipped: 32KB

● Compress in one place only

● Reuse compression

● Move CPU load from backend

Page 23: Varnish Cache - step by step

Step 4: Semi-static caching

Difficulty level: Medium

Payoff: High

Page 24: Varnish Cache - step by step

Step 4: Semi-static content caching

● Dynamically rendered content

● Expensive to render

● Almost never changes

● Set short TTL (10-120 seconds)

● Mitigates traffic peaks

Page 25: Varnish Cache - step by step

Step 4: Semi static (2/2)

● Manual cache expiry

● Magic URLs

● Shift-refresh from special IP-range

● Web interface for ban or purge

Page 26: Varnish Cache - step by step

Difficulty level: Medium Payoff: High

Step 5: Autopurge

Page 27: Varnish Cache - step by step

Step 5: Autopurge● Backend generates purge requests

● Set TTL to ~forever

● Invalidate on change

● Can be difficult for composite pages

○ Map relationships between data and pages

Page 28: Varnish Cache - step by step

Difficulty level: Easy

Payoff: High

Step 6: Grace mode

Page 29: Varnish Cache - step by step
Page 30: Varnish Cache - step by step

Step 6: Grace mode

• Originally developed to mitigate pile-ups

• Can serve “graced” content when fresh content is unavailable

• Unavailable means

• backend down

• object busy

Page 31: Varnish Cache - step by step

Grace mode (2)

• Both req.grace and beresp.grace must accept the old object

• Tip: Set req.grace to 10s normally and 1 hour when backend is down

Page 32: Varnish Cache - step by step

Difficulty level: Hard

Payoff: High

Step 7: Content composition with ESI

Page 33: Varnish Cache - step by step

Article snippet 1

Article snippet 4

Article snippet 3

Article snippet 2

Welcome, Foobar (logout)

Top 10 popular news items

Horrible Designed News Site Blah

Edge Side Includes (ESI)

Page 34: Varnish Cache - step by step

TTL: 30min

TTL: 30minTTL: 30min

TTL: 30min

TTL: 60min, user specific

TTL: 1min

TTL: 1800minutes

Edge Side Includes (ESI)

Page 35: Varnish Cache - step by step

Step 7: Content composition

● Edge Side Includes (ESI)

● Split web pages into pieces

○ Individual caching policies

○ One piece = one URL

● Live stitching of content

● Works with compressed content as well

Page 36: Varnish Cache - step by step

<html><body>

... <esi:include src="/frontpage/breaking" />

... <esi:include src="/frontpage/latest" />

... <!-- Do we like the cache to meddle with content? -->

</body><html>

Page 37: Varnish Cache - step by step

Difficulty level: HardPayoff: High

Step 8: Advanced cache invalidation

Page 38: Varnish Cache - step by step

Step 8: Advanced cache invalidation

● Sometimes a page relies on multiple “objects” for invalidation

● Finding out what object need to expire what pages can be hard

● Typically a problem in e-commerce and media

Page 39: Varnish Cache - step by step

Advanced cache invalidation (2/2)

● Tag pages with custom headers

○ X-Skus: 49834, 34321, 4343

● Execute with bans or hashninja

● Possible database driven expiry (triggers)

Page 40: Varnish Cache - step by step

Conclusions● High rewards with little work

● 3 - 15 days to reach “high” payoff

● Requires backend knowledge to do significant changes

● We talked about what to do - if you want to learn more about how please read the book or sign up for training.

Page 41: Varnish Cache - step by step

Q & A