What to put behind a CDN

Caching and Redis Mid-level Python 3.13+ PostgreSQL 17+ Reviewed By Best Latest Apps

Pick the fix

A product price changes and must update on 40 cached CDN pages within seconds. Best approach?

Options
Hint 1 of 1

How do you find the 40 pages?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Tag pages with the product's surrogate key and purge that tag on update

Correct. One call purges exactly the affected pages.

A surrogate key (cache tag) groups every cached page that depends on one product. Purging that tag invalidates just those 40 pages within seconds and leaves the rest of the cache warm.

Why not the others?
  • B Purge the entire CDN. Works, but empties everything and spikes the origin.
  • C Set max-age to 1 day. Prices would be stale for up to a day.
  • D Disable the CDN for product pages. Gives up the benefit for all reads.

30-second answer

A CDN caches responses at edge locations near users. Static assets are the easy win; public, identical-for-everyone API responses (catalogue pages, config) can be cached with s-maxage and short TTLs. Personalised responses shouldn't be shared-cached. For updates, either rely on short TTLs plus stale-while-revalidate, use versioned URLs for assets, or purge by URL or tag through the CDN's API when content changes.

Practise saying it · 30 s. No recording, just a timer

What goes on a CDN?

Say your answer out loud, then check which of these points you covered:

  • Static assets, versioned URLs
  • Public API responses, short TTL
  • Not personal data
  • Tag-based purge
  • Clean cache keys

Deep dive

Cache keys: include only the query parameters and headers that change the response, or the hit rate collapses.

Tag-based purge (surrogate keys) lets one product update purge every page that shows it.

Shield or origin-shield tiers reduce origin load on misses.

The CDN also absorbs traffic spikes and some DDoS.

Common mistakes

  • Including tracking parameters (utm_*) in the cache key.
  • Purging the whole CDN on every deploy.
  • Caching responses that set cookies.

Senior take & follow-ups

Split static, public dynamic and personal content, then purge strategies.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you update a cached asset instantly?

    Show hint

    Change its URL (content hash in the filename).

  2. What's a surrogate key?

    Show hint

    A tag on cached responses that lets you purge a group at once.

Suggest a fix if something here is wrong or out of date.