Login
ChallengesLearn
Scoreboard
Teams
SPNZ

LearnWEB ProtocolsCDN & Caching
WEB Protocols·Lesson 5 of 8

CDN & Caching

Edge servers, cache headers (Cache-Control, ETag, Last-Modified), cache invalidation strategies, CDN origin shielding, reverse proxies, Varnish, and how Cloudflare/Fastly make the web fast.

Beginner14 min
CDNCachingPerformance
Loading lesson…
PreviousTLS & SSLNextCORS & Same-Origin Policy

© 2026 SPNZ.

Terms of ServicePrivacy PolicyCookie Policy

Content Delivery Networks (CDNs) are the backbone of modern web performance. By caching content at edge servers distributed around the world, CDNs reduce latency, offload traffic from origin servers, and protect against traffic spikes. Understanding how caching works - and the headers that control it - is essential for any web developer or security engineer.

What you'll be able to do
  • Explain how CDNs use edge servers and geographic distribution to reduce latency.
  • Interpret Cache-Control directives and their effect on browser and CDN caching.
  • Describe how ETag and Last-Modified headers enable conditional requests.
  • Identify cache poisoning risks and know how to mitigate them.
Key terms
Origin server
The authoritative server that holds the canonical version of a resource. The CDN fetches content from the origin when a cache miss occurs.
Edge server
A geographically distributed caching server that sits between the user and the origin, serving cached content from a location close to the user.
Cache hit / miss
A cache hit occurs when the requested content is found at the edge; a miss means the edge must fetch from the origin.
Cache poisoning
An attack that tricks a CDN or proxy into caching a malicious response and serving it to other users.
What is it?

CDN & caching fundamentals

A CDN is a network of proxy servers deployed across multiple data centres worldwide. When a user requests a resource, the CDN routes the request to the nearest edge server. If the edge has a fresh copy of the resource in its cache, it returns it immediately - a cache hit that can be 10-100x faster than going to the origin. If not, the edge fetches from the origin, caches the response, and forwards it - a cache miss.

The most important tool for controlling caching behaviour is the Cache-Control header. This header carries directives that tell both browsers and intermediate caches how to handle the response:

  • max-age=<seconds> - how long the response may be cached by the browser or private cache.
  • s-maxage=<seconds> - overrides max-age for shared caches (CDNs, reverse proxies).
  • no-cache - the response must be revalidated with the origin before every use.
  • no-store - the response must never be cached under any circumstances.
  • must-revalidate - stale responses must not be served without origin validation.
  • public / private - whether the response can be cached by shared caches or only by the browser.

Beyond time-based invalidation, HTTP provides two mechanisms for conditional requests. The ETag response header carries a unique identifier for a specific version of a resource (usually a hash of its content). The client can send this value back in the If-None-Match request header; if the content has not changed, the server responds with a 304 Not Modified status and no body. Similarly, Last-Modified paired with If-Modified-Since allows revalidation based on timestamps.

httpcached response
HTTP/1.1 200 OK
Cache-Control: public, max-age=3600, s-maxage=86400
ETag: "abc123def456"
Last-Modified: Mon, 17 Mar 2025 12:00:00 GMT
Content-Type: application/json

{"status": "ok"}

CDNs also support explicit cache invalidation strategies. Purge by URL removes a specific resource from all edge caches immediately. Purge by tag (or surrogate key) lets you tag related resources - for example, tagging all pages that reference a product - and invalidate them all at once. Providers like Fastly and Cloudflare use surrogate keys extensively for fine-grained cache management.

Origin shielding is a caching pattern where a designated edge server acts as the single point of contact for the origin, absorbing all cache misses from other edges. This prevents a thundering-herd problem where dozens of edges simultaneously request the same uncached resource from the origin.

How a CDN serves a request
Mini Map
Press enter or space to select a node. You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.
Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.
Try it

CDN cache explorer

Use the simulator below to explore how a CDN edge server handles requests. Enter a URL, see the cache hit/miss flow, inspect response headers, and test what happens when the origin goes down.

CDN Simulatordev
Userbrowser
Edge ServerCDN cache
Origin Serveronline
Requests: 0Hits: 0Ratio: 0.0%
Fetch a URL to see the CDN cache behaviour
How it works
  • The edge server checks its cache when a request arrives. A HIT returns immediately; a MISS fetches from origin.
  • The x-cache header tells you whether the response came from the edge (HIT) or origin (MISS).
  • When the origin is offline, the CDN may serve stale content if stale-while-revalidate is configured.
  • Purging invalidates all cached content at the edge, forcing fresh fetches from the origin on the next request.
Real-world relevance

Why caching matters for performance and security

Proper cache configuration can reduce origin server load by 60-90% and cut page load times by 2-5x for returning visitors. Major CDN providers - Cloudflare, Fastly, Akamai, and AWS CloudFront - power most of the internet's content delivery. A single cache miss that hits the origin can add 200-500ms of latency; a cache hit at the edge serves content in under 10ms.

The security implications of misconfigured caching are severe. Web cache deception (also called cache poisoning) is an attack where an attacker tricks a CDN into caching a sensitive response - such as a page containing personal data or an API response with authentication tokens - and then serves it to other users. This typically exploits discrepancies between how the CDN and the origin server interpret URL paths or query parameters.

Mitigation

Safe caching practices

To prevent cache poisoning and other caching-related vulnerabilities, follow these practices. Always use Cache-Control: no-store for responses containing sensitive data (personal information, session tokens, API keys). Never cache responses that depend on authentication state. Set Cache-Control: private for user-specific resources so they are stored only in the browser cache, not in shared CDN caches.

For dynamic content that changes infrequently, use short max-age values combined with ETag or Last-Modified for efficient revalidation. Validate the Vary header to ensure the CDN does not serve a cached response meant for one user agent or encoding to another. Consider using surrogate keys for finer-grained cache invalidation - they let you purge related resources without knowing their exact URLs.

httpcaching patterns
# Safe caching for authenticated content
Cache-Control: private, no-store, must-revalidate

# Safe caching for public but dynamic content
Cache-Control: public, max-age=60, must-revalidate
ETag: "v2-abc123"

# Aggressive caching for static assets
Cache-Control: public, max-age=31536000, immutable
Key takeaways

What to remember

  • CDNs cache content at geographically distributed edge servers, serving cache hits in under 10ms vs 200-500ms from origin.
  • Cache-Control directives (max-age, s-maxage, no-cache, no-store, public, private) control how and where responses are cached.
  • ETag / If-None-Match and Last-Modified / If-Modified-Since enable efficient conditional revalidation without transferring the full response.
  • Web cache deception attacks exploit misconfigured caching rules to serve sensitive content to unauthorised users - always use private or no-store for authenticated responses.
Further reading
  • HTTP Caching - MDN(MDN)
  • Cache-Control - RFC 9111(IETF)
  • Web Cache Deception Attack(OWASP)

Knowledge check

0/3 answered · 0 correct
  1. 1.What does the Cache-Control: no-store directive mean?

  2. 2.How does an ETag header enable efficient revalidation?

  3. 3.What is web cache deception?