System Design - EP 13: Content Delivery Networks (CDNs)
Source: Dev.to
CDN analogy – The Global Pizza Empire
Imagine you run the world’s best pizza shop, Uncle Tony’s, in New York City.
- Origin server – the New York kitchen where every pizza (content) is created.
- Edge servers – small reheating stations in major cities (London, Tokyo, Mumbai, Sydney) that hold inventory but don’t have the full kitchen.
Cache miss (first request)
A customer in London orders a pepperoni pizza. The local station is empty, so it calls New York, gets a fresh pizza, and delivers it. The station now stores a stash of frozen pepperoni pizzas.
Cache hit (subsequent request)
Later, another London customer orders the same pizza. The station finds a frozen pizza in its freezer, reheats it, and delivers it quickly—no call to New York needed.
Key CDN concepts for system design
Edge servers and origin
- Edge servers are geographically distributed machines operated by CDN providers (e.g., Cloudflare, Akamai, Fastly).
- Users are routed to the nearest edge server, reducing round‑trip distance.
- The origin (e.g., an AWS EC2 instance or S3 bucket) is contacted only when the edge does not have the requested asset.
TTL – Time‑to‑Live
- Each cached file has a TTL that defines how long it stays on the edge server.
- Example: an image with a TTL of 24 hours remains cached for a day; after that, the edge treats it as stale, deletes it, and fetches a fresh copy from the origin.
- TTL balances freshness against the cost of repeated origin fetches.
GeoDNS routing
- CDNs use GeoDNS to resolve a user’s request to the nearest edge based on IP location.
- When a user types your URL, the DNS resolver checks the IP, determines the region (e.g., Germany), and directs the request to the Frankfurt edge instead of New York.
Reducing Time to First Byte (TTFB)
- By serving static assets (images, videos, CSS, HTML) from a nearby edge, the TTFB drops dramatically.
- Users in Tokyo receive assets from a Shinjuku edge server, traveling far fewer miles than from New York.
CDN as a mitigation layer
- During traffic spikes (e.g., Super Bowl Sunday) or malicious attacks (e.g., a botnet flooding your origin), the edge network absorbs the load.
- Edge servers can identify and block unwanted traffic before it reaches the origin, protecting the core infrastructure.
Using the pizza analogy helps visualize how CDNs cache, route, and protect content, turning a single “kitchen” into a global network of fast, reliable delivery points. This model is essential knowledge for system‑design interviews and real‑world cloud architectures.