Guide

CDNs and file storage

Sending every file from one far away server is slow.

Edge servers

A CDN (content delivery network) keeps copies of your files on edge servers around the world. Each user is sent to a nearby edge. Your own server, with the real files, is the origin. On a miss, the edge fetches the file from the origin once, keeps a copy, and serves later users itself.

What to put on a CDN

Static files fit best: images, video, JavaScript and CSS, the same bytes for every user. A page built for one user, like an inbox, must stay out of a shared cache, or another user could see it.

Cache headers

The origin sends a cache header with each file. max-age sets the TTL in seconds, like 3600 for one hour. private means only the user's own browser may keep it. no-store means nobody keeps a copy. Edges and browsers both follow them.

Changing a file

If style.css has a one week TTL, a fix can take a week to reach everyone. Instead, put a version in the name, like style.v2.css. No cache has seen that URL, so everyone gets the new file at once. Versioned files never change, so they keep a long TTL; the HTML page that points to them gets a short one. You can also purge (drop) a file from the CDN, but browsers may still hold their old copy.

Storing uploads

User uploads go in object storage, like Amazon S3. It is cheap and keeps several copies of each file. Files sit in a bucket, each found by a name called a key. Don't keep uploads on an app server's disk; other servers can't see them. A signed URL is a short-lived link that lets the client upload straight to storage, skipping the app servers.

Remember

  • The edge is near the user; the origin holds the real files.
  • Only cache what is the same for everyone in a shared cache.
  • A new file name beats waiting for a TTL.
  • Uploads go to object storage, often through a signed URL.