Part 6 of 8 · 2 min read
Changing a file that is cached everywhere
Waiting for the TTL, purging, and renaming, with the trade-offs of each.
Think of a printed menu handed out to a thousand tables. If a price changes, the choices are to wait until the menus are replaced, to collect them all, or to print a new menu with a new title and tell the staff to use only that.
The book "System Design Interview" names two ways to remove a file before it expires, besides waiting.
- Invalidate the object through the API of the CDN. The vendor tells all edges to drop their copy.
- Version the object. A different name or a parameter such as
image.png?v=2makes the new file a different key.
A worked example. A file has a TTL of 1 day. At noon the owner fixes a mistake in it.
- Wait. Some edges fetched the old copy at 11:59 and keep it until 11:59 the next day. For up to a day, some users see the old file.
- Purge. The owner calls the purge API. Most CDNs take seconds to minutes to reach all edges. The next request at each edge is a miss, so the origin is hit once per edge. With 200 edges, 200 requests.
- Version. The page now points to
app.v2.jsinstead ofapp.v1.js. No edge holdsapp.v2.js, so it is fetched fresh. The old file is simply no longer asked for.
Versioning is the best habit for files that the owner controls. The build tool puts a fingerprint of the file's content into its name, for example app.3fa9c1.js. When the content changes, the name changes. Because a name now always means the same bytes, it can safely be given a TTL of a year.
The catch: the page that points to the file must itself be fresh. A home page with a long TTL would keep pointing to the old file name. So HTML has a short TTL, and the assets it names have a long one.
| Method | Speed of change | Cost | Best for |
|---|---|---|---|
| Wait for TTL | Slow | None | Low-stakes files |
| Purge | Seconds to minutes | API call, burst of misses | A mistake that must go now |
| Version in the name | Instant for new pages | Needs a build step | Scripts, styles, images |
For the curious For the curious
Some CDNs offer "stale while revalidate": the edge keeps serving the old copy while it fetches a new one in the background. Users never wait for a miss, at the price of one slightly old answer.
Common mistake
Relying on purge as the normal way to release. If a purge fails or lags, users are left with broken pages. Use versioned names for release, and keep purge for emergencies.
Check yourself:
- Why can a versioned file name safely have a TTL of one year?
- Why must the HTML page that names the file have a short TTL?