LESSON 06 · Edge delivery

Deliver assets near users

Use a CDN for reusable static files across distances.

SYSTEM VIEW

The architecture

Diagram view
Visitor waits—
Edge hits—
Origin requests—

Watch one image cross the world, then get cached at an edge and served near the visitor.

  1. A visitor in Sydney loads an image from the origin in Virginia, the server that holds the source copy. As a rough example, each round trip takes about 200 ms.

    What changed

    • GET /photo.jpg from Sydney
    • About 200 ms per round trip
  2. A content delivery network keeps copies of assets at edge locations, servers close to users. The visitor now asks a nearby edge, which doesn’t hold this image yet: a miss.

    What changed

    • CDN edge close to users
    • Edge lookup: miss
  3. The edge fetches the image from the origin and caches it on the way back. This first visitor still waits on the long trip.

    What changed

    • 1 more origin request
    • photo.jpg cached at the edge
  4. Another visitor nearby asks for the same image. The edge answers in about 20 ms, and keeps doing so until its cached copy expires. The origin isn’t asked.

    What changed

    • Edge hit in about 20 ms
    • Origin not asked
  5. Say a 200 KB image is requested one million times a day. With a 95% hit ratio, the origin serves 50,000 requests and 10 GB a day, rather than 200 GB without a CDN.

    What changed

    • Origin: 50,000 requests a day
    • 10 GB a day instead of 200 GB
    Hit ratio
  6. A changed file can be refreshed by invalidating the old object, or by giving the new version a new URL such as app.3f9a2c.js. This edge misses once on the new URL, then can cache it for a long time safely.

    What changed

    • app.3f9a2c.js at a new URL
    • One miss at this edge, then cached for a long time
    • Next request: edge hit
  7. A CDN shortens the path for cached assets, but it doesn’t automatically speed up a dynamic request. The page’s HTML still crosses the world to the application and database, so if it takes 800 ms, a faster image won’t fix it. Measure the slow request first.

    What changed

    • GET / to the app in Virginia
    • The edge doesn’t automatically speed it up

Space plays · ← → step · Home resets

THE IDEA

How it works

A content delivery network stores assets such as images, video, CSS, and JavaScript at distributed edge locations, which are servers close to users. On a miss, an edge fetches the asset from its origin, the server that holds the source copy, and caches it; later requests can be answered near the user until the cached copy expires.

Distance is the main benefit. As a rough example, a visitor in Sydney fetching an image from an origin in Virginia waits about 200 ms per round trip, while a nearby edge may answer in about 20 ms. A CDN also takes load off the origin. If a 200 KB image is requested one million times a day and the hit ratio is 95%, the origin serves 50,000 requests and 10 GB rather than 200 GB.

CDN delivery has transfer costs, so rarely used assets may bring little benefit. A changed file can be refreshed by invalidating the old object or by giving the new version a new URL, such as app.3f9a2c.js, which lets you cache it for a long time safely. Cloudflare, Amazon CloudFront, and Fastly all support both. Private files need extra care, such as signed URLs. Plan what the application should do if CDN delivery is temporarily unavailable.

A CDN shortens the path for cached assets. It does not automatically accelerate a dynamic request that still crosses the world to a distant application and database. If the HTML takes 800 ms because the server is far away, a faster image will not fix it.

DECISION POINT

International users report a slow page even though database queries are fast.

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • CDN caching targets static delivery; application caching targets repeated computation or data reads.
  • Measure the slow request before choosing an edge solution.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

Which resource best fits the CDN role in this chapter?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.