Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 65 ยท Caching

CDN and Edge Caching

Learn how a CDN and edge servers cache content close to users, pull vs push CDNs, tiered caching, cache keys, headers, purging and dynamic acceleration.

Distance is a cost you cannot optimize away with faster code. Light in fiber covers about 200 km per millisecond, so a user in Sydney talking to a server in Frankfurt waits at least 130 to 150 ms for every round trip, and a page load needs many of them. The fix is to move the data closer to the user. A content delivery network (CDN) is a globally distributed network of servers, called edge servers or points of presence (PoPs), that keep cached copies of your content near your users.

A CDN is a geographically distributed reverse proxy with a cache. Users connect to a nearby edge; if the edge has the file it answers immediately (cache hit), otherwise it fetches it from your origin server, stores it and answers (cache miss). The next nearby user gets a hit.

flowchart LR
  U1["User in Sydney"] --> E1["Edge: Sydney"]
  U2["User in London"] --> E2["Edge: London"]
  E1 -->|"miss"| S["Shield / regional cache"]
  E2 -->|"miss"| S
  S -->|"miss"| O[("Origin server<br/>Frankfurt")]

Figure: edges answer most requests; only misses travel to a shield cache and, rarely, to the origin.

Benefits

  • Lower latency: content comes from tens of milliseconds away, not hundreds.
  • Less origin load: the CDN absorbs most traffic, so the origin can be smaller.
  • Higher availability: edges serve cached content even if the origin is slow, and they absorb traffic spikes and some denial-of-service attacks.
  • Bandwidth cost savings at the origin, and often cheaper delivery overall.
  • Security and efficiency features: TLS termination close to users, HTTP/2 and HTTP/3, compression, image resizing, web application firewall.

Edge servers, CDN and "edge caching"

The terms overlap. An edge server is any server near the user. Edge caching is storing content on such servers. A CDN is the managed network of edge servers plus the software that routes users to them and manages the cache. You can also build your own edge, but running servers in hundreds of cities is why most teams rent a CDN. Increasingly, edges also run small pieces of code (edge functions) for redirects, A/B tests, authentication checks or personalization.

How users reach the nearest edge

The CDN must send each user to a good edge. Two common techniques:

  • DNS-based routing: the CDN's DNS server answers with the address of a nearby, healthy edge, based on the resolver's location.
  • Anycast: every edge announces the same IP address and internet routing delivers the user's packets to the topologically closest one. It also gives automatic failover and spreads attack traffic.

Pull and push CDNs

  • Pull CDN (the common model): you do nothing beforehand. The first request for a file misses; the edge pulls it from the origin and caches it. Easy to operate; the first user in each region pays the miss, and a cache warm-up may help for launches (warming).
  • Push CDN: you upload files to the CDN (or its storage) ahead of time, and update or delete them yourself. Good for large, rarely changing files such as software downloads and video libraries, but you manage uploads and expiry.

What to cache, and how it is controlled

CDNs excel at static content: images, video segments, CSS, JavaScript, fonts, downloads. They can also cache semi-static API responses and HTML pages for a short time. Truly personalized responses should not be cached at the edge, or one user may see another's data.

You control behavior with HTTP headers (invalidation lesson):

  • Cache-Control: public, max-age=31536000, immutable for fingerprinted assets (app.3f9a1c.js).
  • Cache-Control: s-maxage=60, stale-while-revalidate=300 for pages: edges keep them one minute and refresh in the background.
  • Cache-Control: private or no-store for personal data.
  • Vary and the cache key: the key is usually host + path + selected query parameters. Careless keys cause two kinds of bugs: too narrow (every ?utm_source=... variant caches separately and the hit rate collapses) or too wide (a response that differs by language or device is served to the wrong user).

A CDN in four lines

Edge servers Hundreds of locations near users.
Pull on miss First request fetches from origin and stores.
Headers decide max-age, s-maxage and Vary control reuse.
Protection Absorbs spikes, filters attacks.

Cache static content hard. Never cache private data publicly.

1 / 2

Measuring what a CDN does for you

The simulation below sends requests from users in three regions to a CDN with an edge in each region and an origin far away. It counts hits and misses, and shows the latency users see compared with going straight to the origin. Popularity follows a skewed distribution so a few files get most of the traffic, as on a real site.

Python Edge cache hit ratio and user latency

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The far-away region gains the most, while a user already close to the origin gains little. More important for your bill is the last line: the origin sees only the misses, a small fraction of total traffic.

Cache keys: normalize before you cache

If tracking parameters are part of the cache key, the same image gets cached many times under different URLs and the hit ratio falls. The fix is to normalize: keep only the parameters that change the response.

Python Cache key normalization

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Raw keys create four copies of what are really two images, and every marketing campaign would create more. After normalization, the three identical variants share one entry.

Tiered caching and origin shield

With hundreds of edges, a popular new file can miss in every one of them at once, so the origin receives hundreds of identical requests. Tiered caching (or an origin shield) inserts a regional or central cache between the edges and the origin: edges ask the shield, and the shield alone asks the origin. Also enable request collapsing (coalescing): when many requests for the same missing object arrive together, the cache sends one request to the origin and answers all waiters from that single response. This is the CDN version of preventing a cache stampede.

Invalidating content at the edge

You will sometimes need content to change before its TTL expires:

  • Purge by URL, prefix or tag/surrogate key through the CDN's API. Tags are powerful: tag every page that shows product 42 and purge the tag when the price changes. Purges propagate in seconds to a minute.
  • Versioned URLs: change the filename. No purge needed, and no waiting; this is best for assets.
  • Short TTL with revalidation (stale-while-revalidate, ETags) for pages that change often.
  • Soft purge: mark content stale so edges serve it while fetching fresh content, avoiding a hit spike on the origin.

Dynamic content and accelerated delivery

Even uncacheable responses (checkout, an authenticated API) can benefit from a CDN. The user opens a fast connection (TCP and TLS) to the nearby edge, and the edge uses a persistent, optimized connection to the origin and may choose better network paths. This saves several round trips on long distances. Edge functions can assemble a page from cached fragments plus a small personalized piece, getting most of the speed of caching without leaking private data.

Video streaming

Video is the largest user of CDNs. Content is split into short segments (a few seconds each) in several quality levels (adaptive bitrate, as in HLS or DASH). The player requests segments over plain HTTP, switching quality as bandwidth changes, and each segment is an ordinary cacheable file. Large streaming companies go further and place their own cache appliances inside internet providers' networks, pre-filling them overnight with the content likely to be watched, which is a push model.

Costs and risks

  • Cost: CDNs charge for bandwidth and requests; a low hit ratio wastes money.
  • Stale content: forgotten long TTLs show old pages; always keep a purge path.
  • Cache poisoning and leaks: a response containing personal data stored under a shared key is a security incident. Ensure personalized responses carry private or no-store.
  • Debugging complexity: add response headers like X-Cache: HIT or MISS and an age value; monitor hit ratio, origin traffic and error rates per region.
  • Vendor dependence: prefer standard headers over proprietary features where possible.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What happens on a CDN cache miss?

    A pull CDN populates its cache on demand from the origin.
  2. 2. Why normalize cache keys by dropping tracking parameters?

    Unneeded parameters split one object into many cache entries.
  3. 3. What is the purpose of an origin shield or tiered cache?

    Edges ask the shield, and only the shield asks the origin.
  4. 4. Which asset strategy needs no purge at all?

    A changed file gets a new name, so the old cached copy is simply no longer requested.
  5. 5. Which response should NOT be cached publicly at the edge?

    Personalized content cached under a shared key could be served to other users.

Summary

  • A CDN caches content on edge servers near users, cutting latency, origin load and bandwidth cost.
  • Users reach the nearest edge through DNS routing or anycast; pull CDNs fill on demand, push CDNs are filled by you.
  • Headers (max-age, s-maxage, Vary, private) and the cache key decide what is shared; normalize keys.
  • Use tiered caching and request collapsing to protect the origin; purge by tag or use versioned URLs.
  • Dynamic and video content also benefit; never cache private data in a shared key.

Next: Cache Warming and Stampede Protection