Skip to content

What is a CDN?

Updated Nov 30, 20263 min readBased on official documentation
What is a CDN?

In short

A CDN, or Content Delivery Network, is a group of servers spread across many locations. It stores copies of files and serves each user from the server closest to them. This makes websites faster and helps them handle heavy traffic. CDNs are often used for common library files like jQuery and Bootstrap.

What a CDN is

A CDN, short for Content Delivery Network, is a set of servers placed in many places around the world. Each server keeps copies of the same files. When someone asks for a file, the network sends it from the server that sits closest to that person. Your own server does not have to do all the work.

The idea is simple. A file that travels a shorter distance arrives sooner. Distance affects latency, which is the delay before a response starts to arrive. A CDN cuts that delay for many users at once.

What CDNs are used for

CDNs are widely used for static assets. These are files that do not change from one visitor to the next, such as stylesheets (CSS), JavaScript files and images. Many developers load popular libraries like Bootstrap and jQuery from a CDN instead of hosting their own copies.

This has a few practical benefits. Serving library files from a CDN lowers the number of requests your own servers must handle. CDN servers are also usually configured with sensible cache settings already, so you do not need to set those up yourself. Some teams go further and serve an entire static website through a CDN. A static website is one made only of HTML, CSS and JavaScript files, with no server-side code building pages on each visit.

The downsides

A CDN is not free of trade-offs. The first issue is dependency. Your site now relies on a third-party service. If the CDN goes down, is blocked in some region, or shuts down for good, parts of your site may stop working.

The second issue is security. If an attacker takes over the CDN, they could send harmful code to your visitors. One common defense is Subresource Integrity, or SRI. With SRI, you add a cryptographic fingerprint to a script or stylesheet tag, and the browser refuses to run the file if it has changed.

The third issue is performance, and it surprises many people. Connecting to another website takes extra steps. The browser must look up the domain name, negotiate the connection and so on. Modern browsers also keep separate caches for each website for privacy reasons. So if two sites both load jQuery from the same CDN, the visitor may still download it twice. In some cases, hosting the file yourself is actually faster.

A simple example

Suppose you want to use Bootstrap's stylesheet. Instead of copying the file into your project, you add a link tag that points to a CDN address:

The browser fetches the file from the nearest CDN server. Your own server only sends the HTML page. Replace the example address with the real CDN URL given by the library's documentation.

Common mistakes

Beginners often assume a CDN always makes a site faster. As noted above, that is not guaranteed. Measure your page before and after the change.

Another mistake is loading a library from a CDN without a fallback or an integrity check. If the CDN fails, the page may break silently. Adding SRI hashes protects against tampered files, though it does not help if the CDN is simply unavailable.

Finally, do not forget that a CDN is a third party. Check its terms, its region coverage and its reliability before you depend on it for something important.

Alternatives

The main alternative is self-hosting. You keep the static files on your own server or in your own storage, and you manage the caching yourself. This gives you full control and removes the extra third-party connection, but it adds work and puts more load on your own infrastructure. For a small site with a few assets, self-hosting is often a reasonable choice. For a large site with visitors around the world, a CDN is usually worth considering. You can read more about how web servers handle requests on our site.

FAQ

Is a CDN the same as a web hosting service?

No. A web host runs your site on a server you rent. A CDN keeps copies of your files on many servers around the world so they load faster for each visitor.

Do I need a CDN for a small website?

Not always. A small site with few visitors may load fine from one server. A CDN becomes more useful as traffic grows or when your visitors are spread across many countries.

Does using a CDN make my site less secure?

It adds a risk, because a compromised CDN could serve harmful files. Subresource Integrity helps by letting the browser check that a file matches an expected fingerprint.

Why might a CDN slow a page down?

Connecting to another website takes extra steps, such as a domain lookup and a new connection. Browsers also do not share cached files between sites, so visitors may download the same library more than once.

Sources

This page is based on the official documentation below. Read it for full detail.

CDN — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.