SYS ARCHITECTLearning Platform
Settings
Theory

Content Delivery Network (CDN)

📋 Overview

A Content Delivery Network (CDN) is a geographically distributed network of proxy servers (Edge Servers) designed to deliver web content—both static and dynamic—to users with high speed and availability. By caching content closer to the user's physical location, CDNs drastically reduce latency (RTT), offload traffic from origin servers, and provide a robust layer of defense against volumetric attacks.


🏗️ Core Principles & Characteristics

  • Edge Caching: Storing copies of static assets (JS, CSS, Images, Videos) at nodes closest to the user.
  • Anycast Routing: Using the same IP address across multiple global locations; BGP routing naturally directs the user to the topologically closest node.
  • Request Collapsing: If multiple users request the same expired resource, the CDN sends only one request to the origin, buffering others to prevent a "thundering herd."
  • Origin Shielding: An additional caching layer between Edge nodes and the Origin to further reduce load.
  • Dynamic Acceleration: Optimizing the TCP/TLS handshake and network path between the Edge and Origin for non-cacheable API requests.

⚖️ Trade-offs: Pros & Cons

  • Pros:
    • Latency Reduction: Drastically improves the "Time to First Byte" (TTFB) globally.
    • Cost Efficiency: Reduces egress bandwidth costs from expensive cloud providers (AWS/GCP) via peering.
    • Scalability: Absorbs massive traffic spikes (e.g., breaking news or viral marketing).
    • Security: Provides built-in DDoS protection and Web Application Firewall (WAF) capabilities at the edge.
  • Cons:
    • Cache Invalidation: Difficult to clear stale content across thousands of global nodes instantly.
    • Cost Complexity: Usage-based pricing can lead to "surprise" bills during high-traffic events.
    • Debug-ability: Harder to debug issues when content differs between nodes or during "cache miss" loops.

🌍 Real-World Implementation

  • Static Asset Hosting: Standard for all modern web apps (images, scripts).
  • Video Streaming: Utilizing HLS/DASH chunking to deliver video smoothly (Netflix, YouTube).
  • Software Updates: Distributing large binary patches or installers globally.
  • API Acceleration: Using Cloudflare or Akamai to speed up REST/GraphQL calls via optimized routing.
  • Providers: Cloudflare, Akamai, Amazon CloudFront, Fastly (known for real-time purging).

💡 Interview "Gotchas" & Tips

  • The "Thundering Herd" (Cache Stampede): When a popular item expires, and thousands of requests hit the origin simultaneously. Solution: Request collapsing or probabilistic early revalidation.
  • Versioning vs. Purging: Instead of purging logo.png, use logo_v2.png. It’s faster, reliable, and avoids cache-consistency issues.
  • TTL Selection: Setting a TTL too high leads to stale content; too low leads to origin overload. Use "Stale-While-Revalidate" headers to balance this.
  • Geo-Blocking: CDNs can restrict content delivery based on the user's country for compliance/legal reasons.

📐 Suggested Architecture Primitives

  • Origin Server: (S3 Bucket / Application Load Balancer).
  • Edge Nodes: Globally distributed compute/cache points.
  • DNS: Configured with CNAME or Anycast IP pointing to the CDN.
  • Purge API: Automated hooks in the CI/CD pipeline to invalidate content on deployment.
  • WAF Rules: Configured at the Edge to block SQLi, XSS, and bot traffic.
Canvas