CodeMosa

Master LeetCode Patterns

CDN & Edge Caching

Caching Patterns

Serve static and cacheable dynamic content from edge POPs for lower latency

Core Idea

#

Use globally distributed edge caches and HTTP caching semantics to deliver content close to users. Combine with edge compute for rewrites, image optimization, and security enforcement.

When to Use

#

When you have global users, large static content, or want to offload origin and reduce egress/latency.

Recognition Cues

#
Indicators that this pattern might be the right solution
  • High TTFB for far-away users
  • Origin bandwidth/egress costs are high
  • Traffic spikes overload origin
  • Static assets or cacheable dynamic pages

Pattern Variants & Approaches

#

Overview

#
Clients fetch from edge POPs; cache hits serve immediately, misses fetch from origin which queries backing stores.

Overview Architecture

GETMiss/BypassQuery👤ClientCDN/Edge Cache🖥️Origin💾Database

When to Use This Variant

  • Global users and static assets
  • High egress costs at origin
  • Need WAF/DoS protection at edge

Use Case

Static websites, media delivery, cacheable dynamic pages with personalization controls.

Advantages

  • Lower TTFB and origin load
  • Tiered caching and shielding
  • Edge security controls

Implementation Example

# Example headers
Cache-Control: public, max-age=600
ETag: "content-hash"
Vary: Accept-Encoding, Authorization

Tradeoffs

#

Pros

  • Lower latency and higher throughput
  • Origin offload and egress savings
  • DDoS protection and WAF closer to users
  • Edge compute for dynamic personalization

Cons

  • Potential staleness and invalidation complexity
  • Debugging multi-layer caches is harder
  • Vendor lock-in for proprietary features
  • Extra layer adds failure modes

Common Pitfalls

#
  • Incorrect Cache-Control/ETag usage
  • Caching personalized or private data by accident
  • Missing Vary headers (auth, language, device)
  • Slow or missing purge/invalidation pipeline
  • Stampede on cache purge or miss

Design Considerations

#
  • Explicit caching headers, ETag/Last-Modified
  • Origin shield and tiered caching
  • Signed URLs/cookies and tokenized access
  • Image/video transforms and compression at edge
  • Purge APIs, soft TTL, and pre-warming

Real-World Examples

#
Cloudflare

Global anycast CDN with edge workers

300+ cities; massive throughput
Akamai

Enterprise CDN for media and software

Exabyte-scale monthly delivery
Netflix

Open Connect peering caches closer to ISPs

World's largest video CDN

Complexity Analysis

#
Scalability

Global POPs - Anycast network

Implementation Complexity

Medium - Multi-layer cache semantics

Cost

Low to Medium - Offsets origin costs