What a CDN does
A CDN distributes copies of your content to edge nodes worldwide. Requests are served from the nearest edge node, not your origin server. Benefits: lower latency for users, lower load on origin, DDoS mitigation.
What to cache at the edge
- Static assets (JS, CSS, images) — long TTL (1 year) with content-hash URLs for cache busting.
- Public API responses — short TTL (30–300s) with
stale-while-revalidate. - HTML pages for non-personalised content.
- Never cache: authenticated API responses, session-specific content, payment endpoints.
Cache key design
The default cache key is the full URL including query string. Normalise query params (sort alphabetically) to maximise hit rate. Add custom dimensions to the cache key only when necessary (e.g., country for geolocalised content).
Purging strategies
- TTL expiry — simplest, but content is stale until TTL expires.
- Tag-based purging — CloudFront, Fastly, and Cloudflare support cache tags. Purge all resources tagged with a content ID when that content changes.
- URL-based purging — fine-grained but requires knowing every cached URL for a given resource.