CacheU
High Level Design

Content Delivery Network (CDN)

A deep dive into Content Delivery Networks (CDN) — how they work, why they are critical for large-scale systems, CDN architecture, caching mechanisms, request routing, edge computing, cache invalidation, and real-world system design considerations.

Content Delivery Network (CDN)

Introduction

When users access a website or application, the data must travel from the server to the user's device. If the server is located far away geographically, the request takes longer to complete.

Example:

  • Server location: New York
  • User location: India

The request must travel thousands of kilometers across the internet, introducing significant latency.

This leads to problems:

ProblemExplanation
High latencyLong distance network travel
Slow page loadStatic assets take longer to download
Server overloadOrigin server receives all requests
Poor global performanceDistant users experience slow service

A Content Delivery Network (CDN) solves this problem by distributing content across many geographically distributed servers called edge servers.

Instead of every request going to the origin server, users fetch content from the closest edge location.


What is a CDN?

A Content Delivery Network (CDN) is a globally distributed network of servers that cache and deliver content from locations closer to users.

Instead of:


User → Origin Server

Requests become:


User → Nearest CDN Edge Server → Origin Server (only if needed)

Architecture overview:

Diagram
flowchart LR User1 --> Edge1 User2 --> Edge2 User3 --> Edge3 Edge1 --> Origin Edge2 --> Origin Edge3 --> Origin Origin[(Origin Server)]

Each edge server stores cached content, reducing the need to contact the origin server.


Why CDNs Are Critical for Large Systems

Large-scale platforms such as streaming services, e-commerce platforms, and social networks rely heavily on CDNs.

Key benefits:

BenefitExplanation
Reduced latencyContent served from nearby edge servers
Lower origin loadFewer requests hit the origin server
Higher availabilityCDN nodes provide redundancy
Global scalabilityEfficiently serve users worldwide
DDoS protectionTraffic distributed across many nodes

Without CDN:

Diagram
flowchart LR Users --> OriginServer

All traffic hits the origin server.

With CDN:

Diagram
flowchart LR Users --> EdgeServers EdgeServers --> OriginServer

Most requests are served at the edge layer.


Types of Content Delivered by CDN

CDNs primarily deliver static and semi-static content.

Content TypeExamples
Static assetsImages, CSS, JS
Media filesVideos, audio
DocumentsPDFs
Downloadable filesSoftware packages
API responsesCached API responses

Dynamic content can also be optimized through edge computing and caching strategies.


Core CDN Architecture

A typical CDN architecture includes several key components.

Diagram
flowchart LR User --> DNS DNS --> EdgeServer EdgeServer --> Cache EdgeServer --> OriginServer

Components:

ComponentRole
DNS routingDirects user to nearest edge
Edge serversCache and serve content
Origin serverSource of truth
Cache storageStores cached objects
CDN control planeManages configuration

CDN Request Flow

Let’s walk through a typical CDN request.

Diagram
sequenceDiagram participant User participant DNS participant Edge participant Origin User->>DNS: Resolve domain DNS-->>User: Return nearest CDN edge User->>Edge: Request content alt Cache Hit Edge-->>User: Return cached content else Cache Miss Edge->>Origin: Fetch content Origin-->>Edge: Return content Edge->>Edge: Store in cache Edge-->>User: Return content end

Steps:

  1. User resolves domain using DNS.
  2. DNS directs request to nearest CDN edge.
  3. Edge server checks its cache.
  4. If cached → return immediately.
  5. If not → fetch from origin.
  6. Store response for future requests.

Edge Servers

Edge servers are distributed geographically across the globe.

Example:

RegionEdge Servers
North AmericaMultiple cities
EuropeMultiple data centers
AsiaRegional nodes
South AmericaLocal edge nodes

This reduces the physical distance between user and content.

Example architecture:

Diagram
flowchart LR UserIndia --> EdgeMumbai UserEurope --> EdgeFrankfurt UserUS --> EdgeNewYork EdgeMumbai --> Origin EdgeFrankfurt --> Origin EdgeNewYork --> Origin

CDN Caching

CDNs store frequently accessed content at edge servers.

Common caching rules include:

StrategyDescription
TTL cachingContent expires after time
Cache headersControlled by HTTP headers
Manual invalidationPurge outdated content
Versioned assetsUnique URLs for updates

Example HTTP cache headers:

Cache-Control: max-age=3600

Meaning:

Cache this content for 1 hour

Cache Invalidation in CDN

When content changes, CDN caches must be updated.

Methods:

MethodDescription
TTL expirationContent automatically expires
Cache purgeManually remove cached objects
VersioningChange asset URL
Soft refreshCDN fetches new version

Example:

image_v2.png

Changing file name forces cache refresh.


CDN Request Routing

CDNs use intelligent routing algorithms to determine the best edge server.

Common strategies:

StrategyDescription
Geo routingClosest geographic server
Latency routingLowest network latency
Load-based routingAvoid overloaded servers
Anycast routingSame IP across multiple nodes

Example routing:

Diagram
flowchart LR User --> DNS DNS --> EdgeClosest

DNS directs user to the optimal edge location.


Anycast Routing

Many CDNs use Anycast networking.

Multiple servers share the same IP address, and the network automatically routes users to the closest instance.

Diagram
flowchart LR User --> Internet Internet --> EdgeA Internet --> EdgeB Internet --> EdgeC

Benefits:

BenefitExplanation
Automatic routingInternet chooses closest node
High availabilityFailover automatically
Low latencyShorter network paths

Edge Computing

Modern CDNs support running code at the edge.

This allows dynamic logic to run closer to users.

Example use cases:

Use CaseDescription
AuthenticationValidate tokens
A/B testingServe different versions
PersonalizationModify content per user
Security filteringBlock malicious requests

Edge computing architecture:

Diagram
flowchart LR User --> EdgeCompute EdgeCompute --> Origin

Instead of always contacting the origin, logic runs directly at edge servers.


CDN for Video Streaming

Video streaming platforms rely heavily on CDNs.

Example flow:

Diagram
flowchart LR User --> EdgeCache EdgeCache --> VideoChunks VideoChunks --> OriginStorage

Videos are divided into small chunks, which are cached at edge locations.

Benefits:

BenefitExplanation
Faster playbackLocal video segments
Reduced bufferingLow latency
Lower bandwidth costsFewer origin requests

CDN Security Features

CDNs often provide security protections.

FeatureDescription
DDoS mitigationAbsorb traffic floods
Web Application Firewall (WAF)Filter malicious requests
Rate limitingPrevent abuse
Bot protectionBlock automated attacks

Security architecture:

Diagram
flowchart LR User --> CDN CDN --> SecurityLayer SecurityLayer --> Origin

The CDN acts as a protective shield for the origin server.


Multi-CDN Architecture

Large platforms sometimes use multiple CDN providers.

Architecture:

Diagram
flowchart LR User --> DNS DNS --> CDN1 DNS --> CDN2 CDN1 --> Origin CDN2 --> Origin

Benefits:

BenefitExplanation
Higher reliabilityFailover between CDNs
Better performanceChoose fastest CDN
Cost optimizationTraffic distribution

CDN vs Reverse Proxy

Both CDNs and reverse proxies sit between users and servers.

FeatureCDNReverse Proxy
Geographic distributionGlobalUsually local
Edge cachingYesLimited
Performance optimizationHighModerate
Security featuresAdvancedBasic

CDNs are essentially globally distributed reverse proxies with caching.


Trade-offs of CDN

CDNs provide huge benefits but also introduce complexities.

AdvantageTrade-off
Faster content deliveryAdditional infrastructure
Reduced origin loadCache consistency challenges
Global scalabilityCost
Improved securityConfiguration complexity

Proper cache management is critical.


Real World CDN Architecture

Large-scale systems combine CDNs with multiple layers of caching.

Diagram
flowchart LR User --> BrowserCache BrowserCache --> CDN CDN --> LoadBalancer LoadBalancer --> AppServers AppServers --> Database

This layered architecture ensures:

  • low latency
  • high scalability
  • fault tolerance

Summary

A Content Delivery Network (CDN) is a distributed system designed to deliver content faster and more reliably by placing cached data closer to users.

Core concepts:

ConceptMeaning
Edge serversCDN nodes near users
Origin serverSource of content
Cache hitData served from CDN
Cache missCDN fetches from origin
Anycast routingIntelligent traffic routing

CDNs are essential for modern applications that must serve millions of users globally with low latency.

They dramatically improve:

  • performance
  • scalability
  • reliability
  • security

As a result, nearly every large-scale web platform relies on CDNs as a core infrastructure component.