Marketplace

cdn-architecture

Use when designing content delivery networks, caching strategies, or global content distribution. Covers CDN architecture, cache hierarchies, origin shielding, cache invalidation, and edge optimization.

allowed_tools: Read, Glob, Grep

$ Installieren

git clone https://github.com/melodic-software/claude-code-plugins /tmp/claude-code-plugins && cp -r /tmp/claude-code-plugins/plugins/systems-design/skills/cdn-architecture ~/.claude/skills/claude-code-plugins

// tip: Run this command in your terminal to install the skill


name: cdn-architecture description: Use when designing content delivery networks, caching strategies, or global content distribution. Covers CDN architecture, cache hierarchies, origin shielding, cache invalidation, and edge optimization. allowed-tools: Read, Glob, Grep

CDN Architecture

Comprehensive guide to Content Delivery Network architecture - caching, distribution, and edge optimization patterns.

When to Use This Skill

  • Designing CDN strategies for web applications
  • Implementing cache hierarchies
  • Optimizing origin load and performance
  • Understanding cache invalidation patterns
  • Selecting CDN providers and features
  • Configuring edge caching rules

CDN Fundamentals

How CDNs Work

Without CDN:
User (Tokyo) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Origin (New York)
              2000km, ~200ms RTT

With CDN:
User (Tokyo) โ”€โ”€โ–บ Edge (Tokyo) โ”€โ”€โ–บ Origin (New York)
              <10km, ~10ms RTT    (only on cache miss)

CDN Benefits:
โ”œโ”€โ”€ Reduced latency (content served from nearby)
โ”œโ”€โ”€ Origin offload (fewer requests hit origin)
โ”œโ”€โ”€ DDoS protection (distributed, absorbs attacks)
โ”œโ”€โ”€ Scalability (handles traffic spikes)
โ””โ”€โ”€ Reliability (multiple POPs for redundancy)

CDN Architecture

CDN Components:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     Internet                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚               โ”‚               โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”
    โ”‚  Edge   โ”‚    โ”‚  Edge   โ”‚    โ”‚  Edge   โ”‚
    โ”‚ POP 1   โ”‚    โ”‚ POP 2   โ”‚    โ”‚ POP 3   โ”‚
    โ”‚(Tokyo)  โ”‚    โ”‚(London) โ”‚    โ”‚(NY)     โ”‚
    โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜
         โ”‚               โ”‚               โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚    Origin Shield    โ”‚
              โ”‚    (Mid-tier)       โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚       Origin        โ”‚
              โ”‚    (Your servers)   โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Terminology:
- POP: Point of Presence (edge location)
- Edge: Servers closest to users
- Origin Shield: Optional intermediate cache
- Origin: Your actual servers

Cache Hit/Miss Flow

Request Flow:

1. User requests asset.js

2. Edge Cache Check
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚ Is asset.js in edge cache? โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                 โ”‚
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚                 โ”‚
   HIT: Return        MISS: Forward
   immediately        to origin shield
   (~10ms)

3. Origin Shield Check (if configured)
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚ Is asset.js in shield?     โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                 โ”‚
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚                 โ”‚
   HIT: Return        MISS: Forward
   to edge            to origin
   (~50ms)

4. Origin Fetch
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚ Fetch from origin server   โ”‚
   โ”‚ Cache in shield and edge   โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
   (~200ms)

Key Metrics:
- Cache Hit Ratio (CHR): % of requests served from cache
- Time To First Byte (TTFB): Latency to receive first byte
- Origin Requests: Number of requests reaching origin

Caching Strategies

Cache-Control Headers

Cache-Control Directives:

Browser + CDN:
Cache-Control: public, max-age=31536000
โ””โ”€โ”€ Cacheable by anyone for 1 year

CDN Only:
Cache-Control: private, no-store
โ””โ”€โ”€ Don't cache (sensitive data)

Short-term Cache:
Cache-Control: public, max-age=300, s-maxage=3600
โ””โ”€โ”€ Browser: 5 min, CDN: 1 hour

Stale-While-Revalidate:
Cache-Control: public, max-age=3600, stale-while-revalidate=86400
โ””โ”€โ”€ Serve stale for 24h while revalidating in background

CDN-Specific Headers:
CDN-Cache-Control: max-age=3600
Surrogate-Control: max-age=3600
Cloudflare-CDN-Cache-Control: max-age=3600

Caching Decision Matrix

Content Type โ†’ Caching Strategy:

Static Assets (JS, CSS, images):
โ”œโ”€โ”€ Long TTL (1 year)
โ”œโ”€โ”€ Content-based filename (hash)
โ”œโ”€โ”€ Immutable flag
โ””โ”€โ”€ Cache-Control: public, max-age=31536000, immutable

HTML Pages:
โ”œโ”€โ”€ Short TTL or no-cache
โ”œโ”€โ”€ Revalidation-based
โ”œโ”€โ”€ ETag/Last-Modified
โ””โ”€โ”€ Cache-Control: no-cache (revalidate every time)

API Responses (public data):
โ”œโ”€โ”€ Short to medium TTL
โ”œโ”€โ”€ Vary by appropriate headers
โ”œโ”€โ”€ Consider stale-while-revalidate
โ””โ”€โ”€ Cache-Control: public, max-age=60, stale-while-revalidate=300

API Responses (personalized):
โ”œโ”€โ”€ Don't cache at CDN
โ”œโ”€โ”€ May cache at browser with auth
โ””โ”€โ”€ Cache-Control: private, no-store

User-Generated Content:
โ”œโ”€โ”€ Medium TTL
โ”œโ”€โ”€ Consider purge on update
โ””โ”€โ”€ Cache-Control: public, max-age=3600

Vary Header

Vary Header Usage:

Purpose: Cache different versions based on request headers

Vary: Accept-Encoding
โ””โ”€โ”€ Cache separate gzip vs brotli vs plain versions

Vary: Accept-Language
โ””โ”€โ”€ Cache separate versions per language
โ””โ”€โ”€ WARNING: Can explode cache variations

Vary: Cookie
โ””โ”€โ”€ Usually means "don't cache at CDN"
โ””โ”€โ”€ Every unique cookie = different cache entry

Best Practices:
โœ“ Vary: Accept-Encoding (always for compressible content)
โœ“ Vary: Origin (for CORS)
โœ— Avoid Vary: Cookie (fragments cache badly)
โœ— Avoid Vary: User-Agent (thousands of variants)

Alternative to Vary:
- Normalize headers at edge
- Use query parameters instead
- Separate URLs for different variants

Origin Shielding

Shield Architecture

Without Origin Shield:

Edge POPs: 200+ locations
    โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚
    โ””โ”€โ”ดโ”€โ”ดโ”€โ”ดโ”€โ”ดโ”€โ”ดโ”€โ”ดโ”€โ”ดโ”€โ”ดโ”€โ”˜
              โ”‚
    All 200 POPs can request from origin
              โ–ผ
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚ Origin โ”‚  (200 potential requesters on cache miss)
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

With Origin Shield:

Edge POPs: 200+ locations
    โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚
    โ””โ”€โ”ดโ”€โ”ดโ”€โ”ดโ”€โ”ดโ”€โ”ดโ”€โ”ดโ”€โ”ดโ”€โ”ดโ”€โ”˜
              โ”‚
    All edge misses go to shield
              โ–ผ
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  Origin Shield  โ”‚  (1 shield per region)
    โ”‚   (Collapsed)   โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
             โ”‚
    Only shield requests from origin
             โ–ผ
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚ Origin โ”‚  (1-3 potential requesters)
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Benefits:
- Collapses cache misses
- Reduces origin load
- Better cache efficiency
- Improved origin availability

Request Collapsing

Request Collapsing (Coalescing):

Scenario: 100 users request same uncached asset simultaneously

Without Collapsing:
100 requests โ”€โ”€โ–บ Edge โ”€โ”€โ–บ 100 requests โ”€โ”€โ–บ Origin
                         (thundering herd)

With Collapsing:
100 requests โ”€โ”€โ–บ Edge โ”€โ”€โ–บ 1 request โ”€โ”€โ–บ Origin
                  โ”‚
            99 requests wait
                  โ”‚
            Response cached, all 100 served

Implementation:
- First request triggers origin fetch
- Subsequent requests for same URL wait
- All requests served from single origin response
- Critical for protecting origin on cache miss spikes

Cache Invalidation

Invalidation Strategies

Strategy 1: TTL-Based Expiration
โ””โ”€โ”€ Let content expire naturally
โ””โ”€โ”€ Simple, predictable
โ””โ”€โ”€ Delay in propagating changes

Strategy 2: Purge (Immediate Invalidation)
โ””โ”€โ”€ Remove specific URLs from cache
โ””โ”€โ”€ Fast update propagation
โ””โ”€โ”€ Can be expensive at scale

Strategy 3: Soft Purge (Stale-While-Revalidate)
โ””โ”€โ”€ Mark content as stale
โ””โ”€โ”€ Serve stale while fetching fresh
โ””โ”€โ”€ Best user experience

Strategy 4: Versioned URLs
โ””โ”€โ”€ Change URL when content changes
โ””โ”€โ”€ No purging needed
โ””โ”€โ”€ Best cache efficiency
โ””โ”€โ”€ asset.js?v=abc123 or asset.abc123.js

Strategy 5: Cache Tags (Surrogate Keys)
โ””โ”€โ”€ Tag content with identifiers
โ””โ”€โ”€ Purge by tag instead of URL
โ””โ”€โ”€ Powerful for related content
โ””โ”€โ”€ Purge all "product-123" tagged content

Versioning Patterns

URL Versioning:

Pattern 1: Query Parameter
/styles.css?v=1.2.3
/styles.css?v=a1b2c3d4 (hash)
+ Simple to implement
- Some CDNs don't cache query strings by default

Pattern 2: Filename Hash
/styles.a1b2c3d4.css
+ Best cache efficiency
+ CDNs cache by default
- Requires build process

Pattern 3: Path Versioning
/v1.2.3/styles.css
+ Clear version organization
- May have many versions to purge

Recommendation:
- Static assets: Filename hash (best cache efficiency)
- APIs: Path versioning (/v1/api/...)
- HTML: Short TTL + revalidation

Cache Tags / Surrogate Keys

Cache Tags Example:

Response from origin:
HTTP/1.1 200 OK
Surrogate-Key: product-123 category-electronics homepage
Cache-Control: public, max-age=86400

Content tagged with:
- product-123 (specific product)
- category-electronics (product category)
- homepage (appears on homepage)

Purge Scenarios:
- Product updated: Purge "product-123"
- Category reorganized: Purge "category-electronics"
- Homepage changed: Purge "homepage"

Single purge affects all URLs with that tag.

Edge Computing

Edge Functions

Edge Function Use Cases:

1. Request Manipulation
   - URL rewriting
   - Header modification
   - Authentication
   - Geolocation-based routing

2. Response Manipulation
   - HTML injection (A/B testing)
   - Content transformation
   - Personalization at edge
   - Response compression

3. Security
   - Bot detection
   - Rate limiting
   - WAF rules
   - Token validation

4. Caching Logic
   - Custom cache keys
   - Vary normalization
   - Cache bypass rules
   - Selective purging

Platforms:
- Cloudflare Workers
- AWS Lambda@Edge / CloudFront Functions
- Fastly Compute@Edge
- Akamai EdgeWorkers

A/B Testing at Edge

Edge A/B Testing:

Traditional (Origin-Based):
User โ”€โ”€โ–บ CDN โ”€โ”€โ–บ Origin โ”€โ”€โ–บ Determine variant โ”€โ”€โ–บ Response
         โ”‚                  (uncacheable due to personalization)
      Cache miss every time

Edge-Based:
User โ”€โ”€โ–บ Edge Worker โ”€โ”€โ–บ Assign variant (cookie/header)
              โ”‚
              โ”œโ”€โ”€ Variant A: Serve /page-a (cacheable)
              โ””โ”€โ”€ Variant B: Serve /page-b (cacheable)

Benefits:
- Each variant is separately cacheable
- No origin computation per request
- Consistent variant assignment
- Analytics at edge

CDN Selection

Provider Comparison

CDN Provider Considerations:

Performance:
โ”œโ”€โ”€ Global POP coverage
โ”œโ”€โ”€ Network quality (peering)
โ”œโ”€โ”€ Cache hit ratio
โ””โ”€โ”€ TTFB benchmarks

Features:
โ”œโ”€โ”€ Edge computing support
โ”œโ”€โ”€ Real-time analytics
โ”œโ”€โ”€ Custom caching rules
โ”œโ”€โ”€ Image optimization
โ”œโ”€โ”€ Video streaming
โ””โ”€โ”€ Security features (WAF, DDoS)

Pricing Models:
โ”œโ”€โ”€ Bandwidth-based
โ”œโ”€โ”€ Request-based
โ”œโ”€โ”€ Flat-rate
โ””โ”€โ”€ Commit discounts

Integration:
โ”œโ”€โ”€ API quality
โ”œโ”€โ”€ Terraform/IaC support
โ”œโ”€โ”€ CI/CD integration
โ””โ”€โ”€ Monitoring integration

Major Providers:
- Cloudflare: Great developer experience, generous free tier
- Fastly: Excellent for dynamic/personalized content
- AWS CloudFront: Best with AWS ecosystem
- Akamai: Enterprise, most POPs globally
- Azure CDN: Best with Azure ecosystem

Best Practices

CDN Best Practices:

1. Cache Efficiency
   โ–ก Use content hashing for static assets
   โ–ก Set appropriate TTLs for content type
   โ–ก Implement origin shielding
   โ–ก Monitor cache hit ratios

2. Performance
   โ–ก Enable compression (Brotli/Gzip)
   โ–ก Use HTTP/2 or HTTP/3
   โ–ก Implement preconnect hints
   โ–ก Optimize for Core Web Vitals

3. Cache Invalidation
   โ–ก Prefer versioned URLs over purging
   โ–ก Use cache tags for related content
   โ–ก Implement soft purge when possible
   โ–ก Have purge automation ready

4. Security
   โ–ก Enable HTTPS everywhere
   โ–ก Configure proper CORS
   โ–ก Implement security headers
   โ–ก Use signed URLs for sensitive content

5. Monitoring
   โ–ก Track cache hit ratio
   โ–ก Monitor origin load
   โ–ก Set up latency alerts
   โ–ก Analyze error rates

Troubleshooting

Common Issues:

1. Low Cache Hit Ratio
   - Check Vary header proliferation
   - Verify cache-control headers
   - Look for query string variations
   - Check for cookie-based variation

2. Stale Content
   - Verify cache-control max-age
   - Check purge propagation delay
   - Confirm versioning strategy
   - Review origin response headers

3. High Origin Load
   - Implement origin shield
   - Enable request collapsing
   - Extend TTLs where appropriate
   - Add caching for API responses

4. Slow Performance
   - Check POP distribution
   - Verify compression enabled
   - Review TLS configuration
   - Analyze origin response time

Debug Headers:
X-Cache: HIT/MISS
X-Cache-Hits: 123
Age: 3600
CF-Cache-Status: DYNAMIC/HIT/MISS

Related Skills

  • edge-computing - Compute at CDN edge
  • latency-optimization - End-to-end latency reduction
  • multi-region-deployment - Global infrastructure patterns
  • caching-strategies - Application-level caching