CDN Routing Explained: Architecture and Optimization

Content Delivery Networks route requests to the optimal edge location, dramatically reducing latency for users worldwide. This guide explains how CDN routing works, from DNS to cache to origin, and how to optimize your CDN configuration.

How CDN Routing Works

When a user requests content from a CDN-enabled domain, multiple routing decisions determine which server responds:

1. DNS Resolution

The first routing decision happens at DNS:

  1. User's browser resolves cdn.example.com
  2. Request reaches authoritative DNS (CDN's nameservers)
  3. CDN DNS returns IP of nearest/best edge location
  4. Browser connects to that IP

2. Anycast Routing

Most modern CDNs use Anycast—the same IP address is advertised from multiple locations:

                    ┌────────────────────┐
                    │  Same IP: 1.2.3.4  │
                    └──────────┬─────────┘
       ┌───────────────────────┼───────────────────────┐
       ▼                       ▼                       ▼
   ┌────────┐              ┌────────┐              ┌────────┐
   │ US Edge│              │ EU Edge│              │Asia Edge│
   │1.2.3.4 │              │1.2.3.4 │              │1.2.3.4 │
   └────────┘              └────────┘              └────────┘
   
Internet routing automatically directs traffic to nearest location

Anycast advantages:

Cache Hierarchy

CDNs organize caches in tiers for efficiency:

Edge POPs (Points of Presence)

Regional/Shield POPs

Origin

User Request → Edge (Hit?) → Regional (Hit?) → Origin

With origin shield:
100 Edge POPs → 5 Regional POPs → 1 Shield → Origin

Cache Key and Routing

What Determines a Cache Hit?

The cache key determines if a request matches cached content:

Cache Key Best Practices

Origin Selection

For multi-origin setups, CDNs can route to different origins based on:

Path-Based Routing

/api/*       → API servers
/static/*    → S3 bucket
/images/*    → Image optimization service
/*           → Default web servers

Geographic Routing

Failover Routing

CDN Features by Provider

Feature CloudFlare CloudFront Fastly Akamai
Edge POPs 300+ 400+ 70+ 4000+
Anycast Yes Yes Yes Yes
Edge Compute Workers Lambda@Edge Compute@Edge EdgeWorkers
Origin Shield Tiered Cache Origin Shield Shielding Tiered Distribution
Instant Purge ~1s Minutes ~150ms ~5s

Edge Computing at the CDN

Modern CDNs execute code at the edge, enabling:

Use Cases

Example: Cloudflare Worker

export default {
  async fetch(request) {
    const url = new URL(request.url);
    
    // Route based on country
    const country = request.cf.country;
    if (country === 'EU') {
      url.hostname = 'eu-origin.example.com';
    } else {
      url.hostname = 'us-origin.example.com';
    }
    
    // Add cache control for static content
    if (url.pathname.startsWith('/static/')) {
      const response = await fetch(url, request);
      const newResponse = new Response(response.body, response);
      newResponse.headers.set('Cache-Control', 'public, max-age=31536000');
      return newResponse;
    }
    
    return fetch(url, request);
  }
};

Optimizing CDN Performance

Cache Hit Ratio

Higher hit ratio = less origin load and better performance:

TTL Strategy

# Static assets (immutable with versioned URLs)
Cache-Control: public, max-age=31536000, immutable

# HTML pages
Cache-Control: public, max-age=300, stale-while-revalidate=600

# API responses
Cache-Control: private, max-age=0, no-store

# Dynamic but cacheable
Cache-Control: public, s-maxage=60, max-age=0

Prefetching and Preloading

CDN Security

DDoS Protection

WAF at Edge

Origin Protection

Key Takeaways

Need CDN Optimization Help?

We optimize CDN configurations for maximum performance. Contact us for a consultation.