Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 34

CDN Architecture and Edge Caching Strategies

Recommended resources

Hello! Welcome to the next lesson in our journey to master system design.

In our last lesson, we delved into blob/object storage systems like Amazon S3. We established that while they are incredibly scalable and durable for storing static assets like images and videos, they are not optimized for low-latency delivery to a global audience. A user in Tokyo accessing a video stored in a US-based data center will inevitably experience delays.

This lesson directly addresses that challenge. Your learning outcome is to explain CDN architecture and edge caching strategies for global content delivery. A Content Delivery Network (CDN) is the essential layer that sits between your users and your origin storage (like S3) to make your application feel fast, no matter where your users are. Understanding CDNs is crucial for almost any system design interview involving a global user base.

We will cover:

  • The fundamental architecture of a CDN, including Points of Presence (PoPs) and Edge Servers.
  • How a user's request is intelligently routed to the nearest server using DNS and Anycast.
  • Core edge caching strategies: pull, push, and hybrid models.
  • Advanced concepts like multi-tier CDN architectures.
  • Critical cache invalidation techniques to ensure content freshness.

1. The Core Problem: Latency

Imagine your application's servers and object storage are located in a single region, for example, us-east-1 in AWS. For users nearby, performance is great. But for users in Europe, Asia, or Australia, every request for an image or video must travel across continents and oceans, leading to high latency.

A CDN solves this by creating a globally distributed network of servers that cache your content.

CDN (Content Delivery Network) Architecture
This diagram illustrates the fundamental concept of a CDN. The single 'Origin Server' holds the master copy of the content, while geographically distributed 'Edge Servers' cache copies and serve them to nearby users, drastically reducing the data's travel distance.

2. CDN Fundamentals and Architecture

At its heart, a CDN is a network of Points of Presence (PoPs) distributed around the world. Each PoP is a data center containing a cluster of caching servers known as Edge Servers.

When a user requests content, instead of going all the way to your origin server, the request is routed to the nearest PoP. If the edge server there has a cached copy of the content (a "cache hit"), it serves it directly. If not (a "cache miss"), the edge server fetches it from the origin, serves it to the user, and caches it for future requests.

Let's start with a short, high-level overview.

What Is A CDN? How Does It Work?

This clip from the ByteByteGo channel provides a quick and clear introduction to what a CDN is and its primary purpose.

Watch the first 1 minute and 13 seconds of the video. Focus on the core idea of bringing content closer to the user via Points of Presence (PoPs) and edge servers.

As the video explains, the main benefits are:

  • Reduced Latency: Faster load times and a better user experience.
  • Reduced Origin Load: The origin server handles far fewer requests, reducing its load and bandwidth costs.
  • Increased Availability & Reliability: The distributed nature of a CDN can handle traffic spikes and absorb some types of failures.
  • Enhanced Security: CDNs can provide a first line of defense against DDoS attacks.

Now, let's explore the architecture in more detail.

High-Level Architectural Components of a CDN
This diagram shows the main components in a CDN workflow: the user, DNS for routing, Edge Servers for caching, the Origin Server for the source of truth, and a Control Plane for managing the network.

A critical question in any CDN discussion is: How does the system know which edge server is "nearest" to the user?

The next video walks through a full CDN design, and we'll start with its explanation of request routing and server placement.

Design a CDN | System Design Walkthrough

The video 'Design a CDN' from the AlgoJS channel provides an excellent, in-depth walkthrough. We will use it throughout this lesson. Let's start with how requests are routed and where servers are strategically placed.

Watch from 10:33 to 14:17. Pay close attention to the two key routing mechanisms discussed: DNS-based routing: Using GeoDNS to resolve a domain to the IP address of a geographically close PoP. Anycast: A more robust networking technique where multiple servers share the same IP, and routers automatically send the request to the topologically nearest node. The video also explains the two prime locations for placing edge servers: near Internet Exchange Points (IXPs) and inside major Internet Service Provider (ISP) networks.

To summarize this crucial part:

  • GeoDNS is a good starting point, but it's not always optimal as it doesn't account for real-time network conditions or server load.
  • Anycast is a more sophisticated approach used by major CDNs. It allows for better load balancing and fault tolerance by routing traffic based on the "cheapest" network path, not just geographic distance. Your experience as a backend engineer gives you a good foundation for understanding that network topology can be more important than physical distance.

3. Edge Caching Strategies

Once a request reaches an edge server, the server needs a strategy for managing its cache. This is the heart of a CDN's effectiveness. The main decision is whether to "pull" content on-demand or have it "pushed" in advance.

Push vs. Pull CDNs

Let's return to the AlgoJS video to understand these two models and the hybrid approach that combines them.

Design a CDN | System Design Walkthrough

This section of the video clearly explains the trade-offs between Pull, Push, and Hybrid CDN models.

Watch from 01:54 to 07:43. As you watch, consider the following: Pull CDN (01:54): Why is this efficient for 'long-tail' content (content that is accessed infrequently)? What is the major drawback for the first user requesting a piece of content? Push CDN (04:41): When would you use this model? Think about a major software update or a scheduled movie release. What are the costs in terms of storage and bandwidth? Hybrid CDN (06:57): How does this model offer the best of both worlds?

Here is a summary of the trade-offs:

Strategy How it Works Pros Cons
Pull CDN Edge server fetches content from origin on a cache miss (on-demand). Storage efficient (only caches requested content), good for unpredictable traffic and long-tail content. Higher latency on first request (cache miss). Can lead to a "thundering herd" on the origin if many edges miss at once.
Push CDN Origin server proactively uploads content to all edge servers. Very low latency (content is pre-warmed). Reduces origin load during peak times. Can be wasteful (pushes content that is never requested). Higher storage costs on edges.
Hybrid Push popular/critical content; pull less popular content. Balances performance and resource utilization. More complex to manage. Requires analytics to determine what content is "popular."

Most modern CDNs for services like video streaming or social media use a hybrid model.

Multi-Tier CDN Architecture

For a very large, global CDN, having every edge server fetch from the origin on a cache miss is inefficient. This can be optimized with a multi-tier (or hierarchical) caching architecture.

Design a CDN | System Design Walkthrough

Let's continue with the AlgoJS video to see how a multi-tier architecture improves scalability and further protects the origin server.

Watch from 08:36 to 10:33. Focus on the roles of the two tiers: Child Tier: The edge servers closest to the user. Parent Tier (or Regional Edge Cache): A smaller number of more powerful servers that sit between the child tier and the origin. How does this hierarchy reduce requests to the origin?

In a multi-tier model, if a user-facing edge server (child) has a cache miss, it first requests the content from its designated regional parent cache. Only if the parent cache also has a miss does the request proceed to the origin. This consolidates many cache-miss requests from a region into a single request to the origin, significantly reducing the load.


4. Cache Invalidation: Keeping Content Fresh

A cache is only useful if the content is up-to-date. In an interview, you will almost certainly be asked, "What happens when you update a file? How do you ensure users see the new version?" This is the problem of cache invalidation.

There are several common strategies.

Plan for Cache Invalidation and Updates

For a clear, text-based explanation of invalidation strategies, let's look at the article 'System Design Masterclass: Building a Global CDN' from Design Gurus.

Read Section 4 of the article, 'Plan for Cache Invalidation and Updates'. Focus on the three primary methods described: TTL Expiry: Using Cache-Control headers. Manual Purge: Actively telling the CDN to delete content. Versioning: Changing the URL of the asset.

To complement this reading, the AlgoJS video provides a great visual explanation of how a more advanced technique, E-Tags, works in conjunction with TTL.

Design a CDN | System Design Walkthrough

Now, let's see how Time-To-Live (TTL) and E-Tags work together to provide more granular control over content freshness.

Watch from 14:17 to 17:30. Notice how E-Tags help avoid re-downloading the entire file if it hasn't changed, saving bandwidth. The origin can simply respond with a 304 Not Modified status.

In practice, a robust strategy combines these methods:

  • Use versioned URLs for static assets (main-a3f5d1.js). This is the most reliable method for CSS/JS files because deploying new code automatically points to a new URL, which results in a guaranteed cache miss.
  • Use a long TTL on these versioned assets.
  • Use a short TTL for content that changes frequently but isn't personalized (e.g., a news site's homepage).
  • Use manual purging for urgent fixes or updates (e.g., removing an incorrect product image).
  • Use E-Tags to allow clients and proxies to efficiently validate if their cached version is still fresh without re-downloading it.

Conclusion

You now have a solid framework for explaining CDN architecture in a system design interview. You can confidently discuss not just what a CDN does, but how it does it, and the critical trade-offs involved.

Key Takeaways:

  • Core Purpose: A CDN reduces latency and origin load by caching content at globally distributed Edge Servers (PoPs).
  • Request Routing: Users are directed to the nearest PoP using GeoDNS or, more robustly, Anycast networking.
  • Caching Strategies: The choice between Pull (for long-tail content), Push (for popular content), and Hybrid models depends on traffic patterns. Large-scale systems often use multi-tier caching to shield the origin.
  • Cache Invalidation: A combination of TTL, manual purging, and asset versioning is essential for maintaining content freshness while maximizing cache hits.

Next Lesson Preview:

So far, we have covered several fundamental, self-contained components: blob storage and CDNs. In the next module, we will begin integrating these building blocks to solve end-to-end system design problems. We will start with a classic interview question: designing a URL shortening service like TinyURL. This will require you to think about APIs, data modeling, and how to handle scaling from zero to millions of requests.