Networking for backend engineers: Why CDN's improve performance

Explore the 5 characteristics of CDN's that enable them to improve website performance.

Networking for backend engineers: Why CDN's improve performance
Related networking articles:
* Networking for backend engineers: OSI Model
* Networking for backend engineers: TCP

If you're like me and have always heard that CDN's improve performance, but never understood why then this post is for you.

In this post, we'll dive into the key characteristics of CDN's that enable them to increase performance and get a better understanding of how they work under the hood.

We'll touch on five aspects of CDNs:

  • Request Flow
  • Geographic Placement
  • CDN Architecture
  • Request Routing
  • TCP Optimizations

What is a CDN?

Before we dive into the characteristics of CDN's, let's first give a brief description of what a CDN is and what it's used for.

CDN stands for Content Distribution Network, it is an overlay network - a network built on top of the internet network, designed to increase response times. Typically a CDN is used to deliver static content such as images, videos, HTML, CSS - basically anything that you can reliably cache and offload from your backend servers.

There are a number of popular CDNs such as Cloudflare and Amazon CloudFront which are easy to integrate into your website to increase performance.

So now that we understand what a CDN is and why you'd use one. Let's take a step further and understand why they increase performance.

CDN request flow

First, we'll take a look at how CDN's fit in with the HTTP request flow. When a client (browser, mobile app, etc) requests a static asset like an image, it connects to the CDN and tries to retrieve the resource.

CDN network request flow showing cache hits and misses

If the CDN has the resource, it simply returns the resource from its cache. This increases performance as the client does not have to go to the origin server (your web server) to fetch the resource and it also decreases the load on your server.

If however, the CDN does not have the resource, it makes the request on behalf of the client and fetches the resource from your server, stores it in cache for future requests, and returns the resource to the client.

For frequently accessed resources there should be very few cache misses as most of the resources would eventually be cached by the CDN. In certain cases you can even pre-warm the CDN by pre-emptively pushing resources to it that you expect clients to request, thereby avoiding the initial cache misses. This technique can also be used to invalidate resources that are expired and need to be replaced with new versions of the resource.

CDN geographic placement

So clients fetch the resource from the CDN instead of the origin server, why would that improve performance? Because of where the CDNs are situated.

CDN geographic placement showing network latency difference between CDN and Origin

CDN's have numerous Points of Presence (PoPs) - each PoP consisting of multiple caching servers, that are spread out over a wide geographic area. The aim is that every client should be near a PoP, thereby bringing the content closer to the client and increasing performance.

Depending on the distance between the client and your origin server the performance increase can be dramatic, with the additional benefit of reducing the load on your server.

CDN architecture

CDN's implement a tiered architecture to achieve the best possible caching performance.

CDN architecture showing tiered caching layers

The caching servers closest to the clients represent a Edge cache, these servers are the most prevalent but hold a smaller subset of the content. To avoid cache misses going to the origin server, CDNs use Intermediate caching servers that store more content, but there are fewer of them.

This improves the performance of the caching as a cache miss on the Edge cache doesn't result in calling the origin server all the time, and because there are fewer Intermediate caches the CDN doesn't have to replicate the content to as many servers, significantly saving bandwidth costs.

CDN routing

Is there a difference between a client fetching a resource from your origin server vs. the CDN fetching that same resource? Yes. We'll speak about two major differences, the first one being how network requests are routed on the internet.

CDN routing showing optimized routing protocols used by CDNs

This is best explained with a diagram so let's analyze the diagram above.

The internet uses a routing protocol called Border Gateway Protocol (BGP) which determines the optimal path for a request based on the total number of hops along the path. It chooses the path that results in the least number of hops, the path in blue on the diagram.

While this path has fewer hops it might have a higher latency than another path with more hops. This is where CDNs shine. They determine the optimal path based on the estimated latency of the path, which they monitor through certain metrics like congestion. This results in the CDN choosing the green path in the diagram, a more optimal path, which means that it can fetch the resource faster.

CDN TCP optimizations

The second reason CDNs can perform the network request faster than if it was initiated by a client is the TCP optimizations they employ.

CDN TCP optimizations showing pool of pre-warmed TCP connections

CDNs will keep a pool of TCP connections open, thereby avoiding the performance overhead of establishing a TCP connection (learn more about this overhead here).

Additionally, they are able to optimize the TCP congestion window sizes used and thereby increase the bandwidth of the request, fetching the resource even faster.

Summarizing CDN characteristics

CDN's increase the performance of your website and are therefore a good choice to consider in your web architecture. I hope these points that we have discussed have given you a better understanding of why they actually increase your websites' performance.

If you've enjoyed reading this, reach out to me on Twitter @javaad_patel with your thoughts or subscribe and read along as I go down this networking journey.

Javaad Patel

FullStack Developer

I'm passionate about building great SaaS platform experiences. Currently learning and writing about cloud architectures, distributed systems and devOps.