Sub topic 11
Delivering through a CDN with immutable, cacheable URLs
This chapter assumes you know HTTP headers and status codes, and that your files already sit in object storage. It covers how to put a CDN in front of that storage and make cached copies safe to keep for a long time.
What a CDN does for media
A CDN (content delivery network) is a set of servers spread across many regions. A viewer's request goes to a nearby edge server. On a cache hit, the edge answers from its own copy. On a miss, it fetches the file from your storage, which is the origin, stores it, and then answers.
Media files are large and are requested many times, so a high hit rate cuts latency, origin load, and egress cost.
The invalidation problem
A cache can serve a stale file if you replace the file but keep the same URL. You can ask the CDN to purge the URL, but purges are slow, can cost money, and do not reach copies stored in browsers.
A better approach is to never change what a URL points to. If the content changes, the URL changes.
Content-addressed URLs
Put a hash of the file's bytes, or a unique ID assigned at upload, into the URL. The path /media/9b1c4e/w800.webp names one exact file. A new avatar gets a new hash and so a new URL. Your database stores the current URL, and old URLs keep working for pages that still reference them.
Because the content behind a URL never changes, you can tell every cache to keep it for a long time:
GET /media/9b1c4e/w800.webp HTTP/1.1
Host: cdn.example.com
HTTP/1.1 200 OK
Content-Type: image/webp
Cache-Control: public, max-age=31536000, immutable
ETag: "9b1c4e"The directives mean:
public: shared caches such as the CDN may store the response.max-age=31536000: the copy is fresh for 31,536,000 seconds, which is one year.immutable: the browser should not revalidate the file even when the user reloads the page.ETag: a validator that lets a cache check whether its copy is still current.
Set these headers when the file is written to storage, for example through the CacheControl parameter of an upload. The CDN then passes them on.
Cache keys
The cache key is what the CDN uses to decide whether two requests want the same object. By default it includes the host and path. Query strings, cookies, and headers such as Accept can be added, and each one splits the cache into more entries.
- Keep the key small. Forward only the parts that change the response.
- Do not include cookies for public media. One cookie value per user means almost no hits.
- If you serve WebP to some browsers and JPEG to others from one URL, you need
Vary: Accept, and the CDN must key on it. Separate URLs per format are simpler.
Common errors
- Long
max-ageon a mutable URL such as/avatars/user42.jpg. Users keep seeing the old picture for up to a year, and you cannot fix it. - Hashing the wrong thing, such as the upload time. The URL must change exactly when the bytes change.
- Letting the CDN cache error responses for a long time. Use short lifetimes for 4xx and 5xx responses.
- Forgetting that HTML pages that point to these URLs are not immutable. Give them short lifetimes or revalidate them, so they can point to new URLs.
Exercise
A user uploads a new profile photo. Your site serves it at /avatars/user42.jpg with Cache-Control: max-age=31536000. Users report seeing the old photo. Why does this happen, and how do you fix it without purging?
Show answer
Caches and browsers keep the old bytes for a year because the URL did not change. Serve each version from a content-addressed URL, such as /avatars/user42/9b1c4e.jpg, and update the database to the new URL. Keep the long immutable Cache-Control header on those URLs.