Content Delivery Networks (CDN)

Serve static content from edge servers near your users instead of a single origin.

Beginner6 min read3 interview questions

A CDN is a global network of edge servers that cache your static files — images, scripts, stylesheets, video. A user in Dhaka is served from a nearby edge instead of an origin server in Virginia.

Modern CDNs also terminate TLS, absorb DDoS traffic, run edge functions and route dynamic API calls over optimised backbone networks.

Diagram

~20 ms~15 msfetch oncerevalidateUser — AsiaUser — EuropeEdge — Singaporecache hitEdge — Frankfurtcache missOrigin serveror object storage
Users connect to the closest edge; only cache misses travel to the origin.

Key concepts

Edge / PoP
A point of presence — a data centre of the CDN close to users.
Origin
Your real server or storage bucket that holds the master copy.
Cache-Control
HTTP header that tells the CDN and browser how long to cache, e.g. public, max-age=31536000, immutable.
Purge / invalidation
Removing a file from all edges before it expires.
Cache busting
Putting a version or hash in the file name (app.3f9a.js) so new deploys never serve stale files.

Trade-offs

Benefits

  • Much lower latency for distant users
  • Offloads bandwidth and requests from the origin
  • Built-in DDoS absorption and TLS at the edge

Costs

  • Stale content if cache rules or purges are wrong
  • Cost scales with traffic
  • Debugging which edge served what can be hard

Use it when

  • Static assets of any public website or SPA
  • Video, downloads and large media
  • Global audiences

Common mistakes

  • Caching personalised or authenticated API responses at the edge
  • Long cache times on files without hashed names
  • Forgetting to set CORS headers for fonts and scripts served from the CDN

Interview questions

Q1How do you deploy a new version of a JavaScript file without users getting the old one from the CDN?
Use content-hashed file names and long cache times for assets, and a short cache time for the HTML page that references them.
Q2Push CDN vs pull CDN?
A pull CDN fetches from the origin on the first miss — simple and common. A push CDN has you upload content ahead of time — useful for large, rarely changing files.
Q3Can a CDN help a dynamic API?
Yes: TLS termination near users, persistent connections to the origin, DDoS protection and caching of public GET responses with short TTLs.