Skip to main content
CodingMarble

Technology explained

What Is a CDN? Content Delivery Networks Explained

A content delivery network is a group of servers spread across many locations that keeps copies of a site's files and serves them from the one nearest the visitor. This shortens the distance data travels, reduces load on your own server and can soften traffic spikes and some attacks.

By CodingMarble · Published · 4 min read

Two microwave dishes on a tower above a building
Photo: Miles Smith on Unsplash

The idea: put copies closer to people

Suppose a popular book is stored in one library in one city. Every reader in the country must travel there or wait for it to be posted. A smarter arrangement is to keep copies in branch libraries across the country, so most people collect it nearby, and only the first request at each branch involves the main library. A content delivery network, or CDN, does this for website files.

Your own server, the origin, holds the original files. The CDN provider runs many servers in many cities, called edge servers because they sit at the edge of the network, close to ordinary users. When a visitor requests a file, the request reaches a nearby edge server. If it already holds a copy, it replies at once. If not, it fetches the file from the origin, keeps a copy and passes it on. Later visitors in that area then benefit.

Why distance matters

Signals travel at a finite speed, and a connection needs several back-and-forth exchanges before data flows, so every round trip across an ocean adds noticeable delay, as described in the guide on how the internet works. A page made of many small files multiplies that cost. Serving files from a nearby location cuts the length of each trip. It also means your origin server is asked for each file less often, so it has more capacity left for the work only it can do, such as processing orders.

The benefit depends on where your visitors are relative to your origin. A local business whose customers all live near the server may see little change, while a site with readers in several regions can see a larger difference. Measure rather than assume, using the method at the end of this guide.

What gets cached and what does not

CDNs are best at static files, which are the same for every visitor: images, stylesheets, JavaScript files, fonts, videos and downloadable documents. Whole HTML pages can also be cached when they do not vary between users, for example a blog article or a marketing page.

Content that differs per person or changes constantly is a poor fit, or needs care. A logged-in account page, a shopping basket, a checkout, a search result or a response that depends on cookies must not be shared between people, because one visitor could be shown another's data. Sites tell the CDN what is allowed through instructions on the response, and most CDNs also let you set rules by path. If you cache something that should have stayed private, the consequences are real, so test logged-in and logged-out views separately.

Cache expiry and updating files

A cached copy cannot stay forever or visitors would see stale content. Your server attaches a Cache-Control header that says how long a copy may be reused, for example for five minutes or a year. When the time passes, the edge server checks with the origin whether the file changed, or fetches it again. Longer lifetimes mean faster pages and less origin traffic but a slower route for changes to appear.

Prompt· Typical headers: a long-lived asset and a short-lived page
# A versioned image or script that never changes at this address
Cache-Control: public, max-age=31536000, immutable

# An HTML page that should refresh often
Cache-Control: public, max-age=300

A common practice is to give files a version or content hash in their name, such as app.4f2a9c.js. Because a new release gets a new name, the old copy can be cached for a very long time safely, and visitors pick up the new file as soon as the page points to it. For urgent fixes most CDNs offer a purge or invalidate feature that removes chosen files from every edge. Check your provider's current documentation, since behaviour and limits differ.

Practical tip: when an edit does not show up, test in a private window and look at the response headers before blaming the code. The problem is often a copy that is still within its allowed lifetime, in the CDN or in your own browser.

Absorbing traffic and helping with attacks

Because the edge answers most requests, a sudden rush of visitors, for example after a mention on social media, mostly lands on the CDN's large network rather than on one small server. Many providers also sit in front of the origin and filter traffic, which helps against distributed denial-of-service attacks that try to flood a site with bogus requests. Some add a web application firewall that blocks known malicious patterns. These features reduce risk but do not remove it: attacks that reach uncached, dynamic pages still hit your server, and a CDN does not fix insecure code. The guide on website security basics covers the wider picture.

When a small site benefits, and how to tell if a site uses one

A small site gains most when it has large images or videos, visitors in several regions, or a modest server. Plenty of hosting plans and website platforms already include a CDN, so you may be using one without having set it up. A very small local site with few visitors and light pages may gain little, and an extra layer adds something more to configure and to debug. Pair a CDN with the image work in the guide on optimising images, since a smaller file is faster from anywhere.

To see whether a site uses a CDN, look at its response headers. In your browser's developer tools, open the Network tab, click the main request and read the response headers. You can also use a terminal.

Shell· Show only the response headers for a page
curl -sI https://example.com/ | grep -i -E "server|via|age|cache|x-cache|cf-"

Clues include a server or via header naming a provider, an age header that shows how many seconds the copy has been held, and a cache status header with a value such as hit or miss. Header names are chosen by each provider and a site can hide them, so a missing header is not proof that no CDN is present.

Need this built properly for your business?

CodingMarble is a remote studio in India that builds websites, apps and games on fixed packages with the scope written down before payment. Try the free tools we run, or start a project when you are ready.