Core Module
12 min forge
CDN (Content Delivery Network)
Learn how to cache content globally. Understand how CDNs reduce latency by bringing data closer to the user.
π CDN: Content Delivery Network
A Content Delivery Network (CDN) is a highly-distributed platform of servers that helps minimize delays in loading web page content by reducing the physical distance between the server and the user.
π‘ The Logic (ELI5)
Think of a Global Pizza Chain:
- You have one main kitchen in Italy (The Origin Server).
- If a customer in New York wants a pizza, it takes 8 hours to fly it there (High Latency).
- Instead, you build small Local Kitchens in New York, London, and Tokyo.
- You pre-deliver the dough and tomato sauce to these kitchens.
- When a New York customer orders, they get their pizza from the New York kitchen in 15 minutes.
- The CDN is that network of local kitchens.
π The Deep Dive
Edge Servers vs. Origin Server
- Origin Server: The main server where your original website/app lives.
- Edge Server (POP - Points of Presence): Servers distributed globally that store cached copies of your data.
Push vs. Pull CDN
- Pull CDN: The CDN automatically "pulls" an image from your origin server the first time a user requests it, then caches it for future users. (Most common).
- Push CDN: You manually "upload" content to the CDN whenever you update it. This is best for large files or infrequent updates.
π― Interview Pulse
What to cache on a CDN?
- Static Content: Images, Videos, CSS files, JavaScript files.
- Dynamic Content: Historically no, but modern CDNs (like Cloudflare) can even cache dynamic responses for a few seconds to handle viral traffic.
The Power of "Nearness"
Lower latency is not the only benefit. A CDN also:
- Reduces Bandwidth Costs: Traffic is served from the CDN, not your expensive origin server.
- Protects against DDoS: The massive CDN network absorbs the attack before it reaches your small server.
Popular Tooling
- Cloudflare, Akamai, Amazon CloudFront, Fastly.
Key Term: TTFB (Time To First Byte)
CDNs significantly reduce TTFB because the user's browser connects to a server just a few miles away instead of across an ocean. π