DEV Community

Joud Awad
Joud Awad

Posted on

CDN For System Design

Say "add a CDN" in a system design interview and the next four questions decide whether you pass.

One photo answers all four. Uploaded once to a server in Virginia. Opened by someone in Sydney.

Before a single line of your code runs, geography has already charged that viewer about 930 milliseconds. A bigger server fixes none of it.

The four questions, usually within thirty seconds:

What is your cache key? What does a TTL expiry cost the origin? Which edge answers, and why? How do you serve a private file from a public machine?

I followed that one photo for 44 minutes through all four. A few things I wish someone had told me earlier.

Most engineers think the cache key is the URL. It isn't. It's the method plus the URI plus whatever your config or a Vary header bolts on. CloudFront's default CachingOptimized policy ignores query strings entirely. Cloudflare's default includes the full query string, verbatim, order included. Same photo URL, opposite behavior, two vendors.

Add Vary: User-Agent and you get one cache entry per browser build. One extra header can take a hit ratio from the high nineties into single digits.

A long TTL is permission, not a guarantee.

Eviction happens whenever the edge wants the space back. And when a viral object does expire, it expires on every tier at once, so edges everywhere turn around and hit your origin in the same window. That's the thundering herd. Request collapsing and Origin Shield exist so that as few as one request per object reaches you.

Purges are priced by the path you submit, not the files it clears. CloudFront gives you 1,000 free paths a month, then $0.005 each. Which is the actual argument for versioned filenames: photo.a3f9.jpg never needs purging.

Which edge answers? GeoDNS guesses from your resolver's address, not yours. EDNS Client Subnet patches the guess. Anycast removes it: every edge announces the same IP, and failover becomes a routing event with no DNS TTL to wait out.

And the number interviewers ask for is the wrong one. Going from 90% to 95% hit ratio sounds like five points. It halves your origin load. Origin offload is the metric. Hit ratio is the proxy.

The private file? Signed URLs for a single object. Signed cookies when one viewer needs a hundred of them.

Full walkthrough, one photo end to end, built from the vendor docs and the RFCs rather than folklore:

https://www.youtube.com/watch?v=gV_z0uG8_uo

Top comments (0)