HLD · Edge
CDN & Content Delivery
Serve static and cacheable content from edge locations close to users.
logo.png from a nearby edge after it is cached there.1. What is a CDN?
A Content Delivery Network is a geographically distributed set of edge servers. Each edge location, or point of presence (PoP), can cache content from an origin server.
Example: A user in Mumbai requests cdn.site.com/logo.png. After the nearby edge caches the file, it can serve later requests without fetching it from a distant origin every time.
2. What belongs on a CDN
- Images, videos, CSS, JS bundles
- Static HTML (marketing pages)
- Cacheable API responses (public product catalog) with correct headers
Do not put on a CDN: Personalized authenticated API responses unless carefully keyed and given a short TTL.
3. Cache control headers
ETag / Last-Modified: Conditional requests — 304 Not Modified saves bandwidth.
4. Pull vs push CDN
- Pull (most common): CDN fetches from origin on first request, then caches. Origin = S3, your web server.
- Push: You upload content to CDN proactively. Live streaming, large file distribution.
5. Invalidation
When you deploy new JS bundle, old cached version is stale.
- Cache busting: Filename hashing —
app.a1b2c3.js(best practice) - Purge API: Cloudflare/Fastly purge by URL or tag — use sparingly (propagation delay)
Concrete example: Deploy app.91ac.js instead of replacing app.js. New HTML points to the new file, while an older page can still load the old file until its TTL ends.
6. CDN + dynamic origin pattern
- Static assets → CDN → S3 origin
- API → LB → app servers (not CDN-cached)
- Hybrid: CDN caches GET /api/v1/products with 60s TTL at edge
7. DDoS and TLS
A CDN can help absorb and filter some volumetric attacks across its edge network, but protection depends on the provider and configuration. TLS often terminates at the CDN; the connection from the CDN to the origin should also be protected.
8. Example: video streaming (YouTube-like)
Upload → origin storage → transcoding → segments to CDN. Playback requests HLS chunks from nearest PoP. Popular videos stay hot in cache.
9. Providers
Cloudflare, Akamai, Fastly, AWS CloudFront, Google Cloud CDN.
Quick revision
- CDN: Edge cache → lower latency, less origin load.
- Cache-Control drives CDN behavior; hash filenames for busting.
- Pull CDN on cache miss fetches origin once.
- Versioned filenames: Publish changed static assets without a broad purge.
- Static on CDN; personalized APIs usually bypass or short TTL.
- Security: CDN protections and TLS depend on correct configuration.