Topic Overview
CDN Routing
Understand how Content Delivery Networks route requests to nearest edge servers for optimal performance and reduced latency.
CDN Routing
Why This Matters
Think of CDN routing like having multiple warehouses for a store. Instead of shipping from one central warehouse (slow, far away), you have warehouses in different cities (edge servers). When someone orders, you ship from the nearest warehouse (CDN routes to nearest edge server). This reduces shipping time (latency) and improves customer experience.
This matters because network latency is proportional to distance. If your server is in New York and a user is in Tokyo, data travels 10,000+ miles, adding 100-200ms of latency. CDNs place servers (edge locations) near users, reducing latency to 10-20ms. This dramatically improves performance, especially for static content (images, videos, CSS, JS).
In interviews, when someone asks "How would you optimize content delivery?", they're testing whether you understand CDNs. Do you know how CDN routing works? Do you understand edge servers? Most engineers don't. They just serve content from one server and wonder why it's slow for distant users.
What Engineers Usually Get Wrong
Most engineers think "CDNs are just caching." But CDNs do more—they route requests to the nearest edge server using DNS, anycast, or geographic routing. They cache content at edge locations, reducing load on origin servers. They also provide DDoS protection and SSL termination. Understanding this helps you use CDNs effectively.
Engineers also don't understand that CDN routing affects cache hit rates. If routing is poor (users routed to far edge servers), cache hit rates are low, and performance suffers. Good CDN routing ensures users hit nearby edge servers with cached content. Understanding this helps you optimize CDN performance.
How This Breaks Systems in the Real World
A service was serving static content from a single server. Users far from the server experienced high latency (200ms+). Page load times were slow. The fix? Use a CDN. Deploy edge servers in multiple regions. Route users to nearest edge servers. This reduced latency to 20ms and improved page load times significantly.
Another story: A service was using a CDN but didn't configure cache headers correctly. Content wasn't cached at edge servers, so all requests went to the origin server. The CDN provided no benefit. The fix? Configure proper cache headers (Cache-Control, Expires). Ensure content is cached at edge servers. This reduces origin load and improves performance.
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:
- User requests domain
- DNS server determines user location
- Returns IP of nearest edge server
- 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
1class CDNDNSRouter {2 private edgeServers: Map<string, string> = new Map([3 ['us-east', '203.0.113.1'],4 ['us-west', '203.0.113.2'],5 ['europe', '203.0.113.3'],6 ['asia', '203.0.113.4']7 ]);89 resolve(domain: string, clientIP: string): string {10 // Resolve domain to nearest edge server11 // Determine user location12 const userLocation clientIP
CDN Cache Routing
1class CDNCacheRouter {2 private edgeServers: EdgeServer[] = [];3 private cache: Map<string, any> = new Map();45 async routeRequest(request: Request): Promise<any> {6 // Route request to edge server7 // Check if content cached at edge8 const edgeServer = this.selectEdgeServer(request.clientIP);910 // Check cache11 const cachedContent = await edgeServer.getFromCache(request.url
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:
-
User requests content
User → example.com -
DNS resolution
DNS determines user location Returns IP of nearest edge server -
Route to edge server
User → Nearest edge server -
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:
| Method | How it works | Pros | Cons |
|---|---|---|---|
| DNS | DNS returns nearest IP | Flexible, easy | DNS caching issues |
| Anycast | Same IP, BGP routes | Automatic, fast | Requires BGP |
| Geographic | Route by location | Simple | May 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:
1class GlobalCDNRouting {2 private edgeServers: EdgeServer[];3 private dnsRouter: DNSRouter;4 private cacheManager: CacheManager;5 private healthMonitor: HealthMonitor;67 constructor() {8 this.edgeServers = this.initializeEdgeServers();9 this.dnsRouter = new DNSRouter();10 this.cacheManager = new CacheManager();11 this.healthMonitor = new HealthMonitor();
Features:
- DNS routing: Route based on location and latency
- Anycast: Automatic routing via BGP
- Cache management: Cache at edge, fetch from origin on miss
- Health monitoring: Route around failed servers
- Load balancing: Distribute load across edge servers
-
DNS Resolution Flow - CDNs use DNS to route users to edge servers, understanding DNS explains CDN routing
-
Load Balancers (L4 vs L7) - CDNs use load balancing techniques, understanding load balancers explains CDN functionality
-
Anycast, Unicast & Multicast - CDNs use anycast for routing, understanding routing types explains CDN architecture
-
Latency, Throughput & Bandwidth - CDNs reduce latency by serving content from nearby locations, understanding latency explains CDN benefits
-
HTTP/1 vs HTTP/2 vs HTTP/3 - CDNs optimize HTTP delivery, understanding HTTP versions helps configure CDN performance
-
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
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
Related Topics
DNS Resolution Flow
CDNs use DNS to route users to edge servers, understanding DNS explains CDN routing
Load Balancers (L4 vs L7)
CDNs use load balancing techniques, understanding load balancers explains CDN functionality
Anycast, Unicast & Multicast
CDNs use anycast for routing, understanding routing types explains CDN architecture
Latency, Throughput & Bandwidth
CDNs reduce latency by serving content from nearby locations, understanding latency explains CDN benefits
HTTP/1 vs HTTP/2 vs HTTP/3
CDNs optimize HTTP delivery, understanding HTTP versions helps configure CDN performance
What's next?