Fastly engineer Michael May at OSCON 2015: When it comes to caching, we fall into two categories – those who make phat stacks of cache money and those who suffer from cache anxiety. We know caching aggressively improves performance; however, advanced caching strategies for event-driven content or user-specific content are often neglected for fear of complexity or lack of understanding. In this talk, we’ll cover HTTP caching, old and new strategies for caching historically ‘uncacheable’ content, and secret features of HTTP accelerators like Varnish. Whether you’re already stacking cache or just seeking a prescription for one, you’ll leave with a deeper understanding of caching and accelerating applications that you can take and apply to your Rails, Django, etc. apps.
10. The NASA Problem
Get pics from Deep Space to Earth
1 kb/sec bandwidth
Single (compressed) 2.5Mb image takes ~1 hour
Complete sensor dataset won’t arrive until 2016!
*source:
http://www.planetary.org/blogs/emily-‐lakdawalla/2015/01300800-‐talking-‐to-‐pluto-‐is-‐hard.html
New Horizons
18. ATL to SYD RTT (what we want)
mmay:~ $ ping fast.mmay.net
PING fallback.global-ssl.fastly.net (23.235.39.249): 56 data bytes
64 bytes from 23.235.39.249: icmp_seq=0 ttl=57 time=22.411 ms
64 bytes from 23.235.39.249: icmp_seq=2 ttl=57 time=30.053 ms
64 bytes from 23.235.39.249: icmp_seq=3 ttl=57 time=23.592 ms
64 bytes from 23.235.39.249: icmp_seq=4 ttl=57 time=23.180 ms
64 bytes from 23.235.39.249: icmp_seq=5 ttl=57 time=24.993 ms
--- fallback.global-ssl.fastly.net ping statistics ---
6 packets transmitted, 6 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 22.411/27.042/38.025/5.510 ms
19. We’re not going to beat physics
Credit:
US
Navy
/
CC-‐BY-‐2.0
20. We’re not going to beat physics
Credit:
US
Navy
/
CC-‐BY-‐2.0
But we will be clever engineers
58. AJAX
Extract server-side rendering of private
content into APIs
Fetch & render private data client-side
Cache API for increased performance
59. API Caching with event-driven purge
Set Cache-Control with a reasonably long TTL
60. API Caching with event-driven purge
Set Cache-Control with a reasonably long TTL
Set Surrogate-Key(s) with unique cache key(s)
61. API Caching with event-driven purge
Set Cache-Control with a reasonably long TTL
Set Surrogate-Key(s) with unique cache key(s)
Invalidate cached responses by Surrogate-Key purge
62. Example response handlers with API Caching
GET /content.json {
- Validation
- DB Lookup
- Set Cache-Control Header
- Set ‘Surrogate-Key: my_cache_key’ Header
- Render response
}
PUT /content.json {
- Validation
- Update DB
- Purge ‘my_cache_key’
(e.g. curl -XPOST https://api.fastly.com/purge/my_cache_key)
- Render response
69. Synthetic Response VCL for “like”
hello
world
sub vcl_recv {
if (req.url ~ "^/like" && req.request == "POST") {
error 702 "OK";
}
}
70. Synthetic Response VCL for “like”
hello
world
sub vcl_error {
if (obj.status == 702) {
set obj.status = 200;
set obj.http.Content-Type = “text/plain;”
synthetic {"OK"};
}
return (deliver);
}
71. Synthetic Response VCL for “like”
hello
world
sub vcl_log {
if (req.url ~ "^/like" && req.method == "POST") {
log {"syslog “like :: “} req.url {" "} resp.status;
}
}
76. Out of a sample of 100,000 requests,
there were 8,000 different User-Agents!
77. Caching when cookies present
1. Remove cookie and save in temp variable
2. Do normal cache lookup
3. Set cookie from temp var right before delivery
hello
world
hello
world
78. Caching when cookies present (VCL)
// Request received
sub vcl_recv {
// If request includes Cookie, save temporarily
// & remove for cache lookup
if (req.http.Cookie ~ "mycookie=") {
set req.http.Tmp-Set-Cookie = req.http.Cookie;
unset req.http.Cookie;
} else {
set req.hash_always_miss = true;
}
}
79. Caching when cookies present (VCL)
// Cache miss, fetch from origin
sub vcl_fetch {
// if origin returns Set-Cookie, save temporarily
// & remove so response can be cached
if (beresp.http.Set-Cookie) {
set req.http.Tmp-Set-Cookie = beresp.http.Set-Cookie;
unset beresp.http.Set-Cookie;
}
}
// One last thing
sub vcl_deliver {
// Include Set-Cookie in response to client
if (req.http.Tmp-Set-Cookie) {
set resp.http.Set-Cookie = req.http.Tmp-Set-Cookie;
}
}
83. Wrapping Up
c is our speed limit, moving data around takes time
CDNs are more than a dumb static content cache
84. Wrapping Up
c is our speed limit, moving data around takes time
CDNs can be more than a dumb static content cache
Cache “event driven” content and script logic at the
edge using VCL
85. Wrapping Up
c is our speed limit, moving data around takes time
CDNs can do more than be a static content cache
Cache “event driven” content and script logic at the
edge using VCL
Increase that CACHE HIT RATIO!