What is a CDN?
A CDN is a globally distributed network of edge POPs that caches and serves your static (and sometimes dynamic) content closer to users, reducing latency and offloading your origin.
How it works
- Anycast routes users to the nearest healthy POP.
- POP caches content based on cache keys (URL + Vary headers) and TTLs.
- On miss, POP fetches from origin, applies transformations (compression, image resize), and stores the result.
Benefits
- Lower latency via POPs, origin offload, DDoS mitigation.
Best practices
- Precise TTLs and cache keys, selective purges, variants (device/lang).
- Signed URLs/cookies, HTTP/3, on‑the‑fly image resizing.
Cache key design
- Normalize query strings; include only meaningful parameters.
- Use Vary headers carefully to avoid low hit ratios.
Image optimization pipeline
# language-bash
# Example worker arguments: /img/w/800/q/70/path/to/file.jpg
resize --width "$W" --quality "$Q" < input | cwebp -o output.webp
Code: Cache-control for CDN friendliness
# language-http
Cache-Control: public, max-age=300, s-maxage=1800, stale-while-revalidate=600
Vary: Accept-Encoding, Accept-Language, User-Agent
Code: Cloudflare transform rule (pseudo)
# language-bash
# Tag image requests and route to an image resizing worker
cfctl rules create --if "http.request.uri.path matches '/img/.*'" \
--then "route_to=images-worker" --cache_ttl=86400
Purge and warm-up
- Purge by tag/path rather than full site.
- Pre‑warm hot paths before big launches or deploys.
Metrics and SLOs
- Edge hit ratio, origin fetch rate, p95/p99 edge latency, 4xx/5xx at edge vs origin.
- Error budget for cache misses under traffic spikes.