Blog March 22, 2018

BYO CDN with DNS

Dare you to say that five times fast…

BYO CDN with DNS

BYO CDN with DNS

BYO CDN with DNS

BYO CDN with DNS

BYO CDN with DNS

Great workout for your mouth, but do you know what it means?

  • BYO (Build Your Own), pretty self-explanatory.
  • CDN (Content Delivery Network) which is a service that hosts your website files like HTML, CSS, and JavaScript on multiple web servers around the world.
  • And last but not least, DNS (Domain Name System) the service that maps domain names to IP addresses.

Put it all together and you get a distributed web server network managed by a DNS hosting service (aka: your very own CDN).

We are going to show you how you can create your own CDN service using a DNS management service, like Constellix, using your own web servers. It’s easy and can actually be cheaper than a managed CDN service.

In the last few years, CDNs have become essential for all web-facing websites and applications. They cut down on load times, which have been shown to directly affect bounce rate and improve user experience. A single Tweet from a Google employee is partially responsible for the surge in CDN adoption when they set the standard for website load times at two seconds.

That’s 2000 milliseconds. Aka: your website needs to load before you can blink five times. I’ll wait while you try that… Fast right?

Now, CDN’s aren’t guaranteed to make your site load in under two seconds. You can have a really heavy site with a bunch of external resources, add a CDN and still see long load times.

You need to combine a CDN with savvy web development, mindful web design, and lots of experimenting.

How a CDN Works

Know it all already? You can skip this section and get right to the good stuff.

Before we can get into how CDN’s work, you need to understand how websites work. In order to make a website live on the Internet, you need to host the website files on a web server. You then need to create a DNS record that tells anyone who types your domain into their browser where your web server is located.

How DNS works

This might be all you need if you’re a mom and pop with users super close to your web server. But if you have users in other regions, countries, or a large number of users… you need a CDN. Here’s why:

No CDN

See what I mean? All of your users will have to travel all the way to your web server and back to load your website files. If you only have one web server, this means long load times, high bounce rate, and even search engine ranking loss.

The CDN Difference

CDN’s operate on an Anycast network backbone. That means your website files are hosted on many web servers in data centers around the world. When you update your website and push the update to the origin (your CDN), those changes are instantly propagated to all the web servers in your CDN configuration.

How a CDN works

Now, when a user wants to load your website, it’ll take a fraction of the time, no matter where your users are located.

Again, this may be all you need; something you can turn on and forget about. But if you want more customization and control of where your web servers are… read on.

The Good Stuff

Build Your Own CDN with DNS

Steps:

  1. Provision web servers
  2. Change your nameservers
  3. Create health checks
  4. Create records
  5. Setup routing policies

Provision Web Servers

You can use a cloud service provider like Digital Ocean to provision web servers in just a few seconds. Make sure you have at least one (preferably two) servers in each of your target regions.

Change Nameservers

You can use whichever managed DNS provider you want, but for this blog, we are going to use Constellix. Create an account and follow along. Right now, we are offering $25 off your first bill if you take a quick survey.

Add your domain(s) to your DNS provider. There are 8 different ways to do it in Constellix. Update your nameservers at your registrar and wait to make sure everything propagated. Here are some tips on how to do this safely.

Create Health Checks

Log into the Sonar application and create health checks for your web servers. You can learn how to make a health check here. Make sure you select monitoring nodes/locations that are relevant to where your web servers are located. Be specific when you name your checks and include which region they are in. This will make your life a lot easier when you apply these checks to records later.

Setup Routing Policies & Create Records

You can do this three different ways in Constellix: users are answered by web servers in their region, or the closest server (geographically), or by response times.

#1 Regional:

Group your web servers into five regions: US East, US West, Europe, Asia-Pac, and Oceania. Users will automatically be pointed to CDN’s in their current region. Eg: users in Asia-Pac will be routed to web servers in Asia-Pac.

Global Traffic Director

Enable the Global Traffic Director and commit your changes. You will now see five tabs, one for each region. Choose a web server you want to use as your “default”. This will only be used if we can’t detect which region a user is in. Create a record in the “World” tab that points to your default web server.

Switch to a regional tab and create A or ANAME records (pointing to the IP address or hostname, respectively) for all the web servers you have in that region. Rinse and repeat for each region.

You can also create failover rules for each of the web servers. Enable failover in the record you already created. Attribute the monitoring check you created in the previous step. Specify a backup web server and associated monitoring check. If our nodes detect your primary web server as unavailable, we will move your traffic over to a backup web server.

Our nodes will continue to monitor your primary web server and if it is available again, we will move traffic back to it.

#2 Proximity

CDN’s are built on Anycast networks. That means users are routed to the closest web server in the network. But how does it determine closeness? Based on “network closeness” or the fewest amount of hops between a user and the destination.

But it’s not always the fastest method. Instead, you can use GeoProximity which automatically calculates the geographically closest server to a user.

GeoProximity

Enable GeoIP services and create GeoProximity rules for each of your web server locations. Eg: if you have a web server in Ashburn, VA you would create a GeoProximity rule for the coordinates of your Ashburn data center.

Create A/ANAME records for all the web servers you have and apply their respective GeoProximity rule. Now, your users will automatically be routed to the web server closest to them.

Learn how to set up GeoProximity here.

#3 Performance Routing

But there are some problems with this. Closeness doesn’t always mean that that server is the fastest responding.

That’s why we came up ITO, Internet Traffic Optimization. This service attaches monitoring checks to all your web servers. Every few minutes, we’ll check which server(s) is/are responding the fastest and update your DNS records to send users to that/those server(s).

You can learn how to set up ITO here.