Fun with Learning Technology
LearnCoursesQuestionsTracksToolsNewsExplorePractice
Fun with Learning Technology

A new problem, explained clearly, every day.

Subscribe
Learn
  • Lessons
  • Topics
  • News
  • Tools
  • Courses
  • Career tracks
  • Everything
Site
  • About
  • Contact
  • Support
  • Privacy
  • Terms
Get the daily one

One email per new problem. No spam.

Request a tutorial

Requests shape what gets made next.

© 2026 Fun with Learning TechnologyRSS
Home›Courses›System Design›Content Delivery Networks (CDN)

Fundamentals

Content Delivery Networks (CDN)

A Content Delivery Network is a geographically distributed infrastructure of proxy servers designed to cache and serve content closer to end-users. It matters because it drastically reduces network latency and origin server load by eliminating the need for every request to traverse the entire internet back to your data center. You should reach for one whenever your application serves static assets, media, or heavy payloads to a global audience with performance requirements.

The Core Philosophy: Reducing Physical Distance

The fundamental physics of the internet dictates that data packets must travel across physical distance, and light in fiber optic cables has a finite speed. By positioning cache servers near regional internet exchange points, a CDN minimizes the 'round-trip time' (RTT) required for a client to receive data. When a user requests a file, they interact with a server in their own city rather than a centralized server located thousands of miles away. This effectively flattens the network topography. The reasoning here is simple: if you cannot change the speed of light, you must move the data closer to the observer. By intercepting requests at the 'edge' of the network, the CDN prevents redundant traffic from saturating the backbone, providing a seamless user experience that would otherwise be impossible due to sheer geographic scale.

# Simulate latency impact based on distance
def calculate_latency(distance_km):
    # Light in fiber is ~2/3 speed of c
    speed_of_light_fiber = 200000  # km/s
    return (distance_km / speed_of_light_fiber) * 1000  # ms

print(f"Latency to central DC (5000km): {calculate_latency(5000):.2f}ms")
print(f"Latency to edge server (50km): {calculate_latency(50):.2f}ms")

The Cache Hit Lifecycle

A CDN operates on the principle of the 'cache hit' and 'cache miss.' When a request hits a CDN node, the node first checks its local storage for the requested asset. If the asset exists and is valid, the CDN returns it immediately—this is a 'hit,' resulting in the lowest possible latency. If the asset is missing, stale, or expired, the CDN must act as a client and request the asset from your 'origin server.' This is a 'miss.' Once the origin provides the file, the CDN caches it for subsequent requests. The reasoning behind this architecture is the offloading of repetitive, heavy lifting from your application servers. By handling only the 'cache misses,' your origin server can focus its compute resources on dynamic, personalized logic while the CDN handles the heavy volume of static traffic.

class CDNNode:
    def __init__(self, origin):
        self.cache = {}
        self.origin = origin

    def get_content(self, path):
        if path in self.cache:
            print("Cache hit: Serving from memory.")
            return self.cache[path]
        # Cache miss: Fetch from origin
        data = self.origin.fetch(path)
        self.cache[path] = data
        return data

Cache Invalidation and TTL Strategies

Managing the lifecycle of cached content is the most complex challenge in CDN design. You must define 'Time-to-Live' (TTL) policies that dictate how long an asset remains fresh before the CDN considers it stale. If the TTL is too long, users will see outdated versions of your files after you perform an update. If the TTL is too short, your origin server will be hit too frequently, defeating the purpose of the CDN. To solve this, developers often use 'versioned URLs' or 'cache busting' techniques, where the filename changes (e.g., style.v123.css) whenever the content changes. This forces the CDN to treat the new file as a fresh request while allowing the old files to expire naturally. Understanding this trade-off is critical: you are balancing consistency (having the latest data) against availability and speed.

import time

class CachedAsset:
    def __init__(self, content, ttl_seconds):
        self.content = content
        self.expiry = time.time() + ttl_seconds

    def is_fresh(self):
        return time.time() < self.expiry

# Usage: Content expires after 60 seconds
asset = CachedAsset("<html>...</html>", 60)
print(f"Is asset fresh? {asset.is_fresh()}")

Security at the Edge: DDoS Mitigation

Beyond performance, CDNs provide a massive security advantage by acting as a distributed shield between the public internet and your private infrastructure. Because your CDN provider manages thousands of globally distributed servers, they possess enough total bandwidth and processing capacity to absorb massive Distributed Denial of Service (DDoS) attacks that would instantly overwhelm a single origin server. When an attack occurs, the malicious traffic is partitioned across the entire global network of the CDN. Rather than impacting your origin, the attack traffic is filtered, throttled, or simply dissipated at the edge. This provides an inherent layer of protection that allows your application to remain operational even while under heavy assault, effectively hiding your origin server's true IP address from potential attackers.

# A simple traffic shaper to mimic edge filtering
class TrafficShield:
    def __init__(self, threshold):
        self.threshold = threshold
        self.request_count = 0

    def handle_request(self, ip_address):
        self.request_count += 1
        if self.request_count > self.threshold:
            return "Blocked: Too many requests"
        return "Allowed: Passing to origin"

# Shield protecting against volume
shield = TrafficShield(100)

Global Load Balancing and Anycast

To connect the user to the 'best' edge node, CDNs utilize a networking technique called Anycast. With Anycast, the same IP address is advertised from multiple geographic locations simultaneously using the Border Gateway Protocol (BGP). When a user sends a request, the internet's routing infrastructure naturally directs that packet to the 'closest' node based on network topology metrics. This is superior to traditional DNS-based routing because it happens at the routing layer rather than the application layer, ensuring the user is routed to a healthy, nearby server without a manual lookup. This automated distribution ensures high availability; if one data center goes offline, the internet's routing tables automatically shift traffic to the next nearest location, making the system resilient to regional outages without requiring manual intervention from your team.

# Mocking a route selection process based on network distance
routers = {"us-east": 10, "us-west": 50, "eu-central": 120}

def route_to_best_node(user_location):
    # Anycast naturally picks the path with shortest BGP distance
    best_node = min(routers, key=routers.get)
    return f"Routing packet to {best_node}"

print(route_to_best_node("global"))

Key points

  • CDNs improve performance by physically reducing the distance data travels to reach the end-user.
  • A cache hit occurs when a requested asset is already stored at the edge, avoiding a call to the origin.
  • The origin server only processes requests when the CDN experiences a cache miss.
  • Setting an appropriate Time-to-Live is a balance between data consistency and server load.
  • Versioned filenames are an effective strategy for managing cache invalidation in production environments.
  • Distributing traffic across a global network acts as a buffer against large-scale DDoS attacks.
  • Anycast routing automatically directs users to the nearest healthy edge node using standard network protocols.
  • CDNs offload significant compute and bandwidth burdens from the origin server, improving overall application scalability.

Common mistakes

  • Mistake: Thinking a CDN is a replacement for a database. Why it's wrong: A CDN caches static content, not dynamic database query results or transactional state. Fix: Use a CDN for static assets like images/CSS/JS and a caching layer like Redis for database queries.
  • Mistake: Assuming a CDN increases security for all application layers. Why it's wrong: While CDNs provide DDoS protection, they do not secure your backend logic or internal database vulnerabilities. Fix: Implement application-level security and firewalls behind the CDN.
  • Mistake: Caching highly personalized content. Why it's wrong: Caching user-specific data (like profiles or shopping carts) at the edge leads to sensitive data leakage between users. Fix: Use vary headers or fetch personalized data directly from the origin server.
  • Mistake: Neglecting the TTL (Time-To-Live) settings. Why it's wrong: Setting TTL too high prevents content updates from propagating, while too low causes excessive origin traffic. Fix: Use versioned file names (e.g., style.v2.css) for long TTLs and purge mechanisms for updates.
  • Mistake: Believing all CDNs handle dynamic content equally well. Why it's wrong: Standard CDNs are for static files; dynamic content requires 'Dynamic Site Acceleration' which involves path optimization. Fix: Choose a CDN provider that supports TCP optimization and persistent connections for dynamic traffic.

Interview questions

What is the fundamental purpose of a Content Delivery Network (CDN) in a large-scale system?

A Content Delivery Network is a geographically distributed group of servers that work together to provide fast delivery of internet content. Its primary purpose is to reduce latency by bringing content closer to where users are located. By caching static assets like images, videos, and JavaScript files at the 'edge' of the network, the CDN minimizes the physical distance data must travel between the user and the origin server. This improves page load times, reduces bandwidth costs for the origin, and provides protection against traffic spikes by absorbing requests that would otherwise overwhelm the core infrastructure.

Explain the difference between a 'Push' CDN and a 'Pull' CDN.

In a 'Push' CDN, the origin server is responsible for actively pushing content to the CDN edge servers. This is ideal for sites with high-traffic static content where you want to ensure the data is already available globally before a request is made. Conversely, a 'Pull' CDN works on a request-first basis. The CDN server checks its cache; if the content is missing, it 'pulls' the content from the origin server, caches it for future requests, and serves it. A Pull CDN is easier to configure and maintain because it automatically fetches the latest updates from the origin, although the very first request for a resource may be slightly slower.

How does a CDN determine which server should serve a specific user request?

CDNs use sophisticated routing mechanisms to direct user traffic to the most optimal edge server, typically based on proximity, server load, and network health. This is usually achieved through DNS-based load balancing or Anycast. In DNS-based routing, the CDN's authoritative DNS server resolves the domain name to a specific IP address based on the user's geographical location identified via IP geolocation. With Anycast, multiple servers share the same IP address, and the BGP routing protocol naturally routes the user to the 'closest' server based on the number of network 'hops', ensuring efficient traffic distribution.

Compare the strategies of 'Time-to-Live' (TTL) and 'Cache Invalidation' for managing content freshness.

TTL is a proactive strategy where you define a duration (e.g., 3600 seconds) for how long a file should stay in the cache before the CDN re-validates it with the origin. It is simple but can lead to stale content if updates occur frequently. Cache Invalidation is reactive; when an update occurs at the origin, the system sends an API request to the CDN to purge or 'revalidate' specific assets immediately. While invalidation is more complex to implement via purging APIs, it ensures consistency for critical assets, whereas TTL is better suited for long-lived, less volatile files like CSS or image libraries.

What challenges arise when using a CDN for dynamic content, and how can they be mitigated?

Dynamic content is personalized per user and cannot be easily cached, making it difficult to leverage a CDN. A common challenge is that requests must travel back to the origin, nullifying the latency benefits. To mitigate this, developers use 'Dynamic Site Acceleration' (DSA). DSA employs persistent connections (keep-alive) between the CDN and the origin to reduce handshake overhead, optimizes TCP window scaling to speed up transmission, and uses intelligent routing to find faster paths across the public internet. Furthermore, techniques like Edge Side Includes (ESI) allow fragments of a page to be dynamic while the structure remains cached, optimizing the overall response time.

How would you design a system to ensure cache consistency between multiple CDN edge nodes and a central origin?

Maintaining consistency requires a strategy that balances strictness with performance. One approach is using a 'versioned' file system where assets are renamed whenever they change (e.g., style.v1.css to style.v2.css). This forces the CDN to treat the new file as a cache miss, ensuring users never see stale content. Alternatively, for critical data, implement a 'Write-through' cache pattern where an invalidation signal is broadcast to all edge nodes simultaneously via a globally distributed message bus. In pseudocode: if (origin_update) { broadcast(purge_request, edge_nodes); update_database(); }. This ensures the system remains eventually consistent without sacrificing the low-latency performance benefits of the edge cache.

All System Design interview questions →

Check yourself

1. When designing a system that uses a CDN, why is it critical to use versioned filenames (e.g., app.v1.js) for static assets?

  • A.To ensure the CDN can compress the files better
  • B.To bypass the browser's cache for security reasons
  • C.To solve the issue of cache invalidation when content updates
  • D.To reduce the load on the CDN's control plane
Show answer

C. To solve the issue of cache invalidation when content updates
Versioned filenames allow you to set very long TTLs (improving speed) because the file name changes when the content changes, forcing the CDN to fetch the new version immediately. Option 0 and 3 are incorrect as naming doesn't affect compression or control plane load, and option 1 is incorrect because it defeats the caching performance benefits.

2. A system architect decides to use a CDN to cache API responses. Which scenario would make this a poor design choice?

  • A.The API returns the same public JSON data for every user
  • B.The API response changes based on the user's authentication token
  • C.The API is used to serve public images
  • D.The API is rate-limited by the CDN's edge nodes
Show answer

B. The API response changes based on the user's authentication token
Caching user-specific (authenticated) content at the edge is dangerous and usually ineffective, as it risks showing one user's private data to another. Option 0 is a perfect use case for CDN caching, option 2 is a static asset use case, and option 3 is a common CDN feature.

3. How does a CDN improve the performance of a distributed system?

  • A.By executing application logic closer to the user
  • B.By reducing the physical distance between the user and the requested data
  • C.By replacing the need for load balancers at the origin
  • D.By encrypting all database traffic automatically
Show answer

B. By reducing the physical distance between the user and the requested data
CDNs cache data on Edge Servers geographically closer to the user, reducing latency (round-trip time). Option 0 refers to Edge Computing, not standard CDN caching; option 2 is false as CDNs sit in front of load balancers; option 3 is false as CDNs handle external, not internal, database traffic.

4. Which of the following describes the 'Origin Pull' method of populating a CDN cache?

  • A.The origin server pushes files to the CDN proactively
  • B.The CDN fetches content from the origin when it receives a request for a file not in its cache
  • C.Users upload files directly to the CDN instead of the origin server
  • D.The CDN performs periodic backups of the origin database
Show answer

B. The CDN fetches content from the origin when it receives a request for a file not in its cache
Origin Pull is the standard 'lazy loading' method where the CDN retrieves content only when a user requests it for the first time. Option 0 describes 'Origin Push', which is less common for general traffic; option 2 is incorrect; option 3 is false because CDNs are for serving static data, not database backups.

5. What is the primary benefit of using a CDN to mitigate a DDoS attack?

  • A.The CDN can eliminate all malicious traffic at the source
  • B.The CDN distributes the incoming attack traffic across a global network of servers
  • C.The CDN automatically patches the vulnerability being exploited in the attack
  • D.The CDN prevents the origin server from needing an IP address
Show answer

B. The CDN distributes the incoming attack traffic across a global network of servers
CDNs have huge bandwidth and distributed edges that can absorb massive amounts of traffic, preventing the origin server from being overwhelmed. Option 0 is unrealistic as some malicious traffic bypasses filters; option 2 is false as patches are an application duty; option 3 is technically impossible as servers require IP addresses to function.

Take the full System Design quiz →

← PreviousCaching Strategies — CDN, Redis, MemcachedNext →SQL vs NoSQL — When to Use Which

System Design

31 lessons, free to read.

All lessons →

Track your progress

Sign in to mark lessons done, score quizzes and keep notes.

Open in the app