System Design/
Lesson

Your server is in Virginia. A user in Tokyo makes a request: 200-300ms round-trip just for network latencyWhat is latency?The time delay between sending a request and receiving the first byte of the response, usually measured in milliseconds., before your code even runs. A CDNWhat is cdn?Content Delivery Network - a network of servers around the world that caches your files and serves them from the location closest to the user, making pages load faster. eliminates most of that by caching content on servers worldwide. Edge computing takes it further by running your code on those same servers.

How a CDNWhat is cdn?Content Delivery Network - a network of servers around the world that caches your files and serves them from the location closest to the user, making pages load faster. works

A CDN is a network of servers (Points of Presence or PoPs) distributed globally. Users get content from the nearest PoP.

Without CDN:
  User (Tokyo) ──── 200ms ────▶ Origin (Virginia)

With CDN:
  User (Tokyo) ──── 5ms ────▶ CDN PoP (Tokyo) (cache miss? fetch from origin once)Origin (Virginia)

The flow: DNSWhat is dns?The system that translates human-readable domain names like google.com into the numerical IP addresses computers use to find each other. resolves to the nearest PoP via anycast routing. On a cache hit, the response returns in 5-20ms. On a cache miss, the PoP fetches from your originWhat is origin?The combination of protocol, domain, and port that defines a security boundary in the browser, like https://example.com:443., caches the response, then returns it. Every subsequent nearby request is served from cache.

02

Cache-Control headers

The Cache-Control HTTPWhat is http?The protocol browsers and servers use to exchange web pages, API data, and other resources, defining how requests and responses are formatted. header tells CDNs and browsers what to cache and for how long.

Cache-Control: public, max-age=31536000, immutable
│              │                         │
│              │                         └── Don't revalidate even on refresh
│              └── Cache for 1 year (in seconds)
└── Any cache (CDN, browser) can store this
// Express middleware for setting cache headers
app.use('/static', (req, res, next) => {
  // Static assets with hashed filenames: cache forever
  res.set('Cache-Control', 'public, max-age=31536000, immutable');
  next();
});

app.use('/api', (req, res, next) => {
  res.set('Cache-Control', 'no-store');
  next();
});

app.get('/blog/:slug', (req, res) => {
  res.set('Cache-Control', 'public, max-age=600, stale-while-revalidate=60');
  // ...render blog post
});
Content typeCache-Control valueWhy
Hashed static assets (JS, CSS)public, max-age=31536000, immutableFilename changes on every build, so safe to cache forever
Images, fontspublic, max-age=86400Cache 24 hours; usually stable
HTML pagespublic, max-age=0, must-revalidateAlways check for updates, but use 304 Not Modified
Blog postspublic, max-age=600, s-maxage=3600Browser caches 10min, CDN caches 1hr
API responses (private)private, no-storeUser-specific data must never be cached by CDN
The s-maxage directive overrides max-age for shared caches (CDNs) only, the CDN caches for an hour while the browser re-checks every 10 minutes.

Cache busting

Include a content hash in filenames so new deployments are new URLs:

Before deploy: /static/app.a1b2c3d4.js  (cached for 1 year)
After deploy:  /static/app.e5f6g7h8.js  (new filename, new cache entry)

Build tools like Vite and Webpack do this automatically.

03

Edge computing

Traditional CDNs only cache static content. Edge computing runs actual server-side code at CDNWhat is cdn?Content Delivery Network - a network of servers around the world that caches your files and serves them from the location closest to the user, making pages load faster. locations.

Traditional CDN:
  Static file ──▶ CDN PoP ──▶ User
  (cache only)

Edge Computing:
  Code + data ──▶ CDN PoP ──▶ User
  (runs your logic at the edge)
// Cloudflare Worker: A/B testing at the edge
export default {
  async fetch(request) {
    const url = new URL(request.url);

    const cookie = request.headers.get('Cookie') || '';
    let variant = cookie.includes('ab_variant=B') ? 'B' : 'A';

    if (!cookie.includes('ab_variant')) {
      variant = Math.random() > 0.5 ? 'B' : 'A';
    }

    if (variant === 'B') {
      url.pathname = '/landing-b' + url.pathname;
    }

    const response = await fetch(url.toString());
    const newResponse = new Response(response.body, response);

    newResponse.headers.set(
      'Set-Cookie',
      `ab_variant=${variant}; Path=/; Max-Age=2592000`
    );

    return newResponse;
  }
};
// Vercel Edge Function: geolocation-based routing
import { NextRequest, NextResponse } from 'next/server';

export function middleware(request: NextRequest) {
  const country = request.geo?.country || 'US';

  if (country === 'JP' || country === 'KR') {
    return NextResponse.rewrite(new URL('/api/asia', request.url));
  }
  if (country === 'DE' || country === 'FR') {
    return NextResponse.rewrite(new URL('/api/europe', request.url));
  }

  return NextResponse.next();
}

export const config = { matcher: '/api/:path*' };
04

CDNWhat is cdn?Content Delivery Network - a network of servers around the world that caches your files and serves them from the location closest to the user, making pages load faster. and edge platform comparison

PlatformCDNEdge computeEdge databaseFree tierNotable feature
CloudflareYes (global)Workers (V8 isolates)D1, KV, Durable Objects100K req/dayFastest cold starts (~0ms)
VercelYes (via CDN partner)Edge Functions (V8)Vercel KV, Postgres100K exec/monthDeep Next.js integration
AWS CloudFrontYes (global)Lambda@Edge, CloudFront FunctionsDynamoDB Global Tables1M req/month (CF Functions)Full AWS ecosystem
FastlyYes (global)Compute@Edge (Wasm)KV StoreLimitedReal-time purging (<150ms)
05

When to use edge computing vs originWhat is origin?The combination of protocol, domain, and port that defines a security boundary in the browser, like https://example.com:443.

Good for edge: authenticationWhat is authentication?Verifying who a user is, typically through credentials like a password or token. checks, redirects, A/B testing, geolocation routing, header manipulation, simple APIWhat is api?A set of rules that lets one program talk to another, usually over the internet, by sending requests and getting responses. responses from KV stores, bot detection.

Keep at origin: complex database joins, ML inference, file processing, anything needing more than ~50ms of compute, transactions spanning multiple systems.

Edge environments have limited CPU time (typically 10-50ms), limited memory, and restricted API access. If your logic fits those constraints, put it at the edge.

AI pitfall
AI will recommend putting "your API at the edge" for lower latency. What it gets wrong: if your API queries a database in us-east-1, running API code at a Tokyo edge node does not help, the request still crosses the Pacific to reach the database. Edge computing only reduces latency when the data is also at the edge (KV stores, cached responses) or the logic is self-contained.
Good to know
A CDN is the single highest-ROI infrastructure investment for most web applications. For roughly $20/month, you eliminate the majority of your server load for static assets and cached API responses.
Edge case
CDN cache invalidation across a global network is not instantaneous. Some edge nodes may serve old content for minutes after deployment. Use content-hashed filenames so new deployments are new URLs, avoiding stale cache issues entirely.