Optimize content delivery with CDNs. Covers cache strategies, edge computing, CDN selection, cache invalidation, origin shielding, and measuring CDN performance.
This reference explains how content delivery networks function at the edge, covering cache behavior, invalidation, and configuration patterns for distributed caching systems. It applies when designing or operating global applications that need low-latency access to static assets or dynamic content.
Edge caches store copies of resources locally near end users. A request first hits the nearest edge node, which checks if it has a cached version that satisfies the cache policy.
The Cache-Control header controls caching behavior:
Cache-Control: max-age=3600, public
This tells edge nodes to cache content for one hour and allow public caching. The public directive indicates the response may be cached by any cache, including shared caches.
Invalidation removes stale content from edge nodes. Most systems support invalidation via API endpoints or command-line tools.
curl -X DELETE \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
"https://api.example.com/v1/cache/purge" \
-d '{"urls": ["/images/logo.png", "/css/main.css"]}'
A purge request returns a JSON response indicating success or failure:
{
"status": "success",
"purged": [
"/images/logo.png",
"/css/main.css"
],
"failed": []
}
If a URL doesn't exist in the cache, the system reports it as failed but does not error.
When a request hits an edge node:
X-Cache: HIT headerX-Cache: MISSEdge nodes must be configured to handle both cacheable and non-cacheable content. This involves setting up rules for which requests to cache and how long.
A typical rule defines a URL pattern and associated caching behavior:
{
"pattern": "/static/*",
"ttl": 3600,
"cacheable": true,
"vary": ["Accept-Encoding", "User-Agent"]
}
The vary field tells the edge node which request headers determine cache keys. If a client sends different Accept-Encoding headers, the system treats them as separate cached entries.
Edge nodes respect TTL values in response headers:
Cache-Control: max-age=3600
If no explicit TTL is set, most systems default to a 1-hour cache period. When content changes, you must either wait for TTL expiration or explicitly invalidate the cache.
Some systems support partial invalidation by URL path:
curl -X POST \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
"https://api.example.com/v1/cache/invalidate" \
-d '{"paths": ["/blog/*", "/assets/images/"]}'
This invalidates all resources matching a wildcard pattern.
Some platforms support tag-based invalidation:
curl -X POST \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
"https://api.example.com/v1/cache/invalidate" \
-d '{"tags": ["blog-post-123", "image-gallery"]}'
Invalidating by tag purges all content associated with those tags.
Edge nodes maintain a cache in local memory or disk storage. The cache size is limited by configuration parameters.
Each edge node has a configured maximum size:
{
"cache_size_mb": 1024,
"max_object_size_mb": 50
}
Objects larger than the maximum size may not be cached at all.
A purge request may silently succeed even if some URLs were not invalidated:
{
"status": "success",
"purged": ["/images/logo.png"],
"failed": ["/css/main.css"]
}
The system logs the failure but does not return an HTTP error code.
During cache invalidation, a client might receive a cached version of content that was recently invalidated:
GET /api/users/123
HTTP/1.1 200 OK
X-Cache: MISS
Cache-Control: max-age=60
In this case, the edge node fetched from origin and cached it, but if the origin had changed between fetch and cache, the client receives a stale version.
If TTL is set too low, clients frequently hit origin servers. If TTL is too high, stale content persists longer than intended:
Cache-Control: max-age=300
This short TTL means each request may miss the cache and fetch from origin.
Edge nodes generate cache keys based on URL and request headers. The key determines which cached version to serve.
For a request to /static/css/main.css, with Accept-Encoding: gzip:
Cache-Key: /static/css/main.css?Accept-Encoding=gzip
If a different Accept-Encoding is used, the system treats it as a separate cache entry.
The Vary header tells edge nodes to store multiple versions of content based on request headers:
Vary: Accept-Encoding, User-Agent
This means the same URL might have different cached versions depending on client capabilities.
Edge nodes can be preloaded with content using batch operations or API calls. A warm-up process populates caches before traffic arrives:
curl -X POST \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
"https://api.example.com/v1/cache/warmup" \
-d '{"urls": ["/index.html", "/about.html"]}'
Edge nodes return specific status codes to indicate cache behavior:
200 OK with X-Cache: HIT indicates a cache hit200 OK with X-Cache: MISS indicates a cache miss that fetched from origin304 Not Modified indicates the client's cached version is still validHTTP/1.1 200 OK
X-Cache: HIT
Content-Type: text/html
Cache-Control: max-age=3600
HTTP/1.1 200 OK
X-Cache: MISS
Content-Type: text/html
Cache-Control: max-age=3600
Use reasonable TTL values for different content types:
Include Vary headers when serving different content based on request headers:
Vary: Accept-Encoding
This prevents returning compressed content to a client that doesn't support it.
Monitor cache hit ratios to optimize performance. Low hit ratios indicate poor cache configuration or frequent invalidations.
If requests frequently miss the cache, check:
When invalidation fails silently, inspect logs for:
Use headers like X-Cache to monitor performance:
X-Cache: HIT
X-Cache-Time: 0.005
These headers help identify caching behavior and performance issues.
If an edge node cannot reach origin, it may serve stale content if configured to do so:
Cache-Control: stale-while-revalidate=60
This directive allows serving cached content while attempting to revalidate with origin. If origin is unreachable, the system serves the stale version for up to 60 seconds.
This page was rewritten on 16 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy