-- online Search!

What is a CDN?

by kirupa   |   filed under Web Apps

A Content Delivery Network (CDN) can greatly speed up the communication between your site and your visitors. Learn how they do that...and more!

It is dinner time, and you have a strong craving for pizza. Like many people, you mentally debate for a bit on whether you ought to eat something healthier. Can’t go wrong with a salad, right? Well...

If you are like me, you dismiss the desire for a healthy salad very quickly and start imagining eating the biggest pizza with the most number of toppings that could possibly fit without defying the laws of physics. In the end, you place your order for a pizza to be delivered as quickly as possible.

Let’s imagine that you live in an area that looks a bit as follows:

In ordering your pizza, there are two scenarios we want to discuss. One scenario involves ordering from a mom-and-pop shop that has a single location. The other scenario involves ordering from a chain that has multiple locations.

Ordering from the Single Mom-and-Pop Shop

There is one particular place you like to order from. They make the pizza just the way you like it. You know the owners. They pretend to know you. Being a small shop, they just have a single location:

When you have just a single location, that one location will be responsible for delivering the pizza to you. As these things go, the closer you are to this location, the faster your delivery will be:

The further away you (or other customers) are, the longer the delivery will take from this single location:

If you live really far away but really REALLY want pizza from this location, there is no other option. You are going to have to wait while they deliver all the way to you:

With greater distance, there is also the risk of other factors that will affect how quickly your pizza gets delivered. There is traffic, construction slowdowns, ducks (or other animals with no sense of urgency) crossing a street, inclement weather, accidents, everyone slowing down to watch the aftermath of an accident, and a variety of other things that could happen along the way that will make your pizza less warm and less fresh when it ultimately gets delivered.

Ordering from a Mega Pizza Chain

Instead of ordering from the single mom-and-pop shop, you realize you have a sweet coupon/deal from a mega pizza chain that you noticed while going through a stack of junk mail. Obviously, this deal is set to expire...tomorrow! Ordering from this chain makes the most financial sense. We can’t let the deal expire and go to waste! That would be unacceptable.

This chain has multiple locations in your area:

The advantage of multiple locations is that you can reasonably expect that the store closest to you will be the one who will make your pizza and deliver it:

Because there are multiple locations spread across your area, there won’t be any really long trips. This means locations like the office complex or stadium will get almost as speedy a service as you were able to without worrying about traffic and other related boondoggles we called out earlier:

The other advantage of multiple locations is you are less likely to be impacted by any unforeseen circumstances. A flood of orders suddenly came in to the closest store? No worries. You have other locations that can help balance the extra demand. If a power outage takes some locations offline, our friendly mega pizza chain can optimize delivery from a location that (while possibly further away) is still able to make the delivery:

Yes, they may even creatively decide to deliver your pizza using some unconventional ways like via the water. Anything is possible.

From Pizza to a Content Delivery Network (CDN)

With our pizza scenarios in the previous section, there are a few things we can generalize on:

  • One pizza location wasn’t great from a delivery point of view - especially if you lived further away.
  • By having multiple locations, the mega pizza chain was better able to rely on the closest location to get a pizza to you quickly.
  • Unforeseen problems are reduced with multiple locations. If there was a sudden surge in orders, each of the locations could better balance the workload instead of overwhelming one location. If some of the locations went offline, other locations can still be available.

Now, the focus of this article isn’t about pizzas. It is just that looking at the various things that go into pizza delivery has a lot of overlap with how our web content makes its way around the world.

Page Load Time and Distance

Let’s say that we have a web site that people from around the world tend to visit. Our content lives in a special computer known as a web server whose sole job it is to send our web site’s contents across the internet to any device that requests it. A web server must have a physical location, and our server is located in the western side of the United States. Placed against the vast expanse of our world, it would look a bit like this:

Now, some visitor on the eastern side of the United States wants to visit our web site:

The way data from our web server makes its way to this visitor’s device is a bit similar to how our earlier pizza delivery vehicle drives through town to make a delivery. There are a series of stops and turns involved. In the case of our pizza delivery vehicle, this is just your usual roads and traffic lights and intersections and so on. For the communication between our web server and the visitor, these stops and turns (referred as network hops) are the various network exchanges (think of many really big routers) placed in various geographic locations that are responsible for sending data around. If we had to visualize these hops, it could sorta look a bit like this:


Now, here is an all too familiar problem. Moving data takes time. A longer physical route adds travel time, and routers can add processing and queueing delays along the way. Geographic distance does not tell us how many hops a route has, though, and counting hops does not tell us how long the request will take. A short, congested route can be slower than a longer route with better connections.

Because our web site is interesting to a worldwide audience, some requests will travel across oceans and continents. That can add a lot of travel time even without a large number of hops:


A visitor near our web server may have a shorter route. That gives us a chance to reduce travel time, although the actual routing and network conditions still matter:


Now, while we have been focusing on distance our data needs to travel as a major contributor to how long it will take for something to load, there are other factors at play as well. The quality of the networks our data will travel through will vary. There may be network congestion in some part of the world that slows things down. An underwater cable that is responsible for carrying a lot of internet data might temporarily go offline from...sabotage by these:

But wait, there is more! The local connection to our visitor, often called the last mile, might involve a slow cell phone connection or (gulp) a dial-up modem. That name does not promise exactly one router hop. Connection setup, available bandwidth, and work on our server also take time. After the bytes arrive, the browser still has to process and render them. Distance is one part of page-load time, so we need measurements to find out which part is actually slowing our visitors down.

Time is Visitors and Engagement and Money

Why does all of this matter? Is this really a problem? Well...It isn’t as big a problem as what the world faces with Godzilla (🦖) destroying some coastal city every few years, but there are a bunch of studies on top of studies that show that web sites that load content quickly tend to attract more visitors who stay more engaged. More visitors and more engaged visitors are all good things. We want to ensure that these good things don’t just apply to visitors who happen to be living close to where our web servers are located. We want all of our visitors worldwide to enjoy fast and snappy web site load times:


How can we actually do that? The answer to this question is where this mysterious creature known as a CDN comes in.

CDNs to the Rescue

The problem right now is that our web server, located in one part of the world, is responsible for serving visitors from all parts of the world. What if we had a way to replicate our web server to more parts of the world so more visitors are physically closer to it? What if we could do something like this?


In this approach, we would try to serve visitors from a location with a good network path to them. That is often nearby, but routing, availability, and load can make a different location a better choice. There is one wrinkle here. Replicating our whole web server has some technical issues. What if we could keep our original web server and make copies of suitable content available closer to our visitors?

This is where a CDN comes in. A CDN, short for Content Delivery Network, acts as an intermediary between our web server (often referred to as an origin server) and our visitors spread out across the world. This arrangement can loosely be visualized as follows:


Notice the (lack of) interaction between our visitors all over the world and our web server located in the western United States. The only common interaction for all parties is the server that is part of the CDN, often referred to as an edge server and represented by a gear icon in the above image. This is an important detail that is core to how CDNs work to speed things up, so let’s spend a few more moments on this.

If We Didn’t Use a CDN

Without a CDN, our visitor first resolves the hostname and then requests the home page from our web server. The following diagram combines those steps into a simplified overview. DNS supplies address information for the hostname; it does not receive the page's full URL or forward the HTTP request:


Our domain (foo.com) would point directly to our web server, and our web server will be responsible for sending any requested resources to the visitor’s device. If the distance between our visitor and our web server is really large, the whole trip around these steps may take a long time.

If We Used a CDN

With a CDN, the request goes to an edge server instead. For this example, assume we have explicitly allowed our public index.html page to be cached and the edge already has a reusable copy:

The edge server can return that cached copy without fetching the page from our origin again. That can save a long trip! It does not automatically cache every file, though. Cache rules and response headers determine what may be stored and reused. For example, Cloudflare does not cache HTML or JSON by default. An HTML filename also tells us nothing about whether the response contains private information.

If index.html is not already cached, our edge server will first retrieve it from our web server and then pass it off to the visitor’s device (as seen by the new Step 3):


Retrieving this file from our web server takes additional time. Later requests can reuse the cached response while the cache policy permits it. That origin trip is not a one-time cost forever: copies can expire, be evicted, or be invalidated, and another edge may not have the file yet. A stale copy may also need revalidation with the origin before it can be reused.

CDNs + Static Files = Win!

Shared CSS, JavaScript, and images are often good caching candidates. Dynamically generated content can be cacheable too, if we can safely reuse its response. The cache key identifies which requests share a stored response, and the policy determines how long that response is reusable. If language or other request details change the response, the cache must account for them. Private account pages should not be placed in a shared public cache. A generic rule to cache everything can expose one person's information to someone else. Freshness and invalidation also need deliberate rules; the CDN does not constantly check every file for changes.

 

CDNs Can Do More...Sometimes a Lot More!

The edge servers that are now the primary entry point for our web site are capable of doing more than just caching data. Depending on your CDN provider, these edge servers have the ability to do a lot more. Some of the additional functionality you get include:

  • Increased security. A common attack on web sites is known as a DDoS (distributed denial of service) where some bad actors flood your web site with data in an attempt to overwhelm it. CDNs have protections built-in to help mitigate these classes of attacks, ranging from blocking the incoming requests to taking advantage of the vast network of edge servers to distribute the load appropriately.
  • Lower origin bandwidth use. Cache hits can reduce transfers from our web server. Misses, revalidation, and uncacheable requests still reach the origin. Whether the total cost drops depends on both the origin bill and the CDN's fees.
  • Smaller content sizes. Some providers offer compression or image conversion, and some offer code minification. Check which features are available and enabled rather than assuming every CDN performs the same transformations. Different image versions also need suitable cache rules.
  • Some protection during outages. Cached content may remain available when our web server is down, if a suitable copy exists and the cache policy allows it to be served. A cache miss or an operation that needs the origin, such as submitting an order, can still fail. Having a CDN does not prove the whole site will work during an outage.

There are other niceties different providers may have. Their availability, price, and configuration vary, so include those details when comparing CDN plans.

CDNs and KIRUPA.com

Before wrapping this all up, I want to share a little bit about this site’s experience with using a CDN. Overall, it has been fantastic. This site gets a fair amount of traffic from around the world, and the average response time prior to using a CDN has always hovered around 500 milliseconds. That’s...not great, not terrible. The following historical Cloudflare report shows one part of that experience: response time from the origin to Cloudflare, with and without Argo smart routing over a 48-hour window. This is not a measurement of complete page-load time:

We can visualize that report geographically too. The origin server was in Los Angeles for this example. The darker green markers show greater percentage improvements, but distance alone does not explain the variation between locations:

My CDN provider is Cloudflare, one of the larger and well-known players in this space. I found getting started with them to be a breeze, and they provide a bunch of tools and services that I slowly ended up relying on to make this site even faster for visitors such as yourself. Having many of my screenshots and illustrations auto-converted to WebP on supported browsers often reduced a page's size greatly:

The WebP slice in this historical chart is 18%. Those WebP images were delivered by the CDN to browsers that supported the format. That slice is not an 18% reduction in page size; measuring the bytes saved requires comparing the actual image transfers. For a site like this where an article can have several megabytes of images, smaller suitable versions can make a big difference.

Do you need a CDN?

I guess the big question for you to answer is whether you need a CDN for your site. The answer is, it depends. Using a CDN is not a magic bullet for your web site’s performance problems. It is one tool among many you should use to incrementally keep making your site faster. There is a lot you can do on the client-side layer in terms of how you build your page, how you optimize your scripts, and so on. There are is a lot you can tweak in your web server to improve performance as well. If you are curious, you can see some of the steps I took prior to investing in a CDN.

If network delivery or origin load is a bottleneck, trying a CDN can be worthwhile. Compare representative visitors and workloads, including cold-cache requests, and check that private responses stay private. A faster response from an edge server won't fix expensive JavaScript in the browser. This site has been happily on Cloudflare for some time, but your measurements, required features, and costs should decide whether a provider is a good fit. And NO, the fine folks at Cloudflare haven't paid me to say this...at least not yet :P

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--