Skip to main content

Content Delivery Networks (CDN)

A CDN is a globally distributed network of servers that delivers content to users from the nearest location, reducing latency and offloading origin servers.

Benefits

Loading diagram...
BenefitImpact
Lower latencyContent served from nearby edge
Reduced bandwidthOrigin serves each asset once per edge
High availabilityMultiple edges provide redundancy
DDoS protectionDistributed network absorbs attacks
ScalabilityHandle traffic spikes without scaling origin

How CDN Works

Loading diagram...

Push vs Pull CDN

Pull CDN (Lazy Loading)

Content is fetched from origin on first request.

Loading diagram...
AdvantagesDisadvantages
No upfront upload requiredFirst request is slow (cache miss)
Automatic cache managementOrigin must be available
Only popular content cachedTTL management complexity

Use cases: Dynamic content, large content libraries, unpredictable traffic

Push CDN

Content is uploaded to CDN before users request it.

Loading diagram...
AdvantagesDisadvantages
No cache missesStorage costs for all content
Origin can be offlineUpload latency on deploy
Predictable performanceManual cache management

Use cases: Static sites, video streaming, known content libraries

Comparison

AspectPull CDNPush CDN
SetupConfigure origin URLUpload content
First requestSlow (cache miss)Fast (pre-cached)
Storage costPay for cached onlyPay for all content
Origin dependencyRequired for missesNot required
Use casesDynamic, large catalogsStatic, predictable

CDN Caching Strategies

Cache Headers

HeaderPurposeExample
Cache-ControlCaching directivesmax-age=31536000, public
ExpiresExpiration dateThu, 01 Dec 2024 16:00:00 GMT
ETagContent version"33a64df5"
Last-ModifiedLast change timeWed, 21 Oct 2023 07:28:00 GMT

Cache-Control Directives

DirectiveMeaning
publicCacheable by CDN and browsers
privateOnly browser can cache (user-specific)
max-age=NCache for N seconds
s-maxage=NCDN cache time (overrides max-age)
no-cacheMust revalidate before using
no-storeNever cache
immutableContent never changes
Content TypeTTLCache-Control
Versioned assets (app.abc123.js)1 yearmax-age=31536000, immutable
Images, fonts1 yearmax-age=31536000
CSS/JS (unversioned)1 weekmax-age=604800
HTML pages0 or shortno-cache or max-age=60
API responsesVariesprivate, max-age=60
User-specific dataNeverprivate, no-store

Cache Invalidation

Strategies

Loading diagram...
MethodSpeedComplexityCost
TTL expirationSlowNoneFree
URL purgeFastLowPer-request
Wildcard purgeFastLowHigher
Versioned URLsInstantMediumNone
Cache tagsFastMediumVaries
Loading diagram...

Implementation options:

  • Hash content: app.a3b2c1.js
  • Query parameter: app.js?v=1.2.3
  • Build timestamp: app.js?t=1699876543

CDN Architecture Patterns

Multi-CDN

Loading diagram...

Benefits:

  • Avoid vendor lock-in
  • Better coverage
  • Failover capability
  • Cost optimization

Origin Shield

Loading diagram...

Benefits:

  • Reduces origin requests
  • Single point for cache warming
  • Consistent cache across edges

Edge Computing

Modern CDNs support code execution at the edge.

Loading diagram...
ProviderEdge Compute
CloudflareWorkers
AWS CloudFrontLambda@Edge
FastlyCompute@Edge
VercelEdge Functions

Use cases:

  • Authentication/authorization
  • A/B testing
  • Personalization
  • Image optimization
  • Bot detection

CDN Providers Comparison

ProviderStrengthsUse Cases
CloudFrontAWS integration, Lambda@EdgeAWS-based applications
CloudflareDDoS protection, WorkersSecurity-focused
AkamaiEnterprise, largest networkLarge enterprises
FastlyReal-time purge, VCLDynamic content
Bunny CDNCost-effectiveBudget-conscious

Performance Metrics

MetricDescriptionTarget
Cache hit ratio% requests served from cache> 90%
TTFBTime to first byte< 100ms
Origin offload% traffic not hitting origin> 95%
Bandwidth savedData served from cacheVaries

Cache Hit Ratio Optimization

Loading diagram...