Topic Overview

CDN Routing

Understand how Content Delivery Networks route requests to nearest edge servers for optimal performance and reduced latency.

CDN (Content Delivery Network) routing directs user requests to the nearest edge server to minimize latency and improve performance by serving content from locations close to users.


What is a CDN?

CDN (Content Delivery Network) is a distributed network of servers that:

  • Cache content: Store copies of content at edge locations
  • Serve from edge: Deliver content from nearest server
  • Reduce latency: Minimize distance to users
  • Offload origin: Reduce load on origin server

Benefits:

  • Faster delivery: Content from nearby servers
  • Reduced bandwidth: Less traffic to origin
  • High availability: Multiple servers provide redundancy
  • Global reach: Serve users worldwide

CDN Architecture

Origin Server (Content Source)
CDN Network
    ├─ Edge Server (US East)
    ├─ Edge Server (US West)
    ├─ Edge Server (Europe)
    └─ Edge Server (Asia)
    Users (Routed to nearest edge)

CDN Routing Methods

1. DNS-Based Routing

Route using DNS:

User requests: example.com
DNS lookup: Returns IP of nearest edge server
User connects: To nearest edge server

How it works:

  1. User requests domain
  2. DNS server determines user location
  3. Returns IP of nearest edge server
  4. User connects to edge server

2. Anycast Routing

Same IP, different locations:

Edge servers: All have same IP (e.g., 203.0.113.1)
Routing: BGP routes to nearest server
User: Connects to nearest automatically

Benefits:

  • Automatic routing to nearest server
  • No DNS changes needed
  • Fast failover

3. Geographic Routing

Route based on user location:

User in US → US edge server
User in Europe → European edge server
User in Asia → Asian edge server

Examples

DNS-Based CDN Routing

class CDNDNSRouter:
    def __init__(self):
        self.edge_servers = {
            'us-east': '203.0.113.1',
            'us-west': '203.0.113.2',
            'europe': '203.0.113.3',
            'asia': '203.0.113.4'
        }
    
    def resolve(self, domain, client_ip):
        """Resolve domain to nearest edge server"""
        # Determine user location
        user_location = self.get_location(client_ip)
        
        # Select nearest edge server
        edge_server = self.select_nearest(user_location)
        
        # Return edge server IP
        return self.edge_servers[edge_server]
    
    def get_location(self, ip):
        """Get geographic location from IP"""
        # Use GeoIP database
        return geoip.lookup(ip)
    
    def select_nearest(self, location):
        """Select nearest edge server"""
        distances = {}
        for region, server in self.edge_servers.items():
            distances[region] = self.calculate_distance(location, region)
        
        return min(distances.items(), key=lambda x: x[1])[0]

CDN Cache Routing

class CDNCacheRouter:
    def __init__(self):
        self.edge_servers = []
        self.cache = {}
    
    async def route_request(self, request):
        """Route request to edge server"""
        # Check if content cached at edge
        edge_server = self.select_edge_server(request.client_ip)
        
        # Check cache
        cached_content = await edge_server.get_from_cache(request.url)
        
        if cached_content:
            # Cache hit: Serve from edge
            return cached_content
        else:
            # Cache miss: Fetch from origin
            content = await self.fetch_from_origin(request.url)
            
            # Cache at edge
            await edge_server.cache(request.url, content)
            
            return content

Common Pitfalls

  • DNS caching: Client DNS caches old IP. Fix: Use low TTL, anycast
  • Not considering network latency: Geographic distance not always best. Fix: Use network latency, not just distance
  • Cache invalidation: Stale content served. Fix: Implement cache invalidation, TTL
  • Single point of failure: One edge server fails. Fix: Multiple edge servers, automatic failover

Interview Questions

Beginner

Q: What is a CDN and how does it route requests?

A:

CDN (Content Delivery Network) is a distributed network of servers that cache and serve content from locations close to users.

How it routes requests:

  1. User requests content

    User → example.com
    
  2. DNS resolution

    DNS determines user location
    Returns IP of nearest edge server
    
  3. Route to edge server

    User → Nearest edge server
    
  4. Serve content

    Edge server checks cache
    If cached: Serve from edge (fast)
    If not: Fetch from origin, cache, serve
    

Benefits:

  • Faster delivery: Content from nearby servers
  • Reduced latency: Minimize distance
  • Offload origin: Less traffic to origin server

Routing methods:

  • DNS-based: DNS returns nearest edge IP
  • Anycast: Same IP, routes to nearest
  • Geographic: Route based on user location

Intermediate

Q: Explain different CDN routing methods. How does DNS-based routing work?

A:

1. DNS-Based Routing:

How it works:

1. User requests: example.com
2. DNS lookup: Query CDN DNS server
3. DNS determines: User location (from IP)
4. DNS returns: IP of nearest edge server
5. User connects: To edge server

Example:

User in US → DNS returns US edge server IP
User in Europe → DNS returns European edge server IP

2. Anycast Routing:

Same IP, different locations:

All edge servers: 203.0.113.1
BGP routing: Routes to nearest server
User: Automatically connects to nearest

Benefits:

  • Automatic routing
  • Fast failover
  • No DNS changes

3. Geographic Routing:

Route based on location:

User location determined from IP
Route to edge server in same region

Comparison:

MethodHow it worksProsCons
DNSDNS returns nearest IPFlexible, easyDNS caching issues
AnycastSame IP, BGP routesAutomatic, fastRequires BGP
GeographicRoute by locationSimpleMay not be optimal

Senior

Q: Design a CDN routing system that handles millions of requests globally. How do you route to nearest edge server, handle cache misses, and ensure high availability?

A:

class GlobalCDNRouting {
  private edgeServers: EdgeServer[];
  private dnsRouter: DNSRouter;
  private cacheManager: CacheManager;
  private healthMonitor: HealthMonitor;
  
  constructor() {
    this.edgeServers = this.initializeEdgeServers();
    this.dnsRouter = new DNSRouter();
    this.cacheManager = new CacheManager();
    this.healthMonitor = new HealthMonitor();
  }
  
  // 1. DNS-Based Routing
  class DNSRouter {
    async resolve(domain: string, clientIP: string): Promise<string> {
      // Get user location
      const location = await this.getLocation(clientIP);
      
      // Select nearest edge server
      const edgeServer = await this.selectNearest(location);
      
      // Check health
      if (!await this.healthMonitor.isHealthy(edgeServer)) {
        // Fallback to next nearest
        edgeServer = await this.selectNextNearest(location);
      }
      
      return edgeServer.ip;
    }
    
    async selectNearest(location: Location): Promise<EdgeServer> {
      // Calculate distance to all edge servers
      const distances = await Promise.all(
        this.edgeServers.map(async (server) => ({
          server,
          distance: await this.calculateDistance(location, server.location),
          latency: await this.measureLatency(server)
        }))
      );
      
      // Select based on latency (not just distance)
      return distances.sort((a, b) => a.latency - b.latency)[0].server;
    }
  }
  
  // 2. Anycast Routing
  class AnycastRouter {
    async route(anycastIP: string, clientIP: string): Promise<EdgeServer> {
      // BGP routes to nearest server automatically
      // All edge servers have same IP
      // Routing happens at network level
      
      return await this.getServerForIP(anycastIP);
    }
  }
  
  // 3. Cache Management
  class CacheManager {
    async routeRequest(request: Request): Promise<Response> {
      const edgeServer = await this.selectEdgeServer(request.clientIP);
      
      // Check cache
      const cached = await edgeServer.getFromCache(request.url);
      if (cached && !this.isExpired(cached)) {
        // Cache hit
        return cached;
      }
      
      // Cache miss: Fetch from origin
      const content = await this.fetchFromOrigin(request.url);
      
      // Cache at edge
      await edgeServer.cache(request.url, content, {
        ttl: this.calculateTTL(content)
      });
      
      return content;
    }
  }
  
  // 4. Health Monitoring
  class HealthMonitor {
    async isHealthy(server: EdgeServer): Promise<boolean> {
      // Check server health
      const health = await this.checkHealth(server);
      return health.status === 'healthy';
    }
    
    async handleFailure(failedServer: EdgeServer): Promise<void> {
      // Remove from routing
      this.removeFromRouting(failedServer);
      
      // Route traffic to other servers
      await this.redistributeTraffic(failedServer);
    }
  }
  
  // 5. Load Balancing
  class LoadBalancer {
    async balance(edgeServers: EdgeServer[]): Promise<EdgeServer> {
      // Select server with lowest load
      const loads = await Promise.all(
        edgeServers.map(s => this.getLoad(s))
      );
      
      const minLoad = Math.min(...loads);
      const index = loads.indexOf(minLoad);
      
      return edgeServers[index];
    }
  }
}

Features:

  1. DNS routing: Route based on location and latency
  2. Anycast: Automatic routing via BGP
  3. Cache management: Cache at edge, fetch from origin on miss
  4. Health monitoring: Route around failed servers
  5. Load balancing: Distribute load across edge servers

Key Takeaways

  • CDN: Distributed network of edge servers for content delivery
  • Routing methods: DNS-based, anycast, geographic
  • DNS routing: DNS returns IP of nearest edge server
  • Anycast: Same IP, BGP routes to nearest automatically
  • Cache: Content cached at edge servers for fast delivery
  • Benefits: Reduced latency, offload origin, high availability
  • Best practices: Route by latency (not just distance), handle failures, cache intelligently

About the author

InterviewCrafted helps you master system design with patience. We believe in curiosity-led engineering, reflective writing, and designing systems that make future changes feel calm.