CDN

Servers around the world that deliver content from the location closest to each user.

What is it?

The farther data travels, the longer it takes. If your only server is in one country, users on the other side of the world always notice a delay.

A CDN (Content Delivery Network) stores copies of your content (images, videos, scripts, static files) on servers in many places worldwide, and serves each user from the closest copy.

Explain like I'm 10

It's like a chain of grocery stores instead of one giant warehouse. Nobody drives across the country; each town has a local store with the same goods, so everyone gets what they need quickly.

Examples

Conceptual CDN request flow

// Without a CDN:
// User in Tokyo → server in New York → slow round trip

// With a CDN:
// User in Tokyo → nearby CDN server in Tokyo (already has a copy) → fast

How it works

Content is copied ("cached") to many CDN servers ("edge locations") around the world. Each user is routed to the nearest edge location, which serves its cached copy without contacting the origin server, unless the content is missing or expired there.

Why does it exist?

CDNs cut load times for users far from the origin server and take load off that server, since edge locations answer most requests. They help most with content that rarely changes, like images, videos, and static files.

When to use it

Use a CDN to serve static assets (images, videos, JS/CSS bundles) to users in different regions, so they load quickly wherever the user is.

When not to use it

Don't rely on a CDN for highly personalized or constantly changing data; it doesn't cache that well. If all your users are already close to your one server, a CDN's main benefit (proximity) doesn't add much.

Common mistakes

  • Using a CDN for highly personalized, frequently changing data that doesn't cache well.

  • Forgetting to invalidate CDN caches after updating content, so users keep seeing the old version.

  • Assuming a CDN removes the need for a fast origin server. It helps most with static content, not every request.

Practice exercises

  1. Easy:

    Explain in your own words why a website might load slowly for a user in another country without a CDN.

  2. Medium:

    List which parts of a typical website (images, live account data, CSS files) are a good fit for a CDN, and which aren't.

  3. Hard:

    Describe what could go wrong if you don't invalidate a CDN cache after deploying new versions of your website's images.

Interview questions

What problem does a CDN solve?

It serves content from a location closer to each user, cutting latency and load on the origin server.

What kind of content is best suited for a CDN?

Static, rarely changing content like images, videos, stylesheets, and scripts. Not highly personalized or constantly changing data.

What is an 'edge location'?

One of many geographically spread CDN servers, each holding cached copies of content to serve nearby users quickly.