Content Delivery Networks (CDN)

When designing a global application, the speed of light becomes a real problem. If your server is in New York and a user is in Tokyo, every HTTP request must travel halfway across the world, introducing hundreds of milliseconds of latency.

A CDN solves this by aggressively caching static content physically closer to the users.

[!TIP] ELI5: The Supermarket Warehouse Imagine an Amazon warehouse in New York. If someone in Tokyo buys a toothbrush, it takes 3 weeks to ship it across the ocean (High Latency).

The Solution (A CDN): Amazon builds 50 smaller "Edge Warehouses" around the world. They pre-stock the Tokyo warehouse with the most popular items (like toothbrushes). Now, when the Tokyo customer orders, the toothbrush is delivered locally in 1 day. If they order an obscure item not in the local warehouse (Cache Miss), the order is routed to New York.

1. How a CDN Works

A CDN is a globally distributed network of proxy servers (Edge Servers).

architecture-beta
    group global(cloud)[Global Network]
    
    service user1(server)[User (Tokyo)] in global
    service edge1(database)[Edge Server (Tokyo)] in global
    
    service user2(server)[User (London)] in global
    service edge2(database)[Edge Server (London)] in global
    
    service origin(disk)[Origin Server (New York)] in global
    
    user1:R -- L:edge1
    user2:B -- T:edge2
    
    edge1:R -- L:origin
    edge2:L -- R:origin
  1. The Request: A user in Tokyo requests logo.png.
  2. DNS Routing: The DNS resolution points the user to the IP address of the nearest CDN Edge Server (in Tokyo).
  3. Cache Hit: If the Tokyo server has logo.png cached, it returns it instantly (e.g., 10ms latency).
  4. Cache Miss: If the Tokyo server does not have it, the CDN requests it from the Origin Server in New York, returns it to the user, and caches it for the next Tokyo user.

2. What should be cached?

CDNs are perfect for Static Content:

CDNs are generally not used for Dynamic Content:

3. Cache Invalidation Strategies

The hardest part of caching is knowing when to delete stale data. If you update your company logo, you don't want the old logo stuck on the CDN for a week.

Time-To-Live (TTL)

Set an HTTP header Cache-Control: max-age=3600. The CDN will cache the file for 1 hour. After an hour, it deletes it and fetches a fresh copy from the origin.

Asset Fingerprinting / Versioning (The Best Approach)

Instead of trying to forcefully invalidate the CDN cache, simply change the filename every time the file contents change.

Because the URL is entirely different, the CDN treats it as a brand new asset and fetches it immediately, bypassing the old cached version perfectly.