A CDN is also known as a Content Delivery Network. It’s typically used to speed up a website. Especially when it’s visitors are distributed around the world. For a small number of sites, a CDN is also important because its reduces the demand on the sites infrastructure.
In today’s post, I will cover the fundamentals behind this wonderful part of Tech.
What exactly is a CDN?
Think of it as a set of distributed servers. Each of which can serve up content such as pictures from the origin. Let’s make it simple.
You have a site named corebear.net. The machine that hosts the site is London.
The site loads perfectly fine for those who are near London. Perhaps even for those in places like New York due to premium routing on the network level.
In fact, its so fast for you because you also live in London. You gain a false sense of security because you also believe its fast for your other visitors.
Unfortunately, that is a common misconception and here is why.
If I load the site from a place like Singapore, Australia, South America, Las Vegas or Eastern Europe. It’s a lot more likely that the user will receive a different experience. This is due to the physical distance between the machine that is serving the request vs the end-users location.
This is where a Content Delivery Network (CDN) comes into play. Typically a CDN provider will have hundreds of machines scattered around the world. The idea is these same machines will then serve your visitors on your behalf and utilise your own server when it needs to fetch data.
The result? A faster experience for end-users. Which often translates to greater success on the internet.
Is it that simple?
You guessed it! No, it’s not that simple. There are factors involved that need to be considered. But the basic premise of a CDN is it will help speed things up for visitors.
Here’s an example scenario where a CDN doesn’t provide a net benefit to the end-user:
- Your own server is in London.
- Your visitor is in Singapore.
- The visitor visits corebear.net.
- The CDN receives the request and determines the best server to service the visitor is in Hong Kong.
- The Hong Kong server doesn’t have a copy of the data and therefore needs to request it from the origin server (the one in London)
- End result: The end-user still has a slow experience.
How does a CDN work?
I believe it’s important to understand the different types of CDN’s first.
It can either be a proxy CDN, where your visitor literally connects to the CDN’s web server, which in turn relies on the origin web server to fetch data accordingly in the background.
Or it can be used for specific static content such as images, CSS and JS files. For example you have a site that has hundreds to thousands of images that lazy load. Instead of saturating your own infrastructure with this work load. You choose to off load it to your CDN.
Instead it pictures loading from /images/example1.png. It loads from your subdomain cdn.corebear.net which points to the CNAME record of your CDN provider.
As a result, whenever someone requests the image. It will go straight to the CDN provider.
But how does it actually work? Good question! Again, it does vary but I’ll explain a few fundamentals:
- Anycast - You visit a site and it resolves an IP address. This is represented as the A record. An Anycast enabled IP is like having the same web server in multiple locations. A good example is if you ping 8.8.8.8 or 1.1.1.1 from multiple locations in the world. It will use Anycast to route you to the nearest server. A CDN could have a single IP represented in all major regions. Then based off Anycast routing. The end-user will interact with the closest server to them.
- Geo-DNS - Similar to Anycast but instead it resolves different IP addresses. A user in London could receive X but a user in Canada will receive Y. This is usually based on what routing tables. It’s not completely accurate but is very good. Some newer DNS providers aim to improve this further by adding additional logic at the expense of slower response times. For example if you want London to receive A IP address and B for those in North America.
- Anycast vs GeoDNS - Both have trade offs but at a fundamental level, this is likely how most CDN providers operate.
- Caching - A cache hit vs a cache miss could mean the difference between a slow experience and a fast one. Caching is typically setup to hot cache any data that is in demand and cold cache anything else for a specific time until they expire.
Should I use a free CDN or a paid CDN?
Free
If your budget is low or your website doesn’t have a lot of demand. A free CDN such as Cloudflare should be sufficient. It provides plenty of features for free. Including DNS hosting.
However, if you’re a business that earns revenue through the internet. It is in your interest to invest a small fee into a suitable paid tier of Cloudflare or to use another CDN altogether.
Paid
There are a lot of different options in the marketplace. Each catering to a variety of different use cases. In summary if you’re serious about facilitating a fast user experience due to the use of a CDN, then you will need to pay for it.
Here are a few options that I can personally vouch for:
- Bunny - Previously known as Bunny CDN. Bunny is a modern CDN provider that also includes other complementary features such as storage, DNS and various “smart” features. Overall it has a good reputation and is known for its fair pricing. I cannot comment on the support experience with Bunny but their online material does appear positive.
- Cloudflare - The more mature version of Bunny. Cloudflare is known around the world as the provider that helps the majority of businesses operate online. It has a rich feature set and options for everyone. But please note, if you require support then you will likely face issues according to countless Reddit users. Factor this in when making a purchase decision.
- Should I do it myself? - Unless you have a very specific use case that warrants you setting up your own pseudo CDN, then I would suggest you don’t do it. Both options above should cover 99% of use cases.