CMD Guide
HomeSystem DesignSystem Design Trade-offs

CDN Usage vs Direct Server Serving

A CDN puts a physical copy of your bytes in a rack near the user and steers the request there — so the round-trip that dominates web latency shrinks from a cross-ocean 80 ms to a metro-local 5 ms — while direct serving forces every user to reach one origin no matter where they are. The whole trade-off turns on two mechanisms: how a request finds the nearest edge, and what happens when that edge does or does not already hold the object.

How a request reaches the nearest edge

Two mechanisms dominate, and real CDNs use one or both:

Once at the edge, the request keys into the edge cache (URL + selected headers, e.g. Vary: Accept-Encoding). A hit is served from the PoP's RAM/SSD in single-digit ms. A miss triggers an origin pull: the edge fetches from origin over an already-warm, pooled TLS connection, stores the object per its Cache-Control, and streams it back. So the first user in a region pays the origin round-trip; everyone after them rides the cache.

diagram
diagram

Worked example: one image, London user, origin in us-east-1

Origin is in Virginia; the London↔Virginia network RTT is ~80 ms and the London edge RTT is ~5 ms. Assume TLS 1.3 (1 RTT handshake) and 40 ms of origin app time. We trace time-to-first-byte (TTFB) for the same 200 KB asset three ways.

PathConnection setupRequest → first byteOrigin workTTFB
Direct serving (London → Virginia)TCP 1 RTT + TLS 1 RTT = 2 × 80 = 160 ms1 × 80 = 80 ms40 ms~280 ms
CDN cache HIT (London edge)TCP + TLS = 2 × 5 = 10 ms1 × 5 = 5 ms0 (served from PoP)~15 ms
CDN cache MISS (edge pulls origin)10 ms (client↔edge) + reuse warm pool to origin5 ms + origin pull 80 ms40 ms~135 ms

Read the punch line: a hit is ~18× faster than direct serving (15 ms vs 280 ms), and even a miss beats direct because the edge amortizes the expensive TLS handshake over a persistent, pre-warmed origin connection instead of paying three fresh cross-ocean round trips. If this asset gets 10,000 London requests during its TTL, exactly one pays the 135 ms miss and 9,999 pay 15 ms — a hit ratio of 99.99%. Origin egress and CPU drop by the same factor, which is often the bigger win than latency. Put it in throughput terms: 10,000 req/s at a 99% hit ratio leaves the origin only ~100 req/s, and at 50 KB per object the edge absorbs roughly 9,900 × 50 KB ≈ 500 MB/s of egress the origin never serves — the offload, not the millisecond, is usually what justifies the CDN.

Close the loop with money, because egress is priced per boundary crossing — the same hit ratio that saves the milliseconds saves the egress bill. That 500 MB/s the edge absorbs is 495 × 106 B/s × 2.592 × 106 s/month ≈ 1.28 PB ≈ 1,280,000 GB per month. Priced three ways (every rate approximate — [VERIFY: current provider pricing — rates change]):

Path for that 1.28 PB/moApproximate rateApproximate monthly bill
All direct: origin → internet egress~$0.09/GB (cloud internet-egress list rate)~$115,000
CDN delivery: edge → users~$0.02–$0.085/GB, tiered — petabyte volumes sit at the low tiers or negotiated rates~$26,000–$110,000, realistically toward the low end at this volume
Origin → CDN fill (the 1% misses)free on same-provider pairs (e.g. S3 → CloudFront)~$0

So at this scale the CDN plausibly pays for itself several times over in egress alone, before counting the origin fleet it lets you shrink. The arithmetic inverts at the other extreme: for a low-traffic or largely uncacheable workload, the hit ratio is poor, the origin still serves most bytes, and the CDN's per-request fees plus config surface exceed the little egress it saves — that is precisely the “direct serving wins” regime below.

Freshness: TTL, Cache-Control, and invalidation

The edge decides how long to keep an object from origin response headers. The knobs a systems engineer actually sets:

Pushing a change before TTL expiry needs invalidation, and there are two philosophies:

Pitfalls

When to use a CDN — and when direct serving wins

Reach for a CDN when the signals point that way: a geographically spread audience; static or cacheable content (images, video, JS/CSS, downloads) with a high read-to-write ratio; traffic spikes you can't provision origin for; or an origin whose egress bandwidth/CPU is the bottleneck. The CDN converts a latency problem and an origin-load problem into a cache-hit-ratio problem.

Prefer direct origin serving when the content is per-request dynamic and uncacheable (a bank statement, a search over live inventory), the audience is single-region and close to your origin (a metro-local internal tool), traffic is low enough that the origin never sweats, or strict data-residency rules forbid copies sitting in third-party PoPs. Here a CDN adds cost, an extra hop, a purge workflow, and a new dependency for a latency win that rounds to zero.

Trade-offs vs the named alternatives

Choose a CDN when you serve cacheable content to a global or bursty audience and origin load/latency is real. Prefer direct serving (optionally behind a regional reverse-proxy cache) when content is dynamic/personalized, the audience is local, or a third-party edge copy is a compliance or dependency risk. The common production answer is hybrid: CDN for static assets and cacheable API GETs, origin-direct for authenticated, write, and personalized paths.

Takeaways


Sources: Grokking the System Design Interview (DesignGurus); Cloudflare Learning Center on Anycast, cache hit ratio, and origin shield; Fastly and AWS CloudFront documentation on cache keys, TTL, and invalidation; MDN Web Docs on HTTP Cache-Control, ETag, and stale-while-revalidate; Ilya Grigorik, High Performance Browser Networking (O'Reilly) for RTT/TLS latency figures. Re-authored/Deepened for this guide.

🤖 Don't fully get this? Learn it with Claude

Stuck on CDN Usage vs Direct Server Serving? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **CDN Usage vs Direct Server Serving** (System Design) and want to truly understand it. Explain CDN Usage vs Direct Server Serving from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **CDN Usage vs Direct Server Serving** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **CDN Usage vs Direct Server Serving** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **CDN Usage vs Direct Server Serving** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes