A Content Delivery Network (CDN) is a global network of cache servers (edge locations, also called points of presence). Users fetch content from the nearest edge instead of your origin.
flowchart LR U1["User in Hanoi"] --> E1["Edge Singapore"] U2["User in Paris"] --> E2["Edge Frankfurt"] E1 -->|"miss only"| O["Origin: S3 / load balancer"] E2 -->|"miss only"| O DNS["DNS / traffic steering"] -.-> E1 DNS -.-> E2 %% step U1: DNS sends each user to a nearby edge location. %% step E1: The edge serves cached content in a few milliseconds. %% step O: Only cache misses travel to the origin, which sees a fraction of the traffic. %% step DNS: With multi-CDN, a steering layer picks between CDN providers per region or by health.
What a CDN gives you
- Latency: content is served from tens of milliseconds away instead of hundreds.
- Offload: the origin serves misses only — often under 10% of requests.
- Resilience: absorbs spikes and DDoS; can serve stale content when the origin is down.
- Security and TLS: TLS terminates at the edge; WAF and bot rules run there.
Freshness
| Control | Use |
|---|---|
Cache-Control: max-age=31536000, immutable |
Versioned files (app.4f9a.js) — cache forever |
Cache-Control: max-age=60 |
HTML or API responses that change often |
s-maxage |
TTL for shared caches (CDN) only |
| Invalidation / purge | Remove a path now (costs time and sometimes money) |
| Cache key | Which URL parts, headers or cookies make a different object — keep it small |
Best practice: version file names instead of invalidating; short TTLs for HTML.
Headers like X-Cache: Hit from cloudfront or cf-cache-status: HIT and Age tell you whether a response came from the edge.
Multi-CDN
Single CDN: all content from one provider. Multi-CDN: content is served by two or more providers, steered by DNS or a traffic manager.
| Benefit | Why |
|---|---|
| Resiliency | One CDN outage does not take the site down |
| Performance | Pick the fastest CDN per country or network, using real-user measurements |
| Cost flexibility | Send more traffic to the cheaper provider; better negotiating position |
| Scale | Combined capacity for extreme events (big launches, live sport) |
Costs: same configuration in several providers (cache rules, TLS certificates, WAF), more complex logs and invalidation, steering logic to run.
Traps: caching personalised pages under a shared key (users see each other's data); cache key including every cookie (hit ratio near zero); invalidating /* on every deploy; forgetting to restrict the origin so users can bypass the CDN.