>_
EngineeringNotes
← Back to System Design

Content Delivery Network (CDN)

Mastering global delivery: Serving high-performance content from edge locations to minimize latency and scale your system to millions.

Introduction & Use Case

Content Delivery Networks make systems cheaper and faster. They act as a distributed cache layer near your users, reducing the distance data has to travel from an origin server (like an S3 bucket or a backend API).

Prerequisites

  • Understanding Caching logic
  • Basic knowledge of Distributed Systems
Latency Metric

A delay of even 0.5s causes users to lose trust. Speed is directly correlated with perceived professionalism.

Regulatory Alignment

Local storage helps meet regional regulations (e.g., movies only viewable in India or specific US states).

The Core Problem: Distance

Without CDN

User (India) ➔ Server (US)

High latency due to physical distance.
Origin server overload during high concurrent traffic.
Increased bandwidth (egress) costs on the primary origin.
With CDN

User (India) ➔ Edge (India)

Low latency delivery from local cache (PoP).
Offloads traffic from the origin server.
Highly scalable and cost-efficient architecture.

How CDN Works (Request Lifecycle)

1

DNS Resolution

User requests www.example.com. The DNS doesn't return the origin server IP; it returns the IP of the nearest CDN Edge server based on geo-location.

2A

Cache HIT

Edge server has the file. It is returned instantly to the user (~15ms). Extreme speed as the request never hits the origin backend.

2B

Cache MISS

Edge doesn't have file. It fetches it from the origin, caches it locally for future requests, and then delivers it to the user (~250ms).

Caching Strategy

Static Content

Images, Videos, JS/CSS, Fonts. This is the primary use case for CDNs.

Dynamic Content

Modern CDNs can handle APIs and personalized data via Edge Logic.

Cache Headers

Cache-Control, Expires, ETag decide longevity and invalidation logic.

The Real-World Path of a CDN Request

1
User Device

Requests assets

DNS Query
2
DNS & Routing

Resolves Edge IP

Route to POP
HIT?MISS?
3
Edge Cache (PoP)

Checking Local Storage

Wait... Cache MISS
4
Origin Server

S3 / Primary Data Source

Step 1

Device sends request for static content.

Step 2

DNS and Controller pick the closest POP based on location.

Step 3

POP checks Cache. If HIT, returns in 10ms. If MISS, goes to Step 4.

Step 4

Full-trip to origin. Edge caches file for all future requests.

CDN Architectures

Push CDN

You upload content to the CDN edge server manually or via automated triggers.

  • Full control over availability
  • Manual management effort

Pull CDN

The CDN fetches content from origin automatically when a user visits a missing asset.

  • Zero maintenance (Common choice)
  • Latency on first access (MISS)

Advanced CDN Concepts

Cache Invalidation

"There are only two hard things in CS: cache invalidation and naming things." Methods: TTL, Purge, Versioning.

Edge Computing

Run code at the edge (PoP) for Authentication, A/B Testing, or Request Rewriting.

DDoS Protection

CDNs act as a shield, absorbing massive traffic and blocking malicious requests before they reach your backend.

Load Balancing

Distributes traffic logically across multiple edge nodes and regions for maximum uptime.

Geo-Delivery

Deliver different content per region (e.g. movies restricted by local country licensing laws).

TLS Termination

Handling SSL/TLS handshake at the edge PoP reduces round-trip times for secure connections.