Cobra Developers
Blog, Infrastructure

What is a CDN and does your website need one?

The short answer

A CDN, or content delivery network, is a group of servers around the world that keep copies of your website's files and serve each visitor from the nearest one. Most websites benefit from one, and many modern hosts include one automatically. It makes pages load faster, absorbs traffic spikes, and shields the main server.

Two of our projects sit behind the same kind of CDN for different reasons. CaseCobra, our practice management product, runs in a container on a host, with a CDN in front of it. The Tamou Law Group website is WordPress, with a caching plugin on the server and a CDN in front of that.

The second arrangement taught us the most useful lesson in this post: a CDN is not something you switch on and forget. On that WordPress site, a CDN feature and the caching plugin ended up in conflict. Both were doing their jobs. They were just doing the same job.

Here is what a CDN is, what it does for you, and how to keep it from fighting your other tools.

What is a CDN?

A CDN is a content delivery network: many servers in many locations that store copies of your website's files. When someone visits, the request goes to a server near them instead of traveling to your one main server. The copy is served from there, which is faster and takes load off the original.

The main server is called the origin. The CDN's servers are called edge locations. The first time a file is requested at an edge location, the CDN fetches it from the origin and keeps a copy. Later visitors in that region get the copy.

Distance matters because data takes time to travel, and a web page is dozens of separate requests. Shaving a little off each one adds up.

A modern CDN usually does more than hold copies:

  1. Caching. Serving stored copies of files without bothering the origin.
  2. HTTPS. Handling the encrypted connection with the visitor.
  3. Compression. Sending text files in a smaller form.
  4. Protection. Filtering out abusive traffic before it reaches the origin.
  5. Optimization. Optional features that rewrite images, scripts, or pages on the way through.

The fifth item is where the trouble starts, and we will come back to it.

Does your website need a CDN?

Most websites benefit from a CDN, and many already have one without the owner knowing. Modern hosting platforms for static sites and web apps typically serve everything through their own CDN by default. A site on a single traditional server, including most WordPress hosting, gains the most from adding one.

Your setupDo you need to add a CDN?
Static site on a modern hosting platformUsually no, one is built in
Web app on a serverless platformUsually no for the static files, which are already served from the platform's network
WordPress on a single serverYes, it is one of the better improvements available
App in a container on one hostYes, for speed and for protection of the origin
Visitors all in one city, server nearbySmaller speed gain, still useful for protection and spikes

The honest caveat is in the last row. If your server and nearly all your customers are in the same region, the distance saving is modest. A CDN will not fix a slow page whose real problem is heavy scripts or oversized images. We cover those causes in what makes a website fast.

CaseCobra is the container case in that table. Requests reach the CDN before they reach the application.

How do CDN caching rules work?

CDN caching rules decide which files the CDN may store and for how long. They are set mainly by a Cache-Control header your site sends with each file. Files that never change can be stored for a long time. Pages that change, or that differ per visitor, are stored briefly or not at all.

The header carries a max-age value in seconds. This site's hosting configuration sets two of them:

{ "source": "/fonts/(.*)", "headers": [{ "key": "Cache-Control", "value": "public, max-age=31536000, immutable" }] },
{ "source": "/img/(.*)", "headers": [{ "key": "Cache-Control", "value": "public, max-age=2592000" }] }

Fonts are cached for a year and marked immutable, because a font file never changes. Images are cached for thirty days. HTML pages get neither rule and are left to the host's defaults, so a page is never pinned for a year by our own configuration.

That is the general pattern:

Kind of fileHow long to cacheWhy
Fonts, versioned scripts and stylesA long timeThe content never changes under the same name
ImagesDays to weeksThey change rarely
HTML pagesBriefly, or revalidate each timeContent changes and should appear promptly
Logged-in pages, carts, account dataNot at allThey differ per person

The last row is a safety rule, not a speed tip. If a CDN caches a page that contains one person's details and serves it to the next visitor, that is a privacy incident. Anything personal must be marked as private or excluded from caching.

When you change a file that is cached for a long time, you have two options: give it a new name, or tell the CDN to purge its copy. Naming files with a version in them is the reliable approach, since it also deals with copies stored in visitors' own browsers, which a purge cannot reach.

Can a CDN conflict with a caching plugin?

Yes. A CDN and a caching plugin overlap in what they can do: both can cache pages, shrink files, and change how scripts load. When both are set to do the same job, they can undo each other's work, serve stale pages, or break scripts. Assign each job to one tool only.

This happened on a WordPress site we maintain. A CDN feature was in conflict with the caching plugin on the server. Neither tool was broken. They overlapped.

The overlaps to check:

  • Page caching. If both layers cache HTML, clearing one leaves the old page in the other. Decide which layer owns page caching, or make sure a purge clears both.
  • Minifying. Shrinking the same file twice gains nothing and occasionally breaks it.
  • Script loading. If two tools each rewrite when and how JavaScript loads, the result can be scripts running in the wrong order or not at all.
  • Image handling. Two tools converting or lazy-loading the same images can leave gaps.

Our rule since then: write down which tool owns each job, turn that job off in the other, and change one setting at a time with a measurement before and after. The full order of work is in our WordPress speed optimization checklist.

What does a CDN not do?

A CDN does not make slow code fast, does not replace backups, and does not secure a site whose software is out of date. It speeds up delivery of what you already have and filters some bad traffic. A page weighed down by third-party scripts will still be slow from an edge location.

It also adds a layer to reason about. When a change does not appear, the first question becomes "which cache is this coming from". That is a fair price for what you get, but it is a price. We compare hosting arrangements more broadly in serverless vs traditional hosting.

Frequently asked questions

Is a CDN the same as web hosting?

No. Hosting is where the original site lives. A CDN sits in front of the host and serves copies. Some companies provide both together, which is why the line is blurry, but they are separate jobs.

Will a CDN improve my search rankings?

A CDN can make pages load faster, and speed is one of many things search engines consider. It is not a ranking shortcut. Content and relevance matter far more than a small improvement in load time.

Why does my site still show old content after an update?

A cached copy is being served, either by the CDN or by your own browser. Purge the CDN's cache for that page, then reload without the browser cache. If it keeps happening, the caching rules for that file are too long.

Does a CDN cost money?

Often not much. Some CDN services offer a free tier aimed at small sites, and many hosting platforms include a CDN in what you already pay. Plans change, so check current pricing before you decide.

If your site is slow and you are not sure whether a CDN would help, we can look. Contact us.

Who wrote this

The Cobra Developers team. We are a software studio in Arizona that builds and runs its own products and builds websites, web apps, and automation for clients. This post draws on our work on CaseCobra, Tamou Law Group. Everything here comes from projects we shipped; see all 30 of them.

guest@cobra: ~