Content Delivery Network (CDN)¶
What it is¶
A CDN is a globally distributed network of proxy servers that serves content from locations closer to the user. Typically used for static files (HTML/CSS/JS, photos, videos); some CDNs (e.g., Amazon CloudFront) also support dynamic content. The site's DNS resolution tells clients which CDN server to contact.
Why it improves performance¶
- Users receive content from data centers close to them.
- Your origin servers don't have to serve the requests the CDN fulfills.
Two models¶
Push CDNs¶
- Receive new content whenever changes occur on your server.
- You upload content directly to the CDN and rewrite URLs to point to it.
- You control when content expires and updates.
- Content is uploaded only when new/changed → minimizes traffic, maximizes storage.
- Best for: low-traffic sites, or content that rarely changes.
Pull CDNs¶
- Grab new content from your server when the first user requests it.
- Content stays on your server; you rewrite URLs to point to the CDN.
- First request is slower until content is cached.
- TTL determines how long content is cached.
-
Minimizes storage on the CDN, but can cause redundant traffic if files expire and are re-pulled before they actually change.
-
Best for: heavy-traffic sites (spreads load, keeps only recently-requested content).
Push vs Pull at a glance¶
| Push CDN | Pull CDN | |
|---|---|---|
| Who uploads content | You (on change) | CDN (on first request) |
| Storage usage | High | Low |
| Traffic | Minimal | Potential redundancy |
| Best for | Low traffic / rare updates | High traffic |
Disadvantages of CDNs¶
-
Cost can be significant depending on traffic (weigh against the cost of not using one).
-
Content can be stale if updated before TTL expires.
- Requires changing URLs for static content to point to the CDN.
Key takeaways¶
- A CDN is essentially a geographically distributed cache.
- It reduces both latency (proximity) and origin load (offload).
- Push vs pull is a storage-vs-traffic trade-off.
Common Interview Questions¶
Design a content delivery network (CDN) like Cloudflare
Why it's asked here: this is the direct CDN question.
Key points to discuss:
- Push vs pull model and which fits the workload.
- Edge PoPs, the origin server, and cache eviction (LRU) plus TTL.
- How DNS routes users to the nearest PoP (anycast).
- Cache invalidation and stale-content trade-offs.
flowchart LR
U[User] --> E[Edge POP]
E -->|cache hit| U
E -->|cache miss| O[Origin server]
O --> E
Design a photo-sharing system like Instagram
Why it's asked here: media delivery is a textbook CDN use case.
Key points to discuss:
- Store originals in object storage (S3) and serve via CDN.
- Generate multiple resolutions; serve the right size at the edge.
- Push uploads to the CDN vs pull on first request.
- Separate the metadata (DB) path from the media (CDN) path.
flowchart LR
U[User] --> A[App server]
A -->|upload| S3[(Object storage)]
A --> DB[(Metadata DB)]
U --> CDN[CDN]
CDN --> S3