CDN
Amazon CloudFront
A worldwide cache that serves static files (and cacheable responses) from servers near the user.
In a monolith
App serves static files
Usually none — the app server serves /static itself, and every byte crosses the ocean to reach far-away users.
In a distributed system
Edge cache in front of origins
Assets and media come from edge locations; cache misses go to the origin (object storage or your API). It also soaks up DDoS traffic at the edge.
On AWS
Amazon CloudFront
Edge caching with S3 (via Origin Access Control) or ALB/API Gateway as origins. AWS WAF and Shield attach here.
- CloudFront + S3 origin$Static site and media; the bucket stays private via OAC
- CloudFront + ALB origin$$Caches selected API responses, forwards the rest
- CloudFront Functions / Lambda@Edge$$Small logic at the edge: redirects, header auth, A/B
- Reach for it when
- Users are far from your servers, or static/media bytes are eating bandwidth and server time.
- What it costs you
- Deploys need cache busting — use content-hashed filenames (app.3f2a.js)
- Stale content until TTL expiry or invalidation
- Per-GB pricing; personalised responses can’t be cached naively
- When it dies
- A managed CDN spans hundreds of edge locations, so an edge outage just reroutes users. The common failure is configuration: private data cached for everyone, or a 0% hit ratio.
Interview follow-ups
How do you ship new CSS without users getting stale files?
Content-hash filenames on every build; the HTML references the new name, old files can stay cached forever. Only the HTML needs a short TTL.
What can’t you cache at the CDN?
Per-user responses (unless carefully keyed), writes, and anything that must be real-time fresh.
Push vs pull CDN?
Pull: the edge fetches from origin on the first miss (most common). Push: you upload content ahead of time — good for large, rarely changing files.
Connection rules
Never
- CDN → SQL databaseCDN origins are HTTP servers or buckets, never databases or caches.
- CDN → Read replicaCDN origins are HTTP servers or buckets, never databases or caches.
- CDN → NoSQL storeCDN origins are HTTP servers or buckets, never databases or caches.
- CDN → CacheCDN origins are HTTP servers or buckets, never databases or caches.
- CDN → SearchCDN origins are HTTP servers or buckets, never databases or caches.
- CDN → DB proxyCDN origins are HTTP servers or buckets, never databases or caches.
- App server → CDNYour code doesn’t send traffic to the CDN — the CDN pulls from origins.
- Service → CDNYour code doesn’t send traffic to the CDN — the CDN pulls from origins.
- Worker → CDNYour code doesn’t send traffic to the CDN — the CDN pulls from origins.