HTTP Acceleration with Varnish
HotelTonight
Mobile-first
10k Hotel Partners
17 Countries
~9M Downloads
I WANNA
GO FAST
Overview
Reverse Proxies
HTTP Headers
Cache Invalidation
Varnish Configuration Language
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.
Request / Response Cycle

Client

Reverse
Proxy

Origin Server

www.hward.com

hward.herokuapp.com

(CNAME)

(or IP Address)
Web application accelerator.
Reverse proxy
w/ caching

Web application accelerator.
Cache Miss

Client

Varnish

Cache

Origin Server
Cache Hit

Client

Varnish

Cache

Origin Server
Initial use of Varnish
Started with a small portion of our traffic.

33% Hits, 1% Misses, 66% Passes
The best traffic is traffic that never
hits our servers.
Continue to migrate endpoints
to Varnish

Removed 10 EC2 XLarge Instances!
Varnish as a service.
•

Dashboards and Stats

•

RESTful API

•

Killer support team (thanks Peter and Austin)

•

Similar pricing to Cloudfront
Fastly
Point of Presence (PoP)
N. America

Australia

Europe

N. America
PoPs

Australia
PoPs

Europe
PoPs

Origin Server
Simulated traffic w/ Loader.io
73k req sec
1ms avg. response
HTTP Headers
HTTP header fields are components of the message
header of requests and responses in the Hypertext
Transfer Protocol.
Rails Default Behavior
Opt-in to Edge Cache

Client

Varnish

Origin Server

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
Edge-Cache Headers
Only cache publicly accessible pages.

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
max-age
vs.

s-maxage
max-age
used on both client and proxy for content expiration

s-maxage
used only by proxy for content expiration
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
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
Rails Controllers
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
Cache Invalidation
•

Time based expiration

•

Resource based expiration

•

Surrogate-Keys
1000 Requests / Minute
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
50,000
1000 Requests / Minute
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
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.
Surrogate-Keys (Fastly Specific)
http://www.fastly.com/blog/surrogate-keys-part-1

posts/1
comments/1
comments/2
Many-to-Many Relationship
Between Keys and Pages

posts/1
Purging a Surrogate-Key

posts/1
Varnish Configuration Language
(VCL)
Imagery Service
Dynamically resized images for all devices.

640x260
640x900

412x	

412
Initially used Magickly Gem

Client

Varnish

Magickly
Gem

S3 Bucket

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

Client

Varnish

S3 Bucket

example.com/image.jpg?w=500&h=500&crop=fit
Re-write URL for backwards
compatibility with VCL
Additional Topics
•

Vary Headers

•

Logging

•

Shielding

•

Grace-period for stale cache
Questions?
Harlow Ward
@futuresanta
harlow@hoteltonight.com
$25 Promo Code: HARLOW

HTTP Acceleration with Varnish