CDN and Edge Computing — Serve Faster by Getting Closer
The speed of light is your bottleneck#
Your server is in Virginia. Your user is in Tokyo. Light through fiber takes ~70ms one way. With TCP handshake, TLS, and the actual request, that's 200-400ms before any data arrives.
The only way to beat physics: put the content closer to the user.
What a CDN does#
A Content Delivery Network caches your content at edge servers worldwide. When a Tokyo user requests your page, they hit a server in Tokyo — not Virginia.
Static assets (images, CSS, JS, fonts): Cached at edge, served in 5-20ms.
API responses (cacheable): Cached at edge with short TTL, reducing origin load by 80-95%.
Dynamic content: Passes through to origin, but the edge handles TLS termination and connection pooling.
CDN caching strategies#
Cache-Control headers#
Cache-Control: public, max-age=31536000, immutable
public— CDN can cache thismax-age=31536000— cache for 1 yearimmutable— don't revalidate even on refresh
For versioned assets (app.a1b2c3.js): Cache forever. The filename changes when content changes.
For HTML pages: Short TTL or stale-while-revalidate:
Cache-Control: public, max-age=60, stale-while-revalidate=300
Serve cached for 60s, then serve stale while fetching fresh in background.
Cache invalidation#
The CDN cached the wrong version. How do you fix it?
- Purge: Delete specific URLs from the cache
- Versioned URLs:
/api/v2/dataor/app.abc123.js— never invalidate, just deploy new version - Cache tags: Tag responses, purge by tag (
purge all product-123 related)
Edge computing#
CDNs cache static content. Edge computing runs code at the edge.
Edge functions#
Lightweight JavaScript/WASM running at CDN edge locations.
Cloudflare Workers, Vercel Edge Functions, Deno Deploy
export default function handler(request) {
const country = request.headers.get("cf-ipcountry");
if (country === "DE") {
return Response.redirect("/de/");
}
return fetch(request);
}
Use cases:
- Geo-routing and personalization
- A/B testing at the edge
- Auth token validation (no round trip to origin)
- API rate limiting
- Bot detection and blocking
Edge vs serverless#
| Edge functions | Serverless (Lambda) | |
|---|---|---|
| Latency | 1-5ms startup | 50-5000ms cold start |
| Location | 300+ global POPs | ~20 regions |
| Runtime | V8 isolates (limited) | Full Node/Python/etc |
| Duration | 30s max (Workers) | 15 min max (Lambda) |
| Use case | Middleware, routing | Backend logic |
Edge is for lightweight, latency-sensitive work. Serverless is for heavier computation.
The modern stack#
User → Edge (CDN + Workers) → Origin (API) → Database
↓
Cached responses
Auth validation
Geo-routing
A/B tests
The edge handles everything it can. Only requests that need fresh data hit the origin.
Providers#
| Provider | CDN | Edge compute | Best for |
|---|---|---|---|
| Cloudflare | Global, fast | Workers (V8) | Full-stack edge |
| Vercel | Global | Edge Functions | Next.js apps |
| AWS CloudFront | Global | Lambda@Edge | AWS ecosystem |
| Fastly | Global | Compute@Edge (WASM) | Custom caching logic |
| Akamai | Largest network | EdgeWorkers | Enterprise |
See CDNs in your architecture#
On Codelit, every architecture with a frontend includes CDN components. Click the CDN node to audit caching strategy, see data flow from origin to edge, and identify what should be cached.
Optimize your architecture's performance: describe your system on Codelit.io and see where edge caching fits.
Try it on Codelit
Chaos Mode
Simulate node failures and watch cascading impact across your architecture
AI Architecture Review
Get an AI audit covering security gaps, bottlenecks, and scaling risks
Related articles
Try these templates
Build this architecture
Generate an interactive architecture for CDN and Edge Computing in seconds.
Try it in Codelit →
Comments