Skip to content
intermediatePhase 44 · Web Architecture

CDN in Architecture

Use CDNs to cache static assets and reduce origin server load.

30m
0 problems
Topic Progress0%

How CDNs Work

A Content Delivery Network (CDN) caches content at edge locations close to users.

CDN Architecture

                    ┌─────────────┐
                    │   Origin    │
                    │   Server    │
                    └──────┬──────┘
                           │
                    ┌──────▼──────┐
                    │   CDN Edge  │
                    │   (PoP)     │
                    └──────┬──────
                           │
              ┌────────────┼────────────┐
              │            │            │
       ┌──────▼──┐  ┌──────▼──┐  ┌──────▼──┐
       │  Edge   │  │  Edge   │  │  Edge   │
       │ Tokyo   │  │ London  │  │  NY     │
       └────┬────┘  └────┬────┘  └────┬────┘
            │            │            │
       ┌────▼──┐   ┌────▼──┐   ┌────▼──┐
       │ User  │   │ User  │   │ User  │
       │ Japan │   │  UK   │   │  US   │
       └───────┘   └───────┘   └───────┘

CDN Flow

1. User requests content
   User → DNS → CDN Edge (closest)

2. Cache HIT (content at edge)
   Edge → User (fast, < 10ms)

3. Cache MISS (content not at edge)
   Edge → Origin → Edge → User
   Edge caches for next time

CDN Benefits

Benefit Description
Lower latency Content served from nearby edge
Reduced bandwidth Origin serves less traffic
Better availability Edge handles origin failures
DDoS protection Distributed architecture
Cost savings Less origin traffic

CDN Providers

Major CDNs:
- Cloudflare (free tier available)
- AWS CloudFront
- Akamai
- Fastly
- Azure CDN
- Google Cloud CDN

Edge Locations

Edge locations are data centers distributed globally where CDN caches content.

Edge Location Network

Global Edge Locations:

North America: 100+ locations
Europe: 100+ locations
Asia Pacific: 50+ locations
South America: 20+ locations
Africa: 10+ locations

Total: 300+ edge locations worldwide

How Edge Locations Work

1. DNS Resolution
   User in Tokyo → DNS → Edge Tokyo (closest)

2. Content Delivery
   Edge Tokyo serves content
   Latency: 5ms (vs 160ms from US origin)

3. Cache Management
   Edge stores hot content
   Evicts cold content
   Refreshes from origin as needed

Edge vs Origin

Aspect Edge Origin
Location Global, close to users Central, few locations
Content Cached, hot content Complete, all content
Latency Low (5-20ms) Higher (50-200ms)
Capacity Distributed Limited
Cost Higher per request Lower per request

Content Types at Edge

Static Content (cacheable):
- Images (JPG, PNG, GIF, WebP)
- CSS files
- JavaScript files
- Fonts
- Videos

Semi-Dynamic:
- API responses (with short TTL)
- Personalized content (variant caching)

Dynamic Content (not cached):
- User-specific data
- Real-time data
- Forms

Edge Computing

Modern CDNs support edge computing:

- Cloudflare Workers
- AWS Lambda@Edge
- Fastly Compute@Edge

Run code at the edge:
- A/B testing
- Authentication
- Image transformation
- Personalization

CDN Strategies

Choosing the right CDN strategy depends on content type and update frequency.

Push vs Pull CDN

Push CDN:
- You push content to CDN
- CDN caches immediately
- You control when content is cached

Pull CDN:
- CDN pulls content on first request
- Auto-caches on cache miss
- Simpler to manage

Push CDN

When to use:
- Content updates infrequently
- You want control over caching
- Small number of large files

Example:
- Software downloads
- Video files
- Static website assets

Flow:
You → Push to CDN → CDN caches → Users access

Pull CDN

When to use:
- Content updates frequently
- Large number of files
- Don't want to manage CDN

Example:
- E-commerce products
- News articles
- Dynamic websites

Flow:
User → Request → CDN → Cache miss → Origin → CDN caches → User

CDN Configuration

# Nginx CDN configuration
location ~* \\.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 30d;
    add_header Cache-Control "public, immutable";
    add_header CDN-Cache-Control "max-age=31536000";
}

# API caching
location /api/ {
    proxy_cache my_cache;
    proxy_cache_valid 200 5m;
    proxy_cache_use_stale error timeout;
}

Cache Invalidation

Strategies:

1. TTL (Time-To-Live)
   Cache-Control: max-age=3600
   Content expires after 1 hour

2. Versioned URLs
   /style.v2.css
   New version = new URL

3. Purge API
   CDN purge /path/to/file
   Immediate invalidation

4. Tag-based
   Purge by tag: "product-123"

CDN Best Practices

  1. Cache static assets aggressively: Long TTLs for CSS, JS, images
  2. Use versioned URLs: For cache busting
  3. Set appropriate TTLs: Based on update frequency
  4. Monitor cache hit ratio: Aim for > 90%
  5. Use CDN for API responses: Cache semi-dynamic content

Practice Problems

0/3solved
Design CDN in Architecture System

Design a scalable CDN in Architecture system. Cover high-level architecture, data model, and API design.

Solution
// Complete system design:
// - Functional + Non-functional requirements
// - Capacity estimation
// - Data model (SQL/NoSQL choice)
// - API endpoints
// - Component architecture
// - Scaling strategy
// - Monitoring & reliability
CDN in Architecture Scaling

How would you scale CDN in Architecture to handle 10x the current load? Identify bottlenecks and solutions.

Solution
// Scaling approach:
// 1. Load balancing
// 2. Database sharding/replication
// 3. Cache layer (Redis)
// 4. CDN for static assets
// 5. Async processing (queues)
// 6. Microservices decomposition
CDN in Architecture Failure Modes

Analyze potential failure modes for CDN in Architecture and design mitigation strategies.

Solution
// Failure mitigation:
// 1. Redundancy (multi-AZ)
// 2. Circuit breakers
// 3. Retry with backoff
// 4. Dead letter queues
// 5. Health checks
// 6. Graceful degradation

Quiz

1. What is the main benefit of a CDN?

Question 1 options

2. What is the difference between push and pull CDN?

Question 2 options

3. What content types should be cached at CDN edge?

Question 3 options

4. How do you invalidate cached content at a CDN?

Question 4 options

Flashcards

Question

What is a CDN?

Answer

Content Delivery Network - caches content at edge locations close to users. Reduces latency, bandwidth, and improves availability. Examples: Cloudflare, CloudFront.

Question

What is the difference between push and pull CDN?

Answer

Push: You push content to CDN (you control caching). Pull: CDN pulls on first request (auto-caches). Pull is simpler; push gives more control.

Question

What content should be cached at CDN?

Answer

Static: images, CSS, JS, fonts, videos. Semi-dynamic: API responses with short TTL. Dynamic: user-specific, real-time data should not be cached.

Question

How do you invalidate CDN cache?

Answer

TTL expiration (automatic), versioned URLs (new URL), purge API (immediate), tag-based purging (by tag). Versioned URLs are most common.

Question

What is CDN in Architecture?

Answer

CDN in Architecture is a key concept in system design.

Revision Notes

Key Takeaways

  • 1.CDNs reduce latency by serving content from edge locations
  • 2.Pull CDN is simpler; push CDN gives more control
  • 3.Cache static content aggressively, not dynamic content
  • 4.Use versioned URLs for cache busting
  • 5.Monitor cache hit ratio for effectiveness

Interview Tips

  • Always include CDN for static assets in your design
  • Discuss push vs pull based on content update frequency
  • Mention cache invalidation strategy
  • Consider CDN for API responses with appropriate TTL

Cheat Sheet

CDN in Architecture - Cheat Sheet

How CDN Works:

  1. User requests content
  2. DNS routes to closest edge
  3. Edge serves cached content (HIT)
  4. If MISS, edge fetches from origin

Push vs Pull CDN:

Aspect Push Pull
Control You push content CDN pulls on request
Complexity More Less
Use case Infrequent updates Frequent updates

Cache Invalidation:

  1. TTL (Cache-Control header)
  2. Versioned URLs (/style.v2.css)
  3. Purge API
  4. Tag-based

Best Practices:

  • Cache static assets aggressively
  • Use versioned URLs
  • Monitor cache hit ratio > 90%
  • Use CDN for API responses