Stop 08 · DNS, CDN & Edge ServicesTopics · 1/7

How CDNs Work

Part of the AWS Roadmap

In short

A user in Guwahati shouldn't wait for every image to travel from a server in Virginia. CDNs keep copies close to users, which makes sites feel quick and keeps your main server calm.

A content delivery network keeps cached copies of your files in edge locations close to users, so pages load faster and your origin server handles less traffic. Amazon CloudFront is AWS's CDN.

The blob character hands out milk packets from a small street-corner milk stall.

Think of it like this

The milk booth on every corner

Nobody drives to the dairy plant for a packet of milk. The dairy sends stock to small booths in every area each morning. A CDN does that with your images, videos and scripts.

Watch it happen

First visit vs second visit

User in Pune
Edge (Mumbai)
Origin server

GET /hero.jpg

1/6DNS sends the user to the nearest edge location instead of your main server.

The big ideas

What you need to know

Edge locations

CDNs run servers in many cities. Each one keeps copies of popular files close to the people asking for them.

Lower latency

Data travels a shorter distance, so pages load faster, especially for images, video and big JavaScript files.

Origin protection

Most requests stop at the edge, so your origin handles far less traffic and survives sudden spikes.

Cache headers control it

Cache-Control tells the CDN how long a file may be reused. Get this wrong and users see stale content.

See it in code

Walk through the code

http
1# Response from your origin for a hashed static file2HTTP/1.1 200 OK3Content-Type: image/jpeg4Cache-Control: public, max-age=31536000, immutable5 6# Response for the HTML page itself7HTTP/1.1 200 OK8Content-Type: text/html9Cache-Control: public, max-age=60

1/2A year-long cache is safe because a new version gets a new file name.

Files with a hash in the name (app.3f9a.js) can be cached for a year. HTML changes often, so give it a short life.

Save yourself the pain

Common mistakes

Oops

Caching HTML for a day, then wondering why the new release isn't showing.

Do this instead

Cache HTML briefly, cache hashed assets for long, and invalidate the CDN when you must.

Oops

Putting a CDN in front of personalised pages without thinking.

Do this instead

Pages with user data must not be cached publicly. Mark them private or no-store, or one user may see another's page.

Your mission

Catch a CDN in the act

~15 min

Quick check

A user gets a 'cache HIT' from the CDN. What happened?

Go deeper

  1. 1Explain cache hit, cache miss and TTL in your own words.
  2. 2Check the response headers of a big website for signs of a CDN cache.
  3. 3List which parts of a web app are good to cache and which are not.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge