CDNs and HTTP
Design a cache key, freshness policy and miss path while calculating load and mean-latency effects.
THE IDEA
CDNs and HTTP
A CDN is a shared cache near the users, and HTTP tells it what it may do. Cache-Control: max-age=60 gives a freshness lifetime; once a copy is older, the cache revalidates with the validator (ETag / If-None-Match) and the origin can answer 304 Not Modified with no body. private keeps a response out of shared caches, no-store out of all caches, and s-maxage sets a lifetime for shared caches only.
A CDN is a shared cache: it may only store what the directives allow. private keeps a response out of shared caches, no-store out of every cache, no-cache means "store, but revalidate before use", and s-maxage sets the lifetime for shared caches only.
Predict first. Does every response become publicly cacheable because it passed through a CDN?
Chapter context
A cache serves a previously obtained representation instead of repeating work. Its correctness depends on which requests can share a value and how long that value may remain useful. Fast retrieval of the wrong representation is still an error.
TRY IT YOURSELF
Part 1. Do it hands-on
Judge freshness and revalidation by hand, then write a Cache-Control interpreter.
A CDN is a shared cache: it may only store what the directives allow. private keeps a response out of shared caches, no-store out of every cache, no-cache means "store, but revalidate before use", and s-maxage sets the lifetime for shared caches only.
- To do: A shared cache stores a response with Cache-Control: max-age=60. The copy is 45 s old. Fresh or stale? Type fresh or stale
- To do: At 75 s old it is past its lifetime. The cache sends a validator request (If-None-Match with the ETag). The origin says the content is unchanged: which status code does it return?
- To do: The cached body is 500,000 bytes. A 304 response carries no body. Body bytes transferred from the origin for this revalidation?
- To do: A response says Cache-Control: private. Does a CDN (a shared cache) get to store and serve it to other users? Type yes or no
Part 2. Do it in code
- parse_cache_control(header): a dict with: max_age (int or None), s_maxage (int or None), private (bool), no_store (bool), no_cache (bool). Directives are separated by commas, case-insensitive, with optional spaces: "public, max-age=60".
- decision(cc, age, shared): "no-store" if no_store is set, or if the cache is shared and the response is private; "revalidate" if no_cache is set; otherwise the lifetime is s_maxage for a shared cache when present, else max_age (0 if absent): "fresh" if age < lifetime, else "revalidate".
- conditional_response(etag, if_none_match, body_size): the origin's answer to a validator request: (304, 0) when if_none_match equals etag, otherwise (200, body_size).
- bytes_saved(body_size, status): body_size when the status is 304, else 0.
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
Does every response become publicly cacheable because it passed through a CDN?
Chapter reference: math, worked example & code
This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.
Math and notation
If h is hit probability, t-hit hit-path time and t-miss complete miss-path time:
The miss time here already includes lookup overhead. Means do not establish tail latency, and background refresh can add origin traffic.
Worked example
At 90% hits, 2-ms hit paths and 50-ms complete miss paths, mean latency is 0.9×2+0.1×50=6.8 ms. For 1000 requests/s, nominal origin demand is 100 requests/s. During a complete cache outage, it could become 1000 requests/s rather than remaining at the normal miss rate.
Reference code
The following example uses the Python standard library and does not call a remote service.
from math import isclose
hit=.9
assert isclose(hit*2+(1-hit)*50,6.8)
assert isclose((1-hit)*1000,100)
assert 1000/100 == 10
Python examples are provided for study and local execution; this page does not execute Python.
Common mistakes
A hit ratio is not a correctness guarantee. Mean latency does not reveal p95. Shared caching requires a safe identity and privacy policy; TTL does not replace authorization.