Internet Backend 08 ๐ CDNs and Edge Infrastructure
A Content Delivery Network is a geographically distributed system of servers that caches and serves content from locations close to the user. The core idea is simple: the speed of light is a constraint, and a request that travels from Sydney to a Virginia origin server pays a latency penalty that no amount of server optimization can remove. The CDN moves the content to an edge server near Sydney, and the user’s request terminates there. The origin server is contacted only on a cache miss, and the edge server’s cache absorbs the rest of the traffic. The CDN is not a single technology. It is a combination of DNS-based routing, anycast, edge caching, cache key design, invalidation mechanisms, and, increasingly, compute at the edge.
The infrastructure is organized around Points of Presence (PoPs). A PoP is a data center that houses the edge servers, and the edge servers are the machines that terminate the user connections and serve the cached content. The PoPs are connected to each other and to the origin through a private backbone, which is often faster and more reliable than the public Internet. The origin shield is the tier between the edge PoPs and the origin: the edge PoPs request the content from the shield, and the shield requests it from the origin. The shield reduces the origin load by collapsing the duplicate requests from many edge PoPs into a single request.
Key point: A CDN is a network of edge PoPs that cache and serve content close to the user. The routing is DNS-based or anycast-based, the caching is governed by the cache key and the TTL, and the invalidation is the mechanism that removes the stale content. The origin shield is the intermediate cache tier that reduces the origin load, and the edge compute is the mechanism that moves the application logic to the PoP.
Why the CDN is the latency and scale layer
The latency problem. The distance is the constraint. A request from a user in Tokyo to an origin in Virginia travels roughly 11,000 kilometers, and the round-trip time is bounded by the speed of light in fiber, which is about 200,000 km/s. The theoretical minimum is around 110 milliseconds, and the real-world latency is higher because of the routing, the congestion, and the TLS handshake. The edge PoP in Tokyo reduces the round-trip to a few milliseconds, and the origin request is made only on the cache miss. The latency reduction is the primary reason the CDN exists.
The scale problem. A popular piece of content can generate millions of requests in a short period. The origin server cannot handle the load, and the bandwidth cost is prohibitive. The CDN absorbs the requests at the edge, and the origin sees only the cache misses. The scale is the second reason the CDN exists: the edge network is designed to handle the traffic spikes that a single origin cannot.
The routing problem. The CDN must route the user to the nearest or the best-performing PoP. The two mechanisms are the DNS-based routing and the anycast routing. The DNS-based routing returns the IP address of a specific PoP based on the resolver’s location, and the anycast routing advertises the same IP address from many PoPs and lets the network’s routing protocol select the closest one . The anycast is the simpler mechanism, but it is less precise because the BGP routing does not always select the geographically closest PoP. The DNS-based routing is more precise, but it depends on the resolver’s location, which may not be the user’s location .
The trade-off. The CDN adds a layer of complexity and a layer of cost. The cache key design determines the hit rate, and a poorly designed cache key can produce a low hit rate and a high origin load . The invalidation is imperfect: the purge removes the content from the edge caches, but the downstream caches (the browser, the corporate proxy) may still serve the stale copy . The edge compute is a newer capability, and it has the cold-start and the runtime constraints that the origin does not have.
a. The routing: DNS-based and anycast
The DNS-based routing is the older and the more precise mechanism. The CDN’s authoritative DNS server receives the resolver’s query, and it returns the IP address of the PoP that is closest to the resolver. The precision depends on the resolver’s location, which is estimated from the resolver’s IP address. The limitation is the public resolver problem: a user who uses a public DNS resolver (8.8.8.8) may be routed to the PoP near the resolver, not the PoP near the user .
The anycast routing is the newer and the more resilient mechanism. The CDN advertises the same IP address from many PoPs, and the BGP routing selects the PoP that is the fewest hops away. The anycast is the mechanism that the DDoS mitigation uses, because the traffic is spread across the PoPs and the attack is absorbed by the network. The limitation is the precision: the BGP routing is based on the network topology, not the geography, and the selected PoP may not be the closest one .
The hybrid approach is the common practice. The CDN uses the DNS to return a unicast address when it can determine the user’s location, and it uses the anycast address when it cannot . The DNS returns the unicast address of the PoP near the user, and the anycast address is the fallback for the cases where the location is unknown.
b. The cache key, the TTL, and the invalidation
The cache key is the identifier that determines whether a request is a cache hit or a miss. The default cache key in CloudFront includes the domain name and the URL path, and it does not include the query string, the headers, or the cookies . The minimal cache key produces the highest hit rate, but it is only correct if the response does not vary by the excluded values. The custom cache key includes the query string, the headers (such as Accept-Language), and the cookies, and it produces a lower hit rate but a more correct response . The cache key design is the trade-off between the hit rate and the correctness.
The TTL is the duration that the edge caches the response. The TTL is set by the origin’s Cache-Control header, and the edge respects it. A long TTL produces a high hit rate and a low origin load, but it makes the content stale for the TTL duration. A short TTL produces a lower hit rate and a higher origin load, but it makes the content fresher. The TTL is the trade-off between the efficiency and the freshness.
The invalidation is the mechanism that removes the content before the TTL expires. The purge is the API call that removes the content from the edge caches, and the next request is a cache miss that fetches the fresh content . The purge is rate-limited and it takes time to propagate. The cache tags are the more precise invalidation mechanism: the origin adds a Cache-Tag header to the response, and the purge can target the tag instead of the URL . The cache tags are the mechanism that allows the invalidation of a group of related objects without enumerating the URLs.
Complete Example Session
// ============================================
// PART 1: THE DNS-BASED ROUTING
// ============================================
// The user requests example.com.
// The resolver queries the CDN's DNS.
// The DNS returns the IP of the PoP near the resolver.
// The user connects to the PoP.
// The PoP serves the content from the cache.
// The origin is contacted only on the cache miss.
// ============================================
// PART 2: THE ANYCAST ROUTING
// ============================================
// The user requests example.com.
// The DNS returns the anycast IP.
// The BGP routing selects the PoP with the fewest hops.
// The user connects to the PoP.
// The PoP serves the content from the cache.
// ============================================
// PART 3: THE CACHE KEY
// ============================================
// The default cache key: the domain + the path.
// The custom cache key: the domain + the path + the query string + the headers + the cookies.
// The minimal key: the highest hit rate.
// The custom key: the lower hit rate, the more correct response.
// ============================================
// PART 4: THE TTL
// ============================================
// The origin sets Cache-Control: max-age=3600.
// The edge caches the response for one hour.
// The hit rate is high.
// The content is stale for up to one hour.
// ============================================
// PART 5: THE INVALIDATION
// ============================================
// The purge removes the content from the edge caches.
// The next request is a cache miss.
// The purge is rate-limited.
// The purge does not remove the content from the browser cache.
// ============================================
// PART 6: THE CACHE TAGS
// ============================================
// The origin sets Cache-Tag: product-123, category-shoes.
// The purge targets the tag.
// All the objects with the tag are invalidated.
// The cache tags are the group invalidation mechanism.
// ============================================
// PART 7: THE ORIGIN SHIELD
// ============================================
// The edge PoPs request the content from the shield.
// The shield requests the content from the origin.
// The shield collapses the duplicate requests.
// The origin load is reduced.
// ============================================
// PART 8: THE EDGE COMPUTE
// ============================================
// The Cloudflare Workers, the Lambda@Edge, the Vercel Edge Functions.
// The authentication, the rate limiting, the A/B testing.
// The logic runs at the PoP, close to the user.
// The latency is 5-10ms instead of 50-100ms.
// ============================================
// PART 9: THE COMMON FAILURES
// ============================================
// The cache hit rate is low โ the cache key is too broad.
// The content is stale โ the TTL is too long or the purge is missing.
// The origin load is high โ the origin shield is missing.
// The edge compute is slow โ the cold start or the runtime limit.
// ============================================
// PART 10: THE CHECKLIST
// ============================================
// [ ] The routing mechanism is chosen (DNS or anycast).
// [ ] The cache key is designed for the hit rate and the correctness.
// [ ] The TTL is set for the freshness and the efficiency.
// [ ] The invalidation is configured (the purge or the cache tags).
// [ ] The origin shield is enabled for the multi-PoP deployments.
// [ ] The edge compute is used for the latency-sensitive logic.
The ten parts covered the DNS-based routing, the anycast routing, the cache key, the TTL, the invalidation, the cache tags, the origin shield, the edge compute, the common failures, and the checklist. The through-line is that the CDN is a layered system, and each layer has a trade-off.
Quick Reference
The routing mechanisms
| Mechanism | Precision | Resilience | Use case |
|---|---|---|---|
| DNS-based | High (resolver location) | Medium | The general routing |
| Anycast | Low (network topology) | High | The DDoS mitigation, the fallback |
| Hybrid | High | High | The common practice |
The cache key components
| Component | Default | Custom |
|---|---|---|
| Domain | Yes | Yes |
| Path | Yes | Yes |
| Query string | No | Optional |
| Headers | No | Optional |
| Cookies | No | Optional |
The invalidation mechanisms
| Mechanism | Scope | Limitation |
|---|---|---|
| Purge by URL | The specific URL | The rate limit, the propagation |
| Purge by tag | The tagged objects | The tag limits |
| TTL expiry | The cached objects | The freshness delay |
The edge compute platforms
| Platform | Runtime | Cold start | Use case |
|---|---|---|---|
| Cloudflare Workers | V8 isolates | <1ms | The general edge logic |
| Lambda@Edge | Node.js, Python | 50-200ms | The AWS integration |
| Vercel Edge Functions | V8 isolates | Low | The Next.js middleware |
Best Practices
โ Do This:
// Use the hybrid routing for the best precision and resilience.
// Design the cache key for the hit rate and the correctness.
// Set the TTL for the freshness and the efficiency.
// Use the cache tags for the group invalidation.
// Enable the origin shield for the multi-PoP deployments.
โ Don’t Do This:
// Use the default cache key for the personalized content.
// Ignore the downstream caches when designing the invalidation.
// Rely on the purge alone for the freshness.
// Use the edge compute for the long-running tasks.
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| The cache hit rate is low | The cache key is too broad | Narrow the cache key |
| The content is stale | The TTL is too long | Shorten the TTL or purge |
| The origin load is high | The origin shield is missing | Enable the origin shield |
| The purge does not work | The downstream cache | Use the versioned URLs |
| The edge compute times out | The runtime limit | Move the long tasks to the origin |
Summary
| Item | Value |
|---|---|
| CDN | The geographically distributed cache network |
| PoP | The data center with the edge servers |
| Routing | DNS-based, anycast, or hybrid |
| Cache key | The identifier for the cache hit/miss |
| TTL | The cache duration |
| Invalidation | The purge or the cache tags |
| Origin shield | The intermediate cache tier |
| Edge compute | The application logic at the PoP |
Key takeaways:
- The CDN is the latency and scale layer. The edge PoPs serve the content close to the user, and the origin is contacted only on the cache miss. The latency reduction and the origin offload are the primary reasons the CDN exists.
- The routing is DNS-based, anycast, or hybrid. The DNS-based routing is more precise but depends on the resolver’s location. The anycast routing is more resilient but less precise. The hybrid approach uses the unicast address when the location is known and the anycast address when it is not .
- The cache key determines the hit rate and the correctness. The minimal cache key produces the highest hit rate, and the custom cache key includes the query string, the headers, and the cookies for the personalized content . The trade-off is the hit rate against the correctness.
- The invalidation is imperfect. The purge removes the content from the edge caches, but the downstream caches (the browser, the proxy) may still serve the stale copy . The cache tags are the more precise group invalidation mechanism .
- The origin shield and the edge compute are the advanced layers. The origin shield reduces the origin load by collapsing the duplicate requests from many PoPs. The edge compute moves the application logic to the PoP, with the cold-start and the runtime constraints that the origin does not have.
Remember: The CDN is a layered system: the routing gets the user to the PoP, the cache key determines the hit, the TTL determines the freshness, the invalidation removes the stale content, and the origin shield and the edge compute are the advanced layers. The trade-offs are the latency against the cost, the hit rate against the correctness, and the freshness against the efficiency. The CDN is not a single technology; it is the combination of the layers, and the design is the choice of the trade-offs.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!