CodePark/ questions / HTTP caching & CDNs
START PRACTICINGPRACTICE
Frontend question · Browser & web performance › HTTP caching & CDNs

What is a CDN and how does it improve performance?

A CDN (content delivery network) is a set of servers in many locations. They cache your content close to users.

A request goes to the nearest edge server. If it has a fresh copy, it responds directly. If not, it fetches the content from your origin server (your own server), caches the response and serves it.

How it helps performance:

  • It lowers latency, because the round trips are shorter.
  • It reduces load on your origin server.
  • It absorbs traffic spikes.
  • Connections are set up faster, because the TLS (encryption) handshake is with a nearby server.

Headers control caching at the edge, such as Cache-Control with s-maxage. When content changes, you can purge the CDN cache, or rely on hashed URLs. CDNs serve static assets. Many can also cache HTML or API responses, and run code at the edge.

  • Edge servers near users cache your content.
  • A CDN gives lower latency and less load on your origin server.
  • Cache-Control and s-maxage control caching on the CDN.
  • You invalidate CDN content by purging it or by using hashed URLs.