CodePark/ questions / Caching layers & CDNs
START PRACTICINGPRACTICE
Backend question · System design & architecture › Caching layers & CDNs

What is a CDN, what content should go through it, and how do you avoid serving stale assets after a deploy?

A CDN (Content Delivery Network) is a network of edge servers close to users. They cache responses from your origin, which is your own server. Users get lower latency, and the origin gets much less traffic. CDNs also absorb traffic spikes and some DDoS traffic.

Good content for a CDN:

  • static assets: JS, CSS, images, fonts, video
  • public API responses that are safe to cache, such as product listings

Do not cache personalised or private responses at the edge. You mark them with Cache-Control: private or no-store.

How to avoid stale assets after a deploy:

  • Use fingerprinted file names, such as app.3f9a1c.js, with a long max-age and immutable. Each deploy creates new names, so you never need to purge old files.
  • Give the HTML that links to those files a short cache time, so users get the new names quickly.
  • For content you cannot rename, use short TTLs (cache lifetimes) or explicit invalidation (purging).
  • Edge caches near users reduce latency and load on the origin.
  • Send static assets and public cacheable responses through the CDN.
  • Never cache private responses at the edge.
  • Use fingerprinted file names with a long max-age.
  • Use a short TTL or a purge for everything else.