42

getting started with varnish

Embed Size (px)

Citation preview

Page 1: getting started with varnish
Page 2: getting started with varnish

Varnish Cache Step by Step

Federico G. Schwindt Technical Presales Engineer

Varnish Software

Page 3: getting started with varnish
Page 4: getting started with varnish

About Varnish Software● The company behind Varnish Cache

● Offers “Varnish Plus” - consisting of:

○ Additional software

○ 24/7 support

○ Professional services

Page 5: getting started with varnish

Insert business logic here

Page 6: getting started with varnish

About Varnish Cache

• Web app accelerator

• Fast

• we measure TTBF in µs

• Flexible

• Some assembly required :-)

Page 7: getting started with varnish

VCL

• Varnish Configuration Language

• Applies to transactions that flow through the cache

• You can hook custom logic into the flow

• Main hooks used: vcl_recv{} and vcl_backend_response{}

Page 8: getting started with varnish
Page 9: getting started with varnish

GoalKnowing what to do to speed up your website.

Page 10: getting started with varnish
Page 11: getting started with varnish

Designing a web app for Varnish

● Build apps from the ground up for Varnish

● Intelligent cache usage with cache headers

● Proper cache invalidation

● Monitoring

Page 12: getting started with varnish
Page 13: getting started with varnish

Incremental implementations

● Deploy Varnish on existing apps

● Gradually increase caching

● “Low hanging fruits” first

Page 14: getting started with varnish

Requirements

● Linux or FreeBSD

● Virtualization is OK, not awesome

● Tuning is mostly for high traffic

Page 15: getting started with varnish

Step 1: Installing VarnishDifficulty level: Easy

Payoff: Minor

Page 16: getting started with varnish

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: getting started with varnish
Page 18: getting started with varnish

Step 2: Cache static assets

Difficulty level: Trivial

Payoff: Minor

Page 19: getting started with varnish

Step 2: Cache static assets

● Cache images, CSS, Javascript

● Backend concurrency decrease

● Backend bandwidth decrease

● Minor CPU usage decrease

Page 20: getting started with varnish

Step 3: CompressionDifficulty level: Trivial

Payoff: Medium

Page 21: getting started with varnish
Page 22: getting started with varnish

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: getting started with varnish

Step 4: Semi-static caching

Difficulty level: Medium

Payoff: High

Page 24: getting started with varnish

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: getting started with varnish

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: getting started with varnish

Difficulty level: Medium Payoff: High

Step 5: Autopurge

Page 27: getting started with varnish

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: getting started with varnish

Difficulty level: Easy

Payoff: High

Step 6: Grace mode

Page 29: getting started with varnish
Page 30: getting started with varnish

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: getting started with varnish

Grace mode (2)

• Period set via beresp.grace

• Serving actually controlled in vcl_hit{}

• Tip: Use 10s normally and 1 hour when backend is down

Page 32: getting started with varnish

Difficulty level: Hard

Payoff: High

Step 7: Content composition with ESI

Page 33: getting started with varnish

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: getting started with varnish

TTL: 30min

TTL: 30minTTL: 30min

TTL: 30min

TTL: 60min, user specific

TTL: 1min

TTL: 1800minutes

Edge Side Includes (ESI)

Page 35: getting started with varnish

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: getting started with varnish

<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: getting started with varnish

Difficulty level: HardPayoff: High

Step 8: Advanced cache invalidation

Page 38: getting started with varnish

Step 8: Advanced cache invalidation (1/2)

● 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: getting started with varnish

Advanced cache invalidation (2/2)

● Tag pages with custom headers

○ X-Skus: 49834, 34321, 4343

● Execute with bans or enhanced cache invalidation

● Possible database driven expiry (triggers)

Page 40: getting started with varnish

Step 9 TCP/IP Tuning

• The defaults in Linux are great

• if you are Linus Torvalds and you’re browsing with Arena

• somaxconn, rmem, wmem, fin timeout, ...

Page 41: getting started with varnish

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 42: getting started with varnish

Q & A