Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

System Design FAQ: Top Questions

14. How would you design a Content Delivery Network (CDN)?

A Content Delivery Network (CDN) is a globally distributed network of servers that cache and deliver content (images, video, HTML, scripts) to users based on geographic proximity, reducing latency and improving performance.

📋 Functional Requirements

  • Cache static assets close to the user
  • Minimize origin server load
  • Handle cache invalidation
  • Track usage and logs

📦 Non-Functional Requirements

  • Low latency delivery worldwide
  • 99.99% availability with fallback handling
  • Security (DDoS protection, HTTPS)

🏗️ Architecture Overview

  • Edge Servers: Serve cached content close to users
  • Origin Server: Main backend to fetch uncached content
  • Cache Layer: Key-based expiration and TTL policies
  • DNS Routing: Geo-based DNS returns closest edge location

🧩 Cache Control (HTTP Headers)


Cache-Control: public, max-age=86400
ETag: "file-hash"
Expires: Wed, 12 Jun 2025 10:00:00 GMT
        

🛡️ Example with Cloudflare Config (Page Rule)


{
  "url_pattern": "example.com/static/*",
  "cache_level": "cache_everything",
  "edge_cache_ttl": 3600,
  "origin_cache_control": "respect"
}
        

🗃️ File Storage Origin (e.g., AWS S3)


aws s3 cp image.jpg s3://cdn-bucket/assets/image.jpg --acl public-read
        

CDN like CloudFront fetches from this bucket using signed or public URLs.

🔄 Cache Invalidation (CloudFront CLI)


aws cloudfront create-invalidation --distribution-id ABC123   --paths "/static/logo.png"
        

📊 Logs & Monitoring

  • Edge cache hit/miss ratio
  • Bandwidth usage by region
  • DDoS and bot protection alerts

⚙️ DNS Geo Routing Example

Use a geo-aware DNS provider (e.g., NS1, AWS Route53) to resolve users to the nearest edge node.

🧠 Optimization Techniques

  • Use Brotli/Gzip compression
  • Lazy-load heavy assets (e.g., via JS)
  • Serve WebP images for supported browsers

📌 Final Insight

CDNs play a critical role in content-heavy platforms. Smart caching, edge replication, and tight integration with storage and DNS improve scalability and global delivery performance.