Skip to main content

Content Delivery Network (CDN)

Content Delivery Network

Content Delivery Network

Image credit: Wikimedia Commons with License

A CDN, or Content Delivery Network, is a network of distributed servers located in multiple geographic locations. CDNs cache and deliver web content from the closest server to the user, reducing latency and improving site performance.

Why Use a CDN?​

Imagine you run a website hosted in India that serves millions of static content items, such as images and videos. While users in India experience low latency due to proximity to the server, users in distant locations like the USA or Australia face higher latency. This can result in slower load times and a poorer user experience.

A CDN solves this problem by caching content on edge servers located closer to users worldwide. Instead of every user accessing your content directly from the origin server in India, the CDN distributes copies to edge servers globally. When a user requests content, it is delivered from the nearest edge server, reducing latency and improving load times, no matter where the user is.

Additionally, a CDN offloads traffic from the origin server, distributing the load among multiple edge servers. This not only enhances performance but also increases scalability and reliability.

How Does a CDN Work?​

DNS Resolution​

When a user in the USA requests an image from a website (e.g., example.com) using Cloudflare's CDN, their browser initiates a DNS query to resolve example.com to an IP address.

Anycast Routing​

Cloudflare employs Anycast networking, where multiple data centers worldwide advertise the same IP address. Routing protocols ensure that the user's DNS query is directed to the nearest Cloudflare data center, based on network topology and latency.

Note

Amazon CloudFront uses DNS-based routing, ensuring that queries are directed to the nearest edge location for optimal performance.

Edge Server Handling​

The closest Cloudflare edge server receives the user's request. If the requested content is cached at this edge server, it is served directly from the cache. If not, the edge server fetches it from the origin server based on cache policies.

Origin Server Interaction​

If the content is not cached or requires an update, the edge server fetches it from the origin server. Cloudflare maintains the origin server's IP address and retrieves the content as needed.

Caching at Edge Server​

Once the content is retrieved from the origin server, the edge server caches it locally according to caching rules (e.g., Cache-Control headers). This cached content is then delivered to the user.

Content Delivery​

Subsequent requests for the same content from users in the same region are served directly from the edge server's cache, reducing latency and origin server load.

Benefits of Using a CDN​

  1. Reduced Latency: CDNs cache content closer to users, minimizing the distance data travels and speeding up load times. For example, users in the USA accessing a website hosted in India will experience faster load times due to content served from a nearby edge server.

  2. Improved Reliability: CDNs distribute traffic across multiple servers, reducing the risk of overload and ensuring high availability. For instance, during high-traffic events like Diwali or Christmas sales, a CDN can handle increased loads, preventing website crashes.

  3. Enhanced Security: CDNs offer protection against DDoS attacks and ensure secure content delivery through techniques like rate limiting, traffic distribution, Web Application Firewall (WAF), Anycast routing, and IP blocking.

Content Invalidation​

CDNs cache content for a set TTL (Time to Live). Once the TTL expires, the content is fetched again from the origin server. However, if content updates occur within the TTL, the updated content needs to be displayed immediately rather than waiting for the TTL to expire. This is where invalidation or purging is used.

CDN providers support different methods of purging cached content:

  • Purge Everything: Clears all files from the CDN cache.

  • Purge Cached Content by URL: Removes specific files from the cache using their URLs.

  • Purge Cached Content by Tag: Clears files tagged with specific cache tags.

  • Purge Cached Content by Host: Removes cached content for a particular hostname.

  • Purge Cached Content by Prefix: Clears all cached content that matches a specific URL prefix.

These methods ensure that users receive the most up-to-date content without waiting for the cache to expire.

CDN Strategies​

Push Strategy​

In a push strategy, content is proactively pushed to CDN edge servers from the origin server. This method gives you control over which content is sent, when it expires, and when updates are necessary. It is useful for controlling content distribution and ensuring availability.

Pull Strategy​

In a pull strategy, content is not pushed to edge servers. Instead, when a user requests content, the CDN fetches it from the origin server, caches it at the edge server, and serves subsequent requests from the cache. This strategy is commonly used due to its simplicity and automation of content distribution. Content can be invalidated as needed using the methods described above.

Note

Choosing between a push and pull strategy depends on the use case, content type, and desired control over content distribution and updates.