Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Learn › Networking › In front of the web server

CDN

The origin was changed, so why do users still get the old file?

Why it exists — If a single origin server sends files directly to every user, users who are far away wait for the distance the data travels, and when users crowd in, that server and its network line carry the load alone. A CDN has servers in several regions share the sending of the same file, so it reduces both the delay caused by distance and the requests piling up at the origin.

Where it's used today — It is mainly used to deliver files that every user receives in the same form, such as images, videos, and CSS and JS files. For these files, you add a hash or a version to the file name so that the address changes whenever the content changes, and you set a long freshness lifetime. Screens that differ for each logged-in user, and API responses that change often, are usually not copied, and the origin answers them directly.

A CDN keeps copies of the files held by the origin server on servers close to the users (edge servers) and has the edge servers answer on its behalf. The first request finds no copy at the edge, so it goes all the way to the origin (MISS), but after that the edge answers right away (HIT). In exchange, the edge does not ask the origin again during the freshness lifetime of a copy, so if you change the file at the origin in the meantime, users still receive the old copy.

Checked against real execution · Ran all 16 combinations in real time (80 seconds) with the proxy_cache of nginx 1.27.5 in the place of the edge, and checked. Checked with nginx, not a commercial CDN

Concepts in this lab

CDN
A network that keeps copies of content on servers placed in many regions around the world and has a server close to the user respond. Static files such as images, video, and scripts are delivered faster, and fewer requests go to the origin server.
Origin server and edge server
The origin server is the server that holds the original of a file, and an edge server is a server of the CDN, placed close to users, that keeps copies of it and answers on its behalf. A request from a user arrives at the edge first. If the edge has a usable copy, it answers right away (HIT), and if not, it fetches the file from the origin, passes it on, and keeps a copy (MISS). The edge does not ask the origin again during the freshness lifetime that the origin set in the response header. That makes responses faster and reduces the load on the origin, but changes made at the origin in the meantime are not reflected at the edge.
Cache key
Where it's used today — A site whose responses differ by language puts a value that indicates the language, such as the Accept-Language header, into the cache key, so that a copy in another language is not sent out by mistake. A query string such as utm_source, used for ad tracking, is left out of the cache key so that copies of the same file do not split apart.
The cache key is the information a cache uses to look up a stored copy. An HTTP cache by default uses the request method and the whole address (host, path, and query string) as the cache key. What a CDN puts in the cache key differs by product and setting, and some CDNs do not include the query string by default (the default cache key of CloudFront is the domain name and the path). When the query string is in the cache key, even a one-character difference in the query string of the same file makes it a different copy, and the request goes to the origin server again. Using this the other way around, if you change the version value in the address each time the file changes, you can send out the new content without deleting the old copy. So that query strings unrelated to the content do not split the copies into small pieces, CDNs have settings that decide which values go into the cache key and which are left out.
Cache invalidation (purge)
Where it's used today — It is used when you must take down a wrongly uploaded image or a file that contains personal information in a hurry, or right after you deploy a first-screen file such as index.html whose address cannot be changed.
Cache invalidation (purge) is the act of forcibly deleting a copy from the cache while it still has freshness lifetime left. If the edge of a CDN keeps sending out an old copy after you changed the origin, requesting invalidation of that address makes the next request fetch the new content from the origin. It is not a function defined in the HTTP standards; each CDN or proxy product offers its own, and it deletes by a single address or by a group of paths. It deletes only the copy at the edge, and it cannot delete the copy that a user's browser keeps separately. So for files that change often, rather than relying on invalidation, it is also common to use a version or a hash in the address.
TTL (time to live)
Where it's used today — In Redis, you set it with EXPIRE or the EX option of SET. Besides caches, it is used for values that must disappear by themselves after a set time, such as phone verification codes, sessions, and distributed locks.
The length of time for which data is valid. If you set it when you put a value in a cache, the value is deleted automatically after that time passes. It is a basic safeguard that stops a value that no longer matches the source from staying in the cache forever.

Try these

Running with the default settings

  1. 0s: A user sends GET /app.css?v=1. The request does not go to the origin; it arrives at the edge server close to the user. The edge has no copy of this address (MISS). The edge fetches the file from the faraway origin, gives it to the user, and keeps a copy. According to the s-maxage=300 that the origin wrote in the response, this copy may be used as it is for 300 seconds.
  2. 10s: Another user requests the same address. The edge has a copy, and it was fetched 10 seconds ago, so it is within its freshness lifetime (HIT). The edge answers right away without asking the origin. The origin does not even know that this request happened. Because many users share one copy, this is called a shared cache.
  3. 30s: A developer changes the app.css at the origin to new content and deploys it. The origin does not tell the edge about this. The copy the edge has stored is still the old content.
  4. 40s: A user requests the address /app.css?v=1. The copy at the edge was fetched 40 seconds ago, so it is within the freshness lifetime (300 seconds) (HIT). The edge gives the stored copy as it is. The file at the origin was changed 10 seconds ago, but the edge does not know. The user gets the old content.
  5. 80s: A user requests the address /app.css?v=1. There is a copy, and 80 seconds have passed since it was fetched, which is within the freshness lifetime (300 seconds) (HIT). The edge answers right away. It has been 50 seconds since the file at the origin changed, but the user still gets the old content. This keeps happening until the freshness lifetime ends at 300 seconds.

Result of this run

Code

# Response header that the origin adds when it serves app.css
Cache-Control: public, s-maxage=300     # a shared cache (the edge) may use the copy as it is for 300 seconds

# Edge: a server that keeps copies and answers on behalf of the origin. Written here as an nginx configuration file (edge is a name made up for this example)
proxy_cache_path /var/cache/nginx/edge keys_zone=edge:1m;   # the folder for the copies and the name of that store
server {
    listen 80;
    location / {
        proxy_pass http://origin.shop.example;         # if there is no usable copy, fetch the file from the origin
        proxy_cache edge;                              # keep the fetched response as a copy, and answer with the copy within the freshness lifetime
        proxy_cache_key $request_uri;                  # what a copy is looked up by: the address (path + query string)
        add_header X-Cache $upstream_cache_status;     # write the decision of the edge (MISS, HIT, EXPIRED) in a response header
    }
}
# The freshness lifetime is not written here. nginx reads Cache-Control (s-maxage) from the origin and decides it

# Order of requests
#  0s  curl http://cdn.shop.example/app.css?v=1
# 10s  curl http://cdn.shop.example/app.css?v=1
# 30s  deploy the new content of app.css to the origin
# 40s  curl http://cdn.shop.example/app.css?v=1
# 80s  curl http://cdn.shop.example/app.css?v=1

Assumptions of this model

All 16 combinations (if the file is not changed, "What is done after the change" is not used) were run with the proxy_cache of nginx 1.27.5 in the place of the edge, and it was confirmed that for each of the four requests whether it was a MISS, HIT, or EXPIRED (the value of $upstream_cache_status in nginx), the content received, whether the request went as far as the origin, and the number of requests that reached the origin all matched the model. This was checked with the proxy_cache of nginx, not with a commercial CDN service. nginx ran in a Docker container with the configuration shown on the screen (proxy_cache, proxy_cache_key $request_uri, the X-Cache header), the origin was a test server built with Node.js 24, and the part of the user was played alternately by fetch of Node and curl 8.21. The clock was not run fast, and the requests were really sent at 0, 10, 40, and 80 seconds. Whether to store a copy and its freshness lifetime were decided by nginx from the Cache-Control: public, s-maxage value of the origin. Open source nginx has no feature that accepts an invalidation request (the proxy_cache_purge directive is in the commercial version), so invalidation was done by deleting the cache file of that address at the edge. The edge and the origin were on one computer, so "close" and "far" and the resulting difference in speed were not measured by running; only whether a request went as far as the origin was checked. That s-maxage sets the freshness lifetime of a shared cache and that a copy past its freshness lifetime is not used as it is are in sections 4.2 and 5.2.2.10 of RFC 9111, and that a copy is looked up by the request method and the address is in section 2. The names HIT, MISS, and EXPIRED and the X-Cache header are not standards but conventions used by many CDNs and proxies, and the spelling differs by product. Invalidation (purge) is also a product-specific feature that is not in the standards. Varnish, Fastly, and others delete the copy of one address with a request that uses the PURGE method, and CloudFront and similar services take an invalidation request through the console or the API (for example, aws cloudfront create-invalidation). When the freshness lifetime has passed, the edge in this lab fetches the whole file again (the default behavior of nginx), but real CDNs usually first ask with a conditional request only whether the file "has changed" (see the HTTP caching and ETag lab). Not covered: that there are several edges in several regions, each keeping its own copies; the copies that browsers keep separately; and a feature that fills in copies in advance.

Previous lab: Proxies and reverse proxies

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these