Date post: | 11-May-2015 |
Category: |
Technology |
Upload: | harlow-ward |
View: | 1,448 times |
Download: | 8 times |
HTTP Acceleration with Varnish
Mobile-first 10k Hotel Partners 17 Countries ~9M Downloads
HotelTonight
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
Reverse Proxy
Origin ServerClient
www.hward.com
(CNAME)
hward.herokuapp.com
(or IP Address)
Web application accelerator.
Web application accelerator.
Reverse proxy w/ caching
Cache Miss
Varnish Origin ServerClient
Cache
Cache Hit
Varnish Origin ServerClient
Cache
Initial use of VarnishStarted 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)
Australia PoPs
N. America PoPs
Europe PoPs
Origin Server
N. America
Australia
Europe
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
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
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.
max-age vs.
s-maxage
max-age
s-maxageused only by proxy for content expiration
used on both client and 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
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
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
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)
posts/1
comments/1
comments/2
http://www.fastly.com/blog/surrogate-keys-part-1
Many-to-Many Relationship Between Keys and Pages
posts/1
Purging a Surrogate-Key
posts/1
Varnish Configuration Language (VCL)
Imagery Service
640x260640x900
412x 412
Dynamically resized images for all devices.
Initially used Magickly Gem
example.com/image.jpg?resize=500x500#
Varnish S3 BucketClientMagickly
Gem
Transition to ImgIX
example.com/image.jpg?w=500&h=500&crop=fit
Varnish S3 BucketClient
Re-write URL for backwards compatibility with VCL
Additional Topics
• Vary Headers
• Logging
• Shielding
• Grace-period for stale cache