+ All Categories
Home > Technology > HTTP Acceleration with Varnish

HTTP Acceleration with Varnish

Date post: 11-May-2015
Category:
Upload: harlow-ward
View: 1,448 times
Download: 8 times
Share this document with a friend
Description:
How effectively scale Rails with Varnish. These slides cover reverse-proxies, HTTP headers, and cache invalidation. We'll also touch on writing VCL (Varnish Configuration Language).
Popular Tags:
41
HTTP Acceleration with Varnish
Transcript
Page 1: HTTP Acceleration with Varnish

HTTP Acceleration with Varnish

Page 2: HTTP Acceleration with Varnish

Mobile-first 10k Hotel Partners 17 Countries ~9M Downloads

HotelTonight

Page 3: HTTP Acceleration with Varnish

I WANNA GO FAST

Page 4: HTTP Acceleration with Varnish

Overview

Reverse Proxies

HTTP Headers

Cache Invalidation

Varnish Configuration Language

Page 5: HTTP Acceleration with Varnish

Reverse proxy

In computer networks, a reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or more servers.

Page 6: HTTP Acceleration with Varnish

Request / Response Cycle

Reverse Proxy

Origin ServerClient

www.hward.com

(CNAME)

hward.herokuapp.com

(or IP Address)

Page 7: HTTP Acceleration with Varnish

Web application accelerator.

Page 8: HTTP Acceleration with Varnish

Web application accelerator.

Reverse proxy w/ caching

Page 9: HTTP Acceleration with Varnish

Cache Miss

Varnish Origin ServerClient

Cache

Page 10: HTTP Acceleration with Varnish

Cache Hit

Varnish Origin ServerClient

Cache

Page 11: HTTP Acceleration with Varnish

Initial use of VarnishStarted with a small portion of our traffic.

33% Hits, 1% Misses, 66% Passes

Page 12: HTTP Acceleration with Varnish

The best traffic is traffic that never hits our servers.

Page 13: HTTP Acceleration with Varnish

Continue to migrate endpoints to Varnish

Removed 10 EC2 XLarge Instances!

Page 14: HTTP Acceleration with Varnish

Varnish as a service.

• Dashboards and Stats

• RESTful API

• Killer support team (thanks Peter and Austin)

• Similar pricing to Cloudfront

Page 15: HTTP Acceleration with Varnish

Fastly Point of Presence (PoP)

Australia PoPs

N. America PoPs

Europe PoPs

Origin Server

N. America

Australia

Europe

Page 16: HTTP Acceleration with Varnish

Simulated traffic w/ Loader.io

Page 17: HTTP Acceleration with Varnish

73k req sec 1ms avg. response

Page 18: HTTP Acceleration with Varnish

HTTP Headers

HTTP header fields are components of the message header of requests and responses in the Hypertext Transfer Protocol.

Page 19: HTTP Acceleration with Varnish

Rails Default Behavior

Varnish Origin ServerClient

Cache

$ curl -v https://launchpad.37signals.com/basecamp -o /dev/null < HTTP/1.1 200 OK < Cache-Control: max-age=0, private, must-revalidate < Set-Cookie: _launchpad_session=[DIGEST]; path=/; HttpOnly; secure

Opt-in to Edge Cache

Page 20: HTTP Acceleration with Varnish

Edge-Cache Headers

Cache-Control Header:

public, max-age or s-maxage > 0, and remove any cookies from the response.

$ curl -v http://www.example.com -o /dev/null < HTTP/1.1 200 OK < Cache-Control: max-age=30, public

Only cache publicly accessible pages.

Page 21: HTTP Acceleration with Varnish

max-age vs.

s-maxage

Page 22: HTTP Acceleration with Varnish

max-age

s-maxageused only by proxy for content expiration

used on both client and proxy for content expiration

Page 23: HTTP Acceleration with Varnish

Cache-Control Headers for Static Assets

$ curl -v http://www.hward.com/assets/blog-f764a70dc0ca4e7bf340d1.css < HTTP/1.1 200 OK < Cache-Control: public, max-age=31557600 < Content-Type: text/css < Status: 200 OK < Via: 1.1 varnish

Page 24: HTTP Acceleration with Varnish

Cache-Control Headers for Dynamic Content

$ curl -v http://www.hward.com < HTTP/1.1 200 OK < Cache-Control: public, no-cache, s-maxage=31557600 < Content-Type: text/css < Status: 200 OK < Via: 1.1 varnish

Page 25: HTTP Acceleration with Varnish

Rails Controllers

Page 26: HTTP Acceleration with Varnish

Verify cache is warming up

$ curl -v http://www.hward.com -o /dev/null < HTTP/1.1 200 OK < X-Served-By: cache-sv61-SJC3 < X-Cache: MISS < X-Cache-Hits: 0 !$ curl -v http://www.hward.com -o /dev/null < HTTP/1.1 200 OK < X-Served-By: cache-sv61-SJC3 < X-Cache: HIT < X-Cache-Hits: 1

Page 27: HTTP Acceleration with Varnish

Cache Invalidation

• Time based expiration

• Resource based expiration

• Surrogate-Keys

Page 28: HTTP Acceleration with Varnish

Cache TTL Hit Ratio Origin Requests / Minute

5 sec 98.00% 20

30 sec 99.80% 2

1 min 99.90% 1

5 min 99.997% <1

1000 Requests / Minute

Page 29: HTTP Acceleration with Varnish

Cache TTL Hit Ratio Origin Requests / Minute

5 sec 99.96% 20

30 sec 99.996% 2

1 min 99.998% 1

5 min 99.999% <1

1000 Requests / Minute50,000

Page 30: HTTP Acceleration with Varnish

PURGE Requests

Invalidate cache with PURGE request to URL.

$ curl -v -X PURGE http://www.hward.com > PURGE / HTTP/1.1 < HTTP/1.1 200 OK < Status: 200 OK < Via: 1.1 varnish {"status": "ok", "id": "68-1392243590-232248"}

Note: In Production make sure to whitelist IP’s that can Purge.

Page 31: HTTP Acceleration with Varnish

Surrogate-Keys (Fastly Specific)

posts/1

comments/1

comments/2

http://www.fastly.com/blog/surrogate-keys-part-1

Page 32: HTTP Acceleration with Varnish

Many-to-Many Relationship Between Keys and Pages

posts/1

Page 33: HTTP Acceleration with Varnish
Page 34: HTTP Acceleration with Varnish

Purging a Surrogate-Key

posts/1

Page 35: HTTP Acceleration with Varnish

Varnish Configuration Language (VCL)

Page 36: HTTP Acceleration with Varnish

Imagery Service

640x260640x900

412x 412

Dynamically resized images for all devices.

Page 37: HTTP Acceleration with Varnish

Initially used Magickly Gem

example.com/image.jpg?resize=500x500#

Varnish S3 BucketClientMagickly

Gem

Page 38: HTTP Acceleration with Varnish

Transition to ImgIX

example.com/image.jpg?w=500&h=500&crop=fit

Varnish S3 BucketClient

Page 39: HTTP Acceleration with Varnish

Re-write URL for backwards compatibility with VCL

Page 40: HTTP Acceleration with Varnish

Additional Topics

• Vary Headers

• Logging

• Shielding

• Grace-period for stale cache

Page 41: HTTP Acceleration with Varnish

Harlow Ward @futuresanta [email protected]

$25 Promo Code: HARLOW

Questions?


Recommended