System Design: Designing a Content Delivery Network (CDN)
A CDN is a geographically distributed group of servers that work together to provide fast delivery of internet content. By caching assets (images, videos, JS/CSS) at the "edge" of the network, CDNs reduce the distance between the user and the data, significantly lowering latency.
1. Core Requirements
- Latency: Serving content from a server closest to the user.
- Availability: Content should be available even if the origin server is down.
- Scalability: Handling massive spikes in traffic (e.g., a viral video).
- Security: Protection against DDoS attacks at the edge.
2. High-Level Architecture
- Origin Server: The source of truth where the original content is stored (e.g., S3).
- Edge Servers (POPs): Distributed servers that cache content and serve it to users.
- Routing System: Directs the user's request to the optimal Edge Server.
3. How Request Routing Works
When you type netflix.com/movie.mp4, how does the system pick the right server?
- Anycast IP: Multiple edge servers share the same IP address. Routers automatically send the request to the topologically nearest server.
- DNS Routing: The DNS server returns a different IP address based on the user's geographic location (Geo-DNS).
4. Caching Strategies: Push vs. Pull
- Pull Model (On-Demand): The edge server only fetches content from the origin when a user requests it for the first time.
- Pros: Efficient storage usage.
- Cons: The first user experiences high latency (cache miss).
- Push Model (Proactive): The origin server "pushes" content to all edge servers immediately after upload.
- Pros: Fast for all users.
- Cons: Wastes storage for niche content that is rarely watched.
5. Cache Invalidation: The Hardest Problem
How do you update an image that is cached on 10,000 servers?
- TTL (Time To Live): Content expires automatically after a set time.
- Purging: The origin server sends a "Purge" command to all edge nodes to delete a specific file.
- Versioned URLs: Instead of updating
logo.png, you uselogo_v2.png. This is the most reliable way to ensure consistency.
6. Security at the Edge
A CDN is the first line of defense.
- DDoS Protection: Edge servers can detect and block massive floods of junk traffic before they ever reach your application servers.
- WAF (Web Application Firewall): Filtering out SQL injection or XSS attacks at the network edge.
Summary
The engineering of a CDN is about Distance. By moving data closer to the user and mastering the complexities of request routing and cache invalidation, you can build a system that delivers a global, high-performance experience to millions of users simultaneously.
