http acceleration with varnish
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).TRANSCRIPT
![Page 1: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/1.jpg)
HTTP Acceleration with Varnish
![Page 2: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/2.jpg)
Mobile-first 10k Hotel Partners 17 Countries ~9M Downloads
HotelTonight
![Page 3: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/3.jpg)
I WANNA GO FAST
![Page 4: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/4.jpg)
Overview
Reverse Proxies
HTTP Headers
Cache Invalidation
Varnish Configuration Language
![Page 5: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/6.jpg)
Request / Response Cycle
Reverse Proxy
Origin ServerClient
www.hward.com
(CNAME)
hward.herokuapp.com
(or IP Address)
![Page 7: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/7.jpg)
Web application accelerator.
![Page 8: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/8.jpg)
Web application accelerator.
Reverse proxy w/ caching
![Page 9: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/9.jpg)
Cache Miss
Varnish Origin ServerClient
Cache
![Page 10: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/10.jpg)
Cache Hit
Varnish Origin ServerClient
Cache
![Page 11: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/11.jpg)
Initial use of VarnishStarted with a small portion of our traffic.
33% Hits, 1% Misses, 66% Passes
![Page 12: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/12.jpg)
The best traffic is traffic that never hits our servers.
![Page 13: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/13.jpg)
Continue to migrate endpoints to Varnish
Removed 10 EC2 XLarge Instances!
![Page 14: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/15.jpg)
Fastly Point of Presence (PoP)
Australia PoPs
N. America PoPs
Europe PoPs
Origin Server
N. America
Australia
Europe
![Page 16: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/16.jpg)
Simulated traffic w/ Loader.io
![Page 17: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/17.jpg)
73k req sec 1ms avg. response
![Page 18: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/21.jpg)
max-age vs.
s-maxage
![Page 22: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/25.jpg)
Rails Controllers
![Page 26: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/27.jpg)
Cache Invalidation
• Time based expiration
• Resource based expiration
• Surrogate-Keys
![Page 28: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/32.jpg)
Many-to-Many Relationship Between Keys and Pages
posts/1
![Page 33: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/33.jpg)
![Page 34: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/34.jpg)
Purging a Surrogate-Key
posts/1
![Page 35: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/35.jpg)
Varnish Configuration Language (VCL)
![Page 36: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/36.jpg)
Imagery Service
640x260640x900
412x 412
Dynamically resized images for all devices.
![Page 37: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/37.jpg)
Initially used Magickly Gem
example.com/image.jpg?resize=500x500#
Varnish S3 BucketClientMagickly
Gem
![Page 38: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/38.jpg)
Transition to ImgIX
example.com/image.jpg?w=500&h=500&crop=fit
Varnish S3 BucketClient
![Page 39: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/39.jpg)
Re-write URL for backwards compatibility with VCL
![Page 40: HTTP Acceleration with Varnish](https://reader034.vdocuments.site/reader034/viewer/2022042607/554fb268b4c9057b298b51a2/html5/thumbnails/40.jpg)
Additional Topics
• Vary Headers
• Logging
• Shielding
• Grace-period for stale cache