Building at scale · L1CDNs and HTTP caching

CDNs and HTTP caching

A CDN answers from a copy near the user. Your HTTP headers decide what it may copy, for how long, and for whom.

L1 Must-knowBuilding at scale18 min read
Fact-checked by AI against cited sources · Human review pendingLast reviewed 21 Oct 2026 · How we check content
Section 1 of 8

What broke

Early on 25 December 2015 (Pacific time), the Steam Store was under a denial-of-service attack. Traffic to the store rose by 2,000% over the average during the Steam Sale. 1

To reduce the load on its store servers, Valve deployed caching rules managed by a web caching partner. During a second wave of the attack, a second caching setting was deployed. In Valve's words, it "incorrectly cached web traffic for authenticated users". 1

So pages built for logged-in users were cached and returned to other users. Between 11:50 and 13:20 PST, store page requests for about 34,000 users may have been shown to someone else. 1

Those pages could contain a billing address, the last four digits of a Steam Guard phone number, purchase history, the last two digits of a card number, or an email address. Full card numbers and passwords were not exposed. 1

Valve shut down the store and deployed a new caching setting. The store stayed down until all caching settings were checked and cached data was purged from the edge servers. 1

Valve called it a configuration error. The cache did what its settings said, and the settings were wrong. This lesson is about writing those settings well.

Section 2 of 8

The idea in simple words

A CDN (content delivery network) keeps copies of your content in data centers that are closer to users than your own servers. This makes pages faster and takes load off your servers. 6 The data centers near users are called edge locations. Your own server, which has the real content, is the origin server.

An edge location is a shared cache: it stores responses and reuses them for more than one user. A browser cache is a private cache: it serves one user only. 2

The CDN must answer two questions for each response:

  1. May I store this, and for how long? Your Cache-Control header answers this.
  2. Which later requests may get this copy? The cache key answers this.

The key idea. A CDN is only as safe as your headers and your cache key. If two different users can produce the same cache key, they can see each other's pages.

Section 3 of 8

How it works

Step through two users and one deploy. Use Next and Back, or the ← and → keys.

A CDN edge: a miss, a hit, and a new file name after a deploy Users Edge (near) Origin GET app.a1b2.jsuser Amiss fetch200 · keep 1 yearapp.a1b2.js200 OK same URLuser Bhitnot asked after a deployGET app.c3d4.jsnew key: miss ✓ new file everywhere, no purge SeniorPath
  1. User A asks for app.a1b2.js. The CDN sends the request to the nearest edge location. The edge has no copy yet, so it is a miss.
  2. The edge fetches the file from your origin server. The answer says it may be cached for a year. The edge stores it and returns it to user A.
  3. User B asks for the same URL from the same area. The edge already has a fresh copy, so it answers at once. Your origin does not see this request.
  4. You deploy new code. Your HTML now points to app.c3d4.js, a new file name. A new URL is a new cache key, so the edge misses and fetches it once.
  5. Users get the new file as soon as they load the new HTML. You did not purge anything. Nobody asks for the old file name any more.
Step 1 of 5

Cache-Control: the rules you send

You set the rules with the Cache-Control response header. The main directives, from RFC 9111 section 5.2: 2

  • max-age=N: the response is fresh for N seconds.
  • s-maxage=N: the same, but only for shared caches like a CDN. It overrides max-age there.
  • public: a shared cache may store this, even for a request with an Authorization header.
  • private: a shared cache must not store this. A browser may.
  • no-cache: you may store it, but you must check with the origin before each reuse.
  • no-store: do not store any part of the request or the response.

The names no-cache and no-store confuse many people. MDN says it plainly: no-cache does not mean "don't cache". If you mean "don't store", use no-store. 3

MDN also says to add private to pages made for one user, especially after login. Without it, a shared cache may reuse the page for many users and leak personal data. 3 That is what happened to Steam.

CDNs add their own defaults on top. Cloudflare does not cache HTML or JSON by default. It also skips responses with private, no-store, no-cache or max-age=0, or with a Set-Cookie header. 7 Fastly does not cache private responses, and reads s-maxage before max-age. 12 Check your own CDN's docs. They differ.

ETag and 304: checking without downloading

When a cached copy becomes stale, the cache does not have to download it again. The origin can send an ETag, a short ID for this version of the content. Later, the client sends that ID back in an If-None-Match header.

If the content has not changed, the server answers 304 Not Modified with no body. The client keeps using its copy. 4 You save bandwidth, but you still pay one round trip to the origin.

The cache key, and Vary

The cache key decides which requests share one stored copy. In the standard, it is at least the request method and the URL. 2 Fastly uses the URL, including the query string, plus the Host header. 11 Cloudflare's default key also uses the full URL, plus a few headers such as Origin. 8

Notice what is not in the key: the cookie or the logged-in user. Two users who ask for /account get the same key. So a shared cache must never store a personal page.

Sometimes one URL can return different versions, chosen by the request headers. The Vary header names those request headers. Caches then store a separate copy for each value. 5 A cache may reuse a copy without checking the origin only if those request headers match. 2

Every extra part of the key splits your traffic into more copies. Cloudflare warns that custom cache keys can reduce your cache hit rate. 8 The cache hit ratio is the share of requests the edge answers from its own copy. A high ratio means a cheap, fast origin. A low ratio means the CDN mostly forwards traffic.

Changing content: purge or a new name

When content changes, you have two choices.

A cache purge. You tell the CDN to drop copies. Cloudflare can purge one URL, a prefix, a tag, a hostname or everything. It recommends purging single URLs. 9 Fastly has URL, surrogate key and purge-all options. A soft purge marks a copy stale, so it can still be served while a new one is fetched. A hard purge removes it, so the next request goes to the origin. Purge-all is always hard. 13

A new file name. Put a version or a hash in the file name, like app.c3d4.js. Never change a file at the same name. This is called cache busting. Each file can then use a long max-age plus immutable, and the HTML can use no-cache. 3 The HTML is checked on each load, and it points to the new names.

Stale content and failures

Two more directives help when a copy goes stale. With stale-while-revalidate, a cache may serve the stale copy for a short time. Meanwhile it fetches a fresh one in the background. With stale-if-error, it may serve the stale copy when the origin returns a 500, 502, 503 or 504 error. 3 Both come from a separate standard, RFC 5861, not from RFC 9111. 2 The lesson on caching and cache stampedes shows why this matters for hot keys.

At a CDN, many users may miss on the same URL at once. Fastly's request collapsing sends only one of them to the origin, and the rest share the answer. 11

Origin shield

A CDN has many edge locations. Each one that misses goes to your origin. A shield adds one more layer. Edges ask a chosen shield location, and only the shield asks the origin. 14 Cloudflare's Tiered Cache works the same way, with lower tiers and upper tiers. 10

This reduces origin requests. The cost: traffic between Fastly locations counts toward your bill. Also, a request that reaches the origin counts as two misses, so your reported hit ratio can look lower than you expect. 14

Section 4 of 8

Trade-offs

ChoiceFresh content?Origin loadUse it when
Long max-age + hashed file namesYes A new name is fetched at once.Very low.JS, CSS, images and fonts from your build.
Short max-age (seconds to minutes)Mostly. Users may see data that old.Medium.Public pages that change often, like a product list.
no-cache + ETagYes Every reuse is checked.One small request per reuse.HTML pages that point to hashed files.
Purge on changeYes , after the purge.A burst of misses after each purge.Content you cannot rename, like an article URL.
private or no-storeYes High. The CDN answers none of these requests.Anything personal: accounts, carts, tokens.

Simple rule: hash your static files and cache them for a long time. Mark every logged-in response private. Cache public HTML only for a short time.

Section 5 of 8

At work: how a senior uses this

  • Set Cache-Control on every response, on purpose. Do not leave the choice to CDN defaults.
  • For each new endpoint, ask: "Is this response the same for every user?" If not, it is private or no-store.
  • When you add CDN rules, test them with two different logged-in users. Check that user B never sees user A's data.
  • Review CDN config changes like code. The Steam leak came from a cache setting deployed in a hurry. 1 Fastly's 2021 outage came from a valid customer config change that hit a hidden bug. About 85% of its network returned errors. 15
  • Track the cache hit ratio and origin requests per second. A sudden drop often means a new query string or header split the cache key.
  • Avoid purge-all in busy hours. At Fastly, purge-all is a hard purge, so every object must be fetched from the origin again. 13
  • Know your plan B. If the CDN fails, can your origin carry the full traffic?
Section 6 of 8

With AI

What to ask the agent

"List every HTTP endpoint in this service and the Cache-Control header it sends. Mark any response that depends on the logged-in user but is not private or no-store."

"Change our build so JS and CSS files get a content hash in the name. Serve them with max-age=31536000, immutable, and serve index.html with no-cache."

"Write a test that logs in as two users, requests /account through the CDN, and fails if the second user sees the first user's email."

What to check yourself

  • No response with personal data can be stored in a shared cache. Look for Set-Cookie and Authorization paths.
  • The agent did not add Vary: Cookie or user IDs to the cache key "to be safe". That splits the cache into one copy per user.
  • Hashed file names change when the content changes, and only then.
  • The CDN config change is reviewed, and you know how to roll it back.
Section 7 of 8

Check yourself

1. A logged-in "My account" page is sent with Cache-Control: max-age=300 and nothing else. What is the risk?

Show answer

A shared cache such as a CDN may store it and show it to other users. Without private, a shared cache may store the page. MDN warns this can leak personal data to other users.

2. What does Cache-Control: no-cache mean?

Show answer

The response may be stored, but must be checked with the origin before each reuse. no-cache means store, but revalidate before reuse. To stop storing, use no-store.

3. A browser sends If-None-Match with the ETag it has, and the file has not changed. What does the server send back?

Show answer

304 Not Modified, with no body. The ETags match, so the server tells the browser to keep using its copy. No body is sent.

4. You ship a new app.js every day. What is the safest way to make every user get the new file fast?

Show answer

Put a content hash in the file name, and cache each file for a long time. A new name is a new cache key, so no old copy can be served. Old names can be cached for a long time because they never change.

Your progress is saved only in this browser. Export it to keep it safe.

Was this clear?
Report a mistake (opens GitHub in a new tab)
Section 8 of 8

Go deeper

Show 15 sources and related lessons
  1. Valve, “Update on Christmas Issues”, Steam news, 30 December 2015 (opens in a new tab)
    Valve's own account of how a caching setting showed users' store pages to other users.
  2. Fielding, Nottingham and Reschke, RFC 9111: HTTP Caching (IETF, 2022) (opens in a new tab)
    The standard. Shared and private caches (section 1), the cache key (section 2), Vary (section 4.1) and every Cache-Control directive (section 5.2).
  3. MDN Web Docs, “Cache-Control” (opens in a new tab)
    Each directive with examples, the no-cache versus no-store trap, private for logged-in pages, and the cache-busting pattern.
  4. MDN Web Docs, “ETag” (opens in a new tab)
    How ETag and If-None-Match lead to a 304 Not Modified answer with no body.
  5. MDN Web Docs, “Vary” (opens in a new tab)
    How Vary makes caches store a separate copy for each value of the listed request headers.
  6. Cloudflare Docs, “Cache” (opens in a new tab)
    A short statement of what a CDN cache is for.
  7. Cloudflare Docs, “Default cache behavior” (opens in a new tab)
    What Cloudflare caches by default, and the headers that stop it from caching.
  8. Cloudflare Docs, “Cache keys” (opens in a new tab)
    What the default cache key contains, and the warning that custom keys can lower the hit rate.
  9. Cloudflare Docs, “Purge cache” (opens in a new tab)
    All the ways to purge, from one URL to everything.
  10. Cloudflare Docs, “Tiered Cache” (opens in a new tab)
    Lower and upper tiers of data centers, and why only the upper tier asks the origin.
  11. Fastly Documentation, “Caching” (concepts) (opens in a new tab)
    Fastly's default cache key (URL and Host), Vary as a secondary key, and request collapsing.
  12. Fastly Documentation, “Cache freshness and TTLs” (opens in a new tab)
    Which header sets the TTL at Fastly, in which order, and how private responses are treated.
  13. Fastly Documentation, “Purging” (opens in a new tab)
    URL, surrogate key and purge-all, and soft purge versus hard purge.
  14. Fastly Documentation, “Shielding” (opens in a new tab)
    How a shield POP sits between edge locations and the origin, and what it costs.
  15. Fastly, “Summary of June 8 outage” (2021) (opens in a new tab)
    A hidden bug and a valid customer config change that made 85% of Fastly's network return errors.

Real incident: Fastly, 8 Jun 2021: Fastly outage: a valid customer config hit a hidden bug

Case studies: Zerodha: Scaling a large broker with simple, boring choices

Related: Caching and cache stampedes · HTTP/1.1, HTTP/2 and HTTP/3 · Scaling reads · Safe deploys and config changes

Term

See full entry

Jump to a section

  1. 1What broke
  2. 2The idea in simple words
  3. 3How it works
  4. 4Trade-offs
  5. 5At work
  6. 6With AI
  7. 7Check yourself
  8. 8Go deeper