Skip to Content
Go Realm v1 is released ๐ŸŽ‰
System DesignContent Delivery Network

๐Ÿ“ฆ Content Delivery Network (CDN)

๐Ÿ”น What is a CDN?

A Content Delivery Network (CDN) is a globally distributed network of servers that caches and delivers static and sometimes dynamic content (like images, videos, CSS, JS, API responses) from servers that are closer to the user instead of always fetching from the main/origin server.

๐Ÿ‘‰ Think of it like branch libraries spread across the world, each holding copies of popular books so readers donโ€™t always have to travel to the central library.


๐Ÿ”น Why Use a CDN?

  • โšก Faster load times โ†’ Users get content from the nearest edge server.

  • ๐ŸŒ Global availability โ†’ Better experience for users far from your origin server.

  • ๐Ÿ“‰ Reduced origin load โ†’ Fewer requests hit your main server.

  • ๐Ÿ”’ Improved security โ†’ Many CDNs provide DDoS protection, SSL, and firewall features.

  • ๐Ÿ’พ Scalability โ†’ Handles large spikes in traffic more easily.


๐Ÿ”น How Does a CDN Work?

  1. User requests a file (e.g., logo.png).

  2. The request is routed via DNS + Anycast to the nearest CDN edge server.

  3. Edge server checks its cache:

    • โœ… Cached โ†’ Return immediately.
    • โŒ Not cached โ†’ Fetch from origin server, cache it, then serve.
  4. Future requests from nearby users get the cached version until expiry โ†’ Controlled by Cache-Control headers.


๐Ÿ”น CDN for APIs (Dynamic Content)

  • Public API responses (e.g., /api/products) โ†’ Cache for short duration.

  • Private/personalized responses (e.g., /api/user/123/orders) โ†’ Bypass cache.


๐Ÿ”น Disadvantages of CDN

  • ๐Ÿ’ฐ Cost โ†’ Can get expensive with high traffic or advanced features.

  • โš™๏ธ Configuration complexity โ†’ Caching rules must be carefully set (risk of stale data).

  • ๐Ÿž Debugging harder โ†’ Issues may happen at CDN layer instead of origin.

  • ๐Ÿšซ Not always useful โ†’ For small/local apps with no global users, CDN adds little value.

  • ๐Ÿ“ฆ Limited caching for dynamic content โ†’ User-specific data often canโ€™t be cached.


๐Ÿ”น FAQs

โ“ Are CDN edges like read replicas of the origin?

  • โŒ No โ†’ Edge servers are not database slaves/replicas.
  • โœ… Yes โ†’ They are smart caches that only store requested content temporarily.
  • They fetch missing content from the origin on demand (Pull CDN).
  • Some CDNs allow pre-uploading (Push CDN).

โ“ How does a request get served from the nearest CDN server?

  • Done via DNS + Anycast routing.
  • โœ… No need to implement this in your code.
  • The CDN automatically routes users to the nearest edge server.

โ“ Can dynamic API responses be cached?

  • โœ… Yes, for public/frequently used endpoints (short TTL).
  • โŒ No, for user-specific/private endpoints.
  • Controlled via Cache-Control headers in your API responses.

  • Cloudflare (free tier available)
  • AWS CloudFront
  • Akamai
  • Fastly

Further reading: CDNย