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.
Explore the Netflix architecture interactively
Try it →Try these templates
Continue learning
Go deeper on performance and scale
As an Amazon Associate I earn from qualifying purchases. Codelit may receive a commission at no extra cost to you.
Systems Performance
Brendan Gregg · 2020
Reference tables and USE-method checklists for when every dashboard is green and latency is not.
4.8 (335)#1 in Computer Performance OptimizationBPF Performance Tools
Brendan Gregg · 2019
150+ ready-to-run bpftrace and BCC tools, with the one-liners and the output you get on a live box.
Software Architecture: The Hard Parts
Neal Ford, Mark Richards, Pramod Sadalage, Zhamak Dehghani · 2021
Works through service granularity, distributed data and saga patterns as full trade-off case studies.
4.6 (731)#2 in Software Design ToolsDatabase Internals
Alex Petrov · 2019
Explains B-trees, LSM trees and consensus protocols so you can predict how a datastore will behave.
4.6 (560)#2 in Management Information Systems