esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

CDN Caching: Serve Copies From Nearby Edge Servers

Learn how a CDN keeps copies close to users, what to cache, how long to keep it, and how to get fresh content out.

In short

CDN caching keeps copies of your content on nearby edge servers, so users get it faster and your origin server does less work.

12 min readFree

What You Will Learn

Imagine your website lives on one server in Virginia. A reader in Tokyo must wait while every file travels across the Pacific Ocean. That wait is called latency, meaning the time one request takes from start to finish. CDN caching is a way to shrink that wait. A CDN, or content delivery network, is a group of servers spread over many places. Those servers keep copies of your files, so most readers get them from a server nearby.

By the end of this lesson you will be able to explain how a cached copy answers a request, decide what to cache and for how long, and keep cached files fresh after you change them. A worked example uses a news site. Browser-side copies and copies kept on your own web server are separate topics, so this lesson does not cover them.

Key words

CDN
A content delivery network: many servers in many places that deliver your files to users.
Origin server
The main server where your real files and pages live. It is the source of truth.
Edge server
A CDN server placed close to users. It keeps copies and answers requests.
Cache
A store of copies, so the next request can skip the slow trip to the origin.
Cache hit
The edge already has a usable copy and answers at once.
Cache miss
The edge has no usable copy, so it must fetch one from the origin.
TTL (time to live)
How long a copy may be used before it must be checked again or replaced.
Purge
Removing a cached copy from the CDN before its TTL runs out.
Cache-Control
A header, which is a short note sent with a response, that tells caches how to store it.

How Does a CDN Work?

A CDN stands between your users and your origin. Instead of sending every request to the origin, the CDN sends each request to the edge server closest to the user. The edge decides whether it can answer from its own store. Here is the path a request takes.

  1. 1

    The request reaches the nearest edge

    The CDN routes the user to the closest edge server, not to the distant origin.

  2. 2

    The edge looks for a copy

    The edge builds a cache key, which is the label it uses to find a copy. It is usually the web address plus any settings you chose. Then it checks its store.

  3. 3

    On a hit, answer at once

    If a copy exists and its TTL has not run out, the edge sends that copy back. The origin never sees the request.

  4. 4

    On a miss, fetch and keep a copy

    If no usable copy exists, the edge asks the origin for the file, saves the reply for the time the headers allow, and then answers the user.

  5. 5

    Later requests reuse the copy

    Nearby users now get hits until the TTL ends or someone purges the copy.

Tip

The cache key matters as much as the TTL. If you add a session cookie to the key, each user gets a separate copy and almost every request becomes a miss.

A cache key that includes too much or too little causes trouble. Too broad a key can serve the wrong content to the wrong people. Too narrow a key makes the cache useless. That is why the settings deserve a careful check.

Why Does This Cut Latency and Origin Load?

A hit is served from a server close to the reader, so the trip is short. The origin also does less work. The share of requests that an edge answers from its own store is called the cache hit ratio. Treat that as one vendor's claim, not a universal rule.

Try the Idea in Code

The program below imitates one edge server. It keeps a dictionary of copies, and each copy has an expiry time. To make the output fixed, the program does not read a real clock. We pass the time in ourselves. Run it and watch which calls are hits and which are misses.

Python
# A tiny edge cache. The time is passed in, so the output never changes.
origin = {"/app.3f9a2c.js": "new code", "/index.html": "home page"}
cache = {}  # path -> (body, time the copy expires)

def fetch(path, now, ttl):
    entry = cache.get(path)
    if entry is not None and now < entry[1]:
        return "HIT", entry[0]
    body = origin[path]  # ask the origin
    cache[path] = (body, now + ttl)
    return "MISS", body

print(fetch("/app.3f9a2c.js", 0, 31536000))
print(fetch("/app.3f9a2c.js", 5, 31536000))
print(fetch("/index.html", 10, 60))
print(fetch("/index.html", 70, 60))

Output

('MISS', 'new code')
('HIT', 'new code')
('MISS', 'home page')
('MISS', 'home page')

The first call misses because the edge is empty, so it fetches from the origin. The second call hits, because the copy has not expired. The homepage copy lasts 60 seconds. At time 70 it has expired, so the fourth call goes back to the origin.

What Should You Cache, and for How Long?

Not all content should be cached the same way. Some files are the same for every user and change rarely. Others are personal or change often. A good CDN setup treats each group on its own terms.

  • Static assets

    Logos, stylesheets, scripts, fonts, and article images change rarely. They can stay in caches for a long time.

  • Public pages

    A homepage looks the same for everyone, but it changes during the day. It needs a short life, such as 60 seconds.

  • Personal data

    Account pages, login replies, and payment replies belong to one person. Keep them out of shared caches.

What Do Cache-Control Headers Do?

The origin decides how long a response may be kept by sending a Cache-Control header with it. The CDN reads that header. Each part of the header is a directive, which is a single instruction such as max-age=60. Three directives matter most here.

  • max-age

    The number of seconds a response stays fresh. Browsers and shared caches both follow it.

  • s-maxage

    A number of seconds that applies only to shared caches such as a CDN. It overrides max-age there, while browsers keep using max-age.

  • no-store

    A rule that tells every cache never to keep the response. Use it for private or sensitive data.

Tip

no-cache does not mean do not cache. It means check with the origin before each reuse. If you want nothing stored at all, use no-store.

How Long Does a Copy Stay Fresh with a TTL?

The TTL is the time a copy may be served before the edge must check or replace it. You set it through max-age or s-maxage. A long TTL means fewer trips to the origin and a higher hit ratio. The cost is that changes take longer to reach readers. A short TTL shows changes quickly, but every expiry sends the next visitor to the origin, which lowers the hit ratio and slows the average response.

Quick check

A product page changes its price every five minutes. The team wants the CDN to hold a copy for five minutes, but wants each visitor's browser to check with the server every time. Which header fits?

Worked Example: A News Site

A news site has its origin in Virginia. It serves its logo, its CSS, and article images through a CDN. The logo and CSS get a one-year cache and hashed file names. The homepage HTML is cached for 60 seconds. Here is how the setup works, step by step.

  1. 1

    Name the static files with a fingerprint

    The build tool adds a hash of each file's content to its name, such as styles.3f9a2c.css. If the content changes, the hash and the name change too.

  2. 2

    Give the static files a one-year life

    Because a new version always gets a new name, the old name can be kept for a year without risk. The origin sends the headers shown in the code below.

  3. 3

    Give the homepage a short CDN life

    The homepage keeps one name, so it cannot use the fingerprint trick. It gets a 60-second life at the CDN. Browsers are told to check each time.

  4. 4

    A traffic spike arrives from Tokyo

    The first reader near Tokyo triggers a miss, and the Tokyo edge fetches the homepage from Virginia. The next readers near Tokyo get hits for up to 60 seconds. The Virginia origin sees about one homepage request per edge each minute, not thousands.

  5. 5

    The newsroom publishes an update

    The homepage copies expire within 60 seconds. Each new stylesheet or script has a new name, so no purge is needed for those files.

Text
Logo, CSS, article images (hashed names):
Cache-Control: public, max-age=31536000, immutable

Homepage HTML (one name, short CDN life):
Cache-Control: public, max-age=0, s-maxage=60

The two header sets follow the same rules as the lesson so far. The static files live a long time because their names change with their content. The homepage keeps a short life at the CDN, and browsers always check. Each part of the design solves one problem.

Keeping Cached Content Fresh

A copy stored at the edge is only as good as the file it came from. When you change a file, the old copy stays in the cache until it expires or is removed. You have two main tools for this: purging and versioned file names. You also have to balance freshness against the hit ratio.

Purging and Invalidation

Cache invalidation means removing or expiring a cached copy before its TTL runs out, so the next request fetches a new one. A purge is the act of doing this on the CDN. It works well for a URL that keeps its name, such as a homepage. The catch is that a purge has to reach every edge, which takes time. A full purge also empties the cache, so every request goes back to the origin until the copies return.

Tip

Before you need an emergency purge, test it on a harmless file. Know how long it takes to reach edges in your CDN.

Cache Busting With Versioned File Names

Cache busting avoids the old copy altogether. Instead of erasing it, you publish the changed file under a new URL. Browsers and CDNs treat the new URL as a different file, so they fetch it. The old copy is never asked for again and ages out on its own.

  1. 1

    Change the content

    A developer edits app.js and releases a new version.

  2. 2

    Build a new file name

    The build tool hashes the new content and names the file app.7b41d0.js.

  3. 3

    Point the HTML at the new name

    The page now refers to app.7b41d0.js instead of the old name, so every reader asks for the new file.

  4. 4

    Let the old copy age out

    The old name is never requested again. Its copies expire by their own TTL, and they do no harm while they wait.

This method works because the file path is part of the cache key. A new path is a new entry. It suits CSS, JavaScript, images, and fonts. A page that must keep one name, like the homepage, still needs a purge or a short TTL.

Freshness Versus Hit Ratio

Every choice about freshness costs something. Longer TTLs raise the hit ratio and lower origin load, but readers may see old content longer. Shorter TTLs give fresh content but send more requests to the origin. Some directives soften this trade. For example, stale-while-revalidate is a Cache-Control response directive.

Tip

When a very popular copy expires, many requests can miss at the same moment and hit the origin together. This is called a cache stampede. Spreading out expiry times or letting the edge serve a slightly old copy while it refreshes helps prevent it.

Quick check

You deploy a new version of app.js, but some users still see the old behavior. The CDN holds the old file with a one-day TTL. Which pair of actions fixes the problem now and prevents it in future releases?

Common Mistakes to Avoid

  • Caching personal responses

    If a response holds one user's data and a shared CDN keeps it, another user may receive it. Mark such responses private or no-store.

  • Cookies in the cache key

    A session cookie in the key gives each user a separate copy, so almost nothing is served from the cache.

  • Long TTLs on files that keep their name

    A one-year TTL on a file that never changes its name leaves readers on the old file until the copy expires. Use versioned names instead.

  • Using no-cache for secret data

    no-cache still allows storage. For banking or medical data, use no-store.

Summary

CDN caching stores copies of your files on edge servers near your users. A request reaches the nearest edge. A hit is answered at once, and a miss is fetched from the origin and kept for its TTL. You control this with Cache-Control headers such as max-age, s-maxage, and no-store. Static files can use long lives with versioned names, while public pages need short lives. When content changes, you either purge the old copy or publish a new file name. Each choice trades freshness against the hit ratio.

Tip

To check your setup, request a file with curl -I in a terminal. Look at the Cache-Control header and the cache status header that your CDN adds. Repeat the request to see whether it becomes a hit.

Sources

How is this lesson?

Last updated on 17 November 2026

Learn with me

Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.