In this workshop, we’ll reveal how improving your caching strategy speeds up page loads with little effort. We’ll follow an HTTP request through Fastly and deep dive into cache hit ratios. In practical sessions, we’ll explain how to optimize your hit rate and still serve fresh content to users. We’ll cover caching headers, invalidation and the effect on user-perceived loading experience.
23. Level Capacity Latency
L1 cache 64 KiB per core ~1ns
L2 cache 256 KiB per core ~4ns
L3 cache Up to 2 MiB per
core, shared
~20ns
Main memory Up to 64 GB ~100ns
52. Here’s one I prepared
earlier:
http://fastly.us/2sybe4q
53. Back to developer tools
Right click on table heading
Response headers ▶ Add
“Cache-Control”
Response headers ▶ Manage
header columns ▶ Add custom
header → “X-Cache”
54.
55.
56.
57.
58. if users double their bandwidth
without reducing their RTT
significantly, the effect on Web
Browsing will be a minimal
improvement. However, decreasing
RTT, regardless of current bandwidth
always helps make web browsing
faster
104. if (req.url == "/dogs/max-age=60") {
set beresp.http.Cache-Control = "max-age=10";
}
105.
106.
107.
108. if (req.url == "/dogs/max-age=60") {
set beresp.ttl = 10s;
set beresp.http.Cache-Control = "max-age=10";
}
109. sub vcl_fetch {
if (beresp.http.Cache-Control) {
if (req.url.path ~ "^/users/flair/") {
set beresp.http.Cache-Control = "public, max-age=180";
} else {
set beresp.http.Cache-Control = "private";
}
}
Here’s an example of where VCL comes in very handy. Recently we
deployed .NET 4.6.2 which had a very nasty bug that set max-age on
cache responses to over 2000 years. The quickest way to mitigate this for
all of our services affected was to override that cache header as-needed
at the edge. As I write this, the following VCL is active:
111. req.http.host
The incoming request’s host header
req.url
The full URL including query string
#####GENERATION#####
A generation number used for purging all
112. What do these URLs have in
common?
● /search?query=dogs&page=1
● /search?page=1&query=dogs
125. Single URL Purge
Instant Purge an individual URL.
Surrogate-Key Purge
Instant Purge a particular service of items
tagged with a Surrogate-Key.
Purge All
Instant Purge everything from a service.
138. “Worst-case scenario, we can always ghost-serve.
Which actually happened: our Rolling Stone
legacy origin was down for three days; the only
reason readers could still visit our site without
noticing the problem was because we used
Fastly’s stale-if-error.”
— Chris Boylan, Director of Engineering,
Wenner Media
140. Soft Purge
Single URL Purge
Surrogate-Key Purge
Soft Purging sets an object’s TTL to 0s,
forcing revalidation. For best results, use in
combination with stale-while-revalidate
and/or stale-if-error
151. When the royal baby George Alexander Louis was born in 2013,
the Guardian's traffic spiked from an average of 400 requests per
second to over 1,000 requests per second. Fastly shielded the
Guardian's origin servers from the hundreds of thousands of new
requests pouring in, maintaining site uptime and delivering
consistent performance for readers around the world. The
company’s software architect, Matthew O’Brien, said that it’s
difficult to predict news events, but Fastly helps them prepare
accordingly.
155. “Fastly effectively handled the higher levels of traffic we
experienced during the Apple Watch announcement.”
Using Fastly’s stale-while-revalidate feature to serve
readers slightly outdated content while the cached
content was refreshed, WIRED knew readers would never
see errors. Zack described stale-while-revalidate as one of
Fastly’s “most exciting features.” He said, “It allowed us to
successfully serve the live blog while constantly
updating its content, ensuring our readers had access to
the freshest content without seeing errors."
— Zack Tollman, Lead Engineer at WIRED
169. HTTP/2 enables a more efficient
use of network resources and a
reduced perception of latency by
introducing header field
compression and allowing multiple
concurrent exchanges on the same
connection
170. Service Workers
This specification describes a
method that enables applications
to take advantage of persistent
background processing, including
hooks to enable bootstrapping of
web applications while offline
171. The power of
the network
Andrew Betts
Principal Developer
Advocate | Fastly