ayushsalampuriya.xyzRevise

HLD · Edge

CDN & Content Delivery

Serve static and cacheable content from edge locations close to users.

In simple words: A CDN stores copies of safe-to-cache files on servers close to users. This can shorten download time and reduce work at the origin server. For example, a user in Mumbai can receive 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

Do not put on a CDN: Personalized authenticated API responses unless carefully keyed and given a short TTL.

3. Cache control headers

Cache-Control: public, max-age=31536000, immutable # hashed JS/CSS Cache-Control: public, max-age=3600 # product image Cache-Control: private, no-store # user dashboard

ETag / Last-Modified: Conditional requests — 304 Not Modified saves bandwidth.

4. Pull vs push CDN

5. Invalidation

When you deploy new JS bundle, old cached version is stale.

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

  1. Static assets → CDN → S3 origin
  2. API → LB → app servers (not CDN-cached)
  3. 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.