AlgoDesign

CDN

CDN introduction

A Content Delivery Network (CDN) is a distributed network of servers strategically located across various geographical locations to deliver web content, such as images, videos, and other static assets, more efficiently to users. The primary purpose of a CDN is to reduce latency and improve the overall performance of web applications by serving content from the server nearest to the user. CDNs can also help improve reliability, availability, and security of web applications.

How do CDNs work?

When a user requests content from a web application, the request is routed to the nearest CDN server (also known as an edge server) based on factors such as network latency and server load. The edge server then checks if the requested content is already cached. If it is, the content is served directly from the cache; otherwise, the edge server fetches the content from the origin server, caches it, and serves it to the user. Subsequent requests for the same content can then be served from the cache, reducing latency and offloading traffic from the origin server.

image

CDN terminology

  • Point of Presence (PoP)

    • A PoP is a physical location where CDN servers are deployed, typically in data centers distributed across various geographical locations. PoPs are strategically placed close to end-users to minimize latency and improve content delivery performance.
  • Edge Server (proxy server)

    • An edge server is a CDN server located at a PoP, responsible for caching and delivering content to end-users. These servers store cached copies of the content, reducing the need to fetch data from the origin server.
  • Origin Server

    • The origin server is the primary server where the original content is stored. CDNs fetch content from the origin server and cache it on edge servers for faster delivery to end-users.
  • Cache Warming

    • Cache warming is the process of preloading content into the edge server's cache before it is requested by users, ensuring that the content is available for fast delivery when it is needed.
  • Time to Live (TTL)

    • TTL is a value that determines how long a piece of content should be stored in the cache before it is considered stale and needs to be refreshed from the origin server.
  • Anycast

    • Anycast is a network routing technique used by CDNs to direct user requests to the nearest available edge server, based on the lowest latency or the shortest network path.
  • Content Invalidation

    • Content invalidation is the process of removing or updating cached content when the original content on the origin server changes, ensuring that end-users receive the most up-to-date version of the content.
  • Cache Purging

    • Cache purging is the process of forcibly removing content from the edge server's cache, usually triggered manually or automatically when specific conditions are met.

CDN advantages

CDNs play a crucial role in enhancing the performance, reliability, and security of modern web applications. By serving content from geographically distributed edge servers, CDNs can provide users with a fast and seamless experience, while reducing load on origin servers and protecting against security threats. Here are the top benefits of using CDNs:

  • Reduced latency

    • By serving content from geographically distributed edge servers, CDNs reduce the time it takes for content to travel from the server to the user, resulting in faster page load times and improved user experience.
  • Improved performance

    • CDNs can offload static content delivery from the origin server, freeing up resources for dynamic content generation and reducing server load. This can lead to improved overall performance for web applications.
  • Enhanced reliability and availability

    • With multiple edge servers in different locations, CDNs can provide built-in redundancy and fault tolerance. If one server becomes unavailable, requests can be automatically rerouted to another server, ensuring continuous content delivery.
  • Scalability

    • CDNs can handle sudden traffic spikes and large volumes of concurrent requests, making it easier to scale web applications to handle growing traffic demands.
  • Security

    • Many CDNs offer additional security features, such as DDoS protection, Web Application Firewalls (WAF), and SSL/TLS termination at the edge, helping to safeguard web applications from various security threats.

Origin server vs edge server (proxy servers)

Origin servers and edge servers are key components in the delivery of web content, especially in content delivery networks (CDNs). They serve different roles in ensuring that users access web content quickly and efficiently. Understanding the distinction between the two is crucial in the context of web architecture and CDN functionality.

Origin server

The origin server is the primary source of the original, unmodified content. It hosts the website's content, including HTML files, images, stylesheets, JavaScript, videos, and other digital assets. When a user requests a web resource (like a webpage), the request is directed to the origin server if the content is not cached elsewhere.

Characteristics

  • Centralized Content Storage
    • It is the central repository where all the website's original content is stored and managed.
  • Content Source
    • It provides the original content to edge servers or directly to users if the content is not cached or when a CDN is not used.
  • Performance Limitations
    • Directly serving all user requests, especially for sites with a global audience, can lead to slower response times due to geographical distance and increased load.

Edge server

Edge servers, on the other hand, are part of a distributed network of servers, typically provided by a CDN. They cache content from the origin server and deliver it to users from a location closer to the user, thereby reducing latency and improving load times.

Characteristics

  • Geographical Distribution
    • Located in various locations (edge locations) closer to the end-users to reduce the distance that data travels over the network.
  • Content Caching
    • Stores cached copies of content from the origin server. This caching mechanism allows for faster delivery of content to users and reduces the load on the origin server.
  • Load Balancing and Scalability
    • Helps in distributing user requests efficiently, handling traffic spikes, and improving the scalability of web content delivery.

Example

Imagine a user from Paris attempting to access a video hosted on a website whose origin server is located in New York. If the website uses a CDN, the user's request might be routed to an edge server in Paris. If the requested video is cached on the Paris edge server, it is delivered quickly to the user from there, significantly reducing the time it would take to fetch the video directly from the origin server in New York.

Summary

While the origin server is the source of the original content, edge servers play a crucial role in optimizing content delivery to end-users by caching content closer to where users are located. This architecture significantly improves website performance, reduces latency, and enhances user experience, especially for websites with a global audience.

CDN architecture

POPs (Points of presence and edge / proxy servers)

A Point of Presence (PoP) is a physical location containing a group of edge servers within the CDN's distributed network. PoPs are strategically situated across various geographical regions to minimize the latency experienced by users when requesting content. Each PoP typically consists of multiple edge servers to provide redundancy, fault tolerance, and load balancing.

Edge servers are the servers within a PoP that store cached content and serve it to users. When a user makes a request for content, the request is directed to the nearest edge server, which either serves the content from its cache or fetches it from the origin server and then caches it for future requests. By serving content from the geographically nearest server, CDNs can significantly reduce latency and improve the overall user experience.

CDN routing and request handling

CDN routing is the process of directing user requests to the most suitable edge server. Routing decisions are typically based on factors such as network latency, server load, and the user's geographical location. Various techniques can be employed to determine the optimal edge server for handling a request, including:

  • Anycast Routing

    • In anycast routing, multiple edge servers share a single IP address. When a user sends a request to that IP address, the network's routing system directs the request to the nearest edge server based on network latency or the number of hops. This approach helps ensure that requests are automatically routed to the most appropriate server.
  • DNS-based Routing

    • With DNS-based routing, when a user requests content, the CDN's DNS server responds with the IP address of the most suitable edge server. This approach can take into account factors such as geographical proximity and server load to select the best edge server for handling the request.
  • GeoIP-based Routing

    • In this approach, the user's geographical location is determined based on their IP address. The request is then directed to the nearest edge server in terms of geographical distance, which often corresponds to lower network latency.

Caching mechanisms

Caching is a crucial component of CDN architecture. Edge servers cache content to reduce latency and offload traffic from the origin server. Various caching mechanisms can be employed to determine what content is stored, when it is updated, and when it should be removed from the cache. Some common caching mechanisms include:

  • Time-to-Live (TTL)

    • TTL is a value set by the origin server that determines how long a piece of content should be stored in the cache before it is considered stale and needs to be fetched again from the origin server.
  • Cache Invalidation

    • Cache invalidation is the process of removing content from the cache before its TTL expires. This is typically done when content is updated or deleted on the origin server and needs to be reflected immediately in the CDN.
  • Cache Control Headers

    • Cache control headers are used by the origin server to provide instructions to the CDN regarding caching behavior. These headers can dictate the cacheability of content, its TTL, and other caching-related settings.

CDN network topologies

CDN network topologies describe the structure and organization of the CDN's distributed network. Different topologies can be employed to optimize content delivery based on factors such as performance, reliability, and cost. Some common CDN network topologies include:

  • Flat Topology

    • In a flat topology, all edge servers in the CDN are directly connected to the origin server. This approach can be effective for smaller CDNs, but may not scale well as the network grows.
  • Hierarchical Topology

    • In a hierarchical topology, edge servers are organized into multiple tiers, with each tier being responsible for serving content to the tier below it. This approach can improve scalability by distributing the load among multiple levels of servers and reducing the number of direct connections to the origin server.
  • Mesh Topology

    • In a mesh topology, edge servers are interconnected, allowing them to share content and load with each other. This approach can enhance the redundancy and fault tolerance of the CDN, as well as improve content delivery performance by reducing the need to fetch content from the origin server.
  • Hybrid Topology

    • A hybrid topology combines elements from various topologies to create an optimized CDN architecture tailored to specific needs. For example, a CDN could use a hierarchical structure for serving static content, while employing a mesh topology for dynamic content delivery.

CDN architecture summary

CDN architecture involves the strategic placement of PoPs and edge servers, efficient routing and request handling mechanisms, effective caching strategies, and the appropriate selection of network topologies to optimize content delivery.

Push CDN vs pull CDN

Pull CDN

In a Pull CDN, the content is not stored on the CDN's servers by default. Instead, the CDN "pulls" the content from the origin server when a user requests it for the first time. Once the content is cached on the CDN's edge server, subsequent requests for the same content will be served directly from the CDN, reducing the load on the origin server.

When the cached content expires or reaches its Time-to-Live (TTL), the CDN will fetch the content again from the origin server, ensuring that users receive up-to-date content.

Examples of Pull CDNs include Cloudflare, Fastly, and Amazon CloudFront.

Advantages of Pull CDN

  • Easy to set up and requires minimal changes to the existing infrastructure.
  • The origin server is only accessed when content is not available on the CDN, reducing the load and bandwidth usage.
  • The CDN automatically handles cache management and content expiration.

Disadvantages of Pull CDN

  • The first user to request the content may experience slightly slower load times as the CDN fetches the content from the origin server.
  • The origin server must be accessible at all times for the CDN to fetch the content when needed.

Push CDN

In a Push CDN, the content is "pushed" to the CDN's servers by the content provider, usually through manual uploads or automated processes. The content is proactively distributed across the CDN's edge servers, making it readily available for user requests.

Push CDNs are typically used for large files or less frequently accessed content, as they allow for better control over content distribution and caching.

Examples of Push CDNs include Rackspace Cloud Files and Akamai NetStorage.

Advantages of Push CDN

  • Better control over content distribution and cache management, especially for large or infrequently accessed files.
  • Content is readily available on the CDN's servers, ensuring consistent load times for users.

Disadvantages of Push CDN

  • More complex to set up and maintain, as content must be manually uploaded or synced to the CDN.
  • Increased storage costs, as content is stored on both the origin server and the CDN's servers.
  • The responsibility of cache management and content expiration lies with the content provider.

In short, Pull CDNs are best suited for frequently accessed content and are easier to set up, while Push CDNs offer more control and are ideal for large or infrequently accessed files.