Blog post image

When to serve your website through a CDN and when not to

I see tons of websites on the internet addressing that your overall website loading speed will increase when you use a CDN (Content delivery network). But I still haven't seen one that talks about when it is more efficient or more sustainable to use a CDN, and when you should stick to your origin server.

In this article, I will try to address some of the upsides and downsides of using a CDN, and I will try to find the answer to when it is more sustainable to host your website through a CDN and when it is not. If this excites you, then head on!

What is a CDN (Content Delivery Network)?

A CDN or a Content Delivery Network is essentially a spider web of optimized servers located in different positions around the world. It has multiple functions, but some of the most known are: 

  • Speeding up your website on a global scale.
  • Adding security to your website by hiding your server's real IP address.
  • Allowing you to serve more visitors at once.
  • Display your website even though your server of origin crashes.

There are many useful functions to a CDN, but the ones listed above are some of the most known/famous.

How does a CDN work?

A Content Delivery Network is a complicated technology, and we don't have to know exactly how they work to understand that they benefit us in many ways. The technology used and the optimization process they go through is on a level that most of us won't be able to grasp. One might think that just because you involve more machines it means more pollution, but the answer to that is; It depends.

One important thing that we have to understand is that a CDN caches your website or in other words take a copy of it. This way it can serve a cached version next time it is requested instead of asking your hosting server for the data. When for example a visitor from Hong Kong requests your website it will be sent to the CDN which determines the closest location to the visitor, and then serves the content from there. If the content isn't available at the closest location it will be downloaded from your hosting server. Then the next time the CDN receives a request from Hong Kong it will serve the cached version from that CDN. This shortens the travel distance and the response time which is both more sustainable and faster.

CDN or no CDN?

You came to this article with one question in mind "When is it sustainable to serve a website via a CDN, and when is it not". So let's look at some different scenarios, and from that, we can try to determine when it is more sustainable to serve your website via a CDN or by using the original server.

Where are your visitors located?

One important thing to talk about is, where are your visitors located. You might run an international blog written in English which means your visitors could be located in almost every country around the globe. You could also be running a local carpenter business which means your visitors are located in your local area or at most the region you live in.

The two different websites are serving two very different audiences, and depending on which you are serving it might not be sustainable to use a CDN, or will it?

If your visitors are located near your server of origin you could argue that it would be more sustainable to just be using your server of origin, but in most cases, it is actually not the case. There might be a few scenarios where a CDN wouldn't be the appropriate choice, but simply basing your choice on your visitor's location isn't enough. To get a clearer picture we would have to locate where your server of origin is and where the nearest CDN location is based. We will also have to weigh all of the additional benefits that come with a CDN to determine whether your visitor's location is enough to rule out the use.

Security against attacks

One of the big benefits of using a CDN is that many of them come with security implementations that can save you from quite the headache. DDoS attacks and malicious bots are just some of the threats you meet online, and handling all of these by yourself is time-consuming and hard.

When you only host your website on the origin server you have to handle all of these attacks by yourself, and if someone should get through your security you risk losing all the data on your server which can be very costly.

DDoS attacks work by sending approximately +275.000 requests per second to your server which will ramp up the energy consumption and slow down your website drastically. Eventually, your server will give up and no one will be able to get access to your website.

With CDN services such as Cloudflare, protection against DDoS and malicious bots come as standard, and just by using their free subscription, you will be protected as well. Their security will stop these attacks from happening and if someone should go through it won't affect your server of origin.

Energy consumption

Finding something on the internet specifically about the energy consumption of known CDN services around the world seems impossible. Even after hours on Google, I wasn't able to find anything specific about the topic. Luckily a conversation on Slack with some fellow developers lead me to some answers that were useful to this article.

As standard, our server of origin will be handling every request that is sent to it. We can choose to optimize it by making it cache some of the requests so that the server won't have to generate data every time. But it is time-consuming, and unless you are an expert you can end up worsening the result or at least end up with the same result as before you started.

Known CDN services such as Cloudflare are specialized in caching content and optimizing machines for the purpose. They have been studying the art of caching files for years, and therefore have much more knowledge in the field than most typical developers or website owners have. They purposely set up their machines to run as optimized as possible for serving cached websites, and therefore it is hard to achieve better results than they have. Some CDNs even run their data centers on 100% renewal energy which makes it even harder to justify not using a CDN.

Self-hosting or using CDN for stylesheets and scripts?

One thing that developers and designers have been doing for years without even thinking about it, is including CDN links to CSS stylesheets and scripts in their HTML. This method is commonly used when working with frameworks such as Bootstrap or jQuery, but this method should be stopped right away.

Back in the day certain stylesheets and scripts would have been cached in global storage in the browser which meant websites could share the same resources, but that is not what is happening anymore. Because of theft and other types of scamming this was removed and instead every website requests its own data.

When you add CDN links to CSS stylesheets or javascript files in your HTML, then you send a request to a different server. This server consumes energy to send back the requested data to your web browser, which means that not only your own server will be consuming energy but also the CDNs that you are sending requests to.

In cases where you need to link to files that you don't have in your project, it is more sustainable to download the file(s) and host it from your own project. This way only your server will consume energy to serve the requested data instead of many.

When is it most efficient to use a CDN and when shouldn't you?

With everything we have talked about in this article until now, I think it stands pretty clear that using a CDN in most cases would be the most sustainable choice. 

A CDN will cache your website in the nearest data centers to your visitors which will shorten the distance that the data have to travel. Even if your visitors are local to your area and your server is located in your living room, it is still hard to justify only using the server of origin because of the other benefits you get out of the box. You will automatically with CDNs such as Cloudflare be protected against DDoS attacks and malicious bots which would require additional time for you to manage. 

There could be very rare and specific cases where only the server of origin would be the most sustainable choice. You could for example run a Raspberry Pi only using a solar panel in your backyard, and the only visitors you would have is the ones in your neighborhood. I honestly think that in a situation like that, it would be more sustainable to only use the server of origin.

Thank you for reading this article! If you have additional facts to add or you just simply want to share your point of view, then please share it in our comment section below. Lastly, I would like to encourage you to share this article with your community to spread awareness of this.