What Every Webmaster Should Know About DNS
DNS is essentially the glue that holds the internet together. In this article, we delve into this topic to discuss some ways web devs can take advantage of the DNS.
Join the DZone community and get the full member experience.Join For Free
<brief/> history of dns
behind every website is a little-known system that is the glue holding the internet together. it’s called the dns, or the domain name system.
thirty years ago, when the internet was still in its infancy when you wanted to visit a website you had to know the ip address of that site. that’s because computers are and were only able to communicate using numbers.
this is an ip address: 127.33.54.200.
it’s long, hard to remember, and we (humans, i presume) are not robots. we needed a way to translate computer-readable information into human-readable. and it had to be fast, lightweight, and scalable.
in the early 1980s, paul mockapetris came up with a system that automatically mapped ip addresses to domain names, and the dns was born. this same system still serves as the backbone of the modern internet, today.
and yet, only a small subset of the world knows that it exists, and an even smaller group understand what it does. the real problem is that the people that need to know how it works and could actually benefit from this knowledge… don’t take the time to learn.
if you’re a webmaster, web designer, front-end developer, it, or technical support you need to know the basics of how dns works and how it can help you manage your domain’s presence on the internet.
how dns works
before we get into how you can use the dns, we need to understand how the system works. we already know that it maps ip addresses to domain names, but where is this information stored? on nameservers!
name servers store dns records which are the actual file that says “this domain” maps to “this ip address.” so is there a room somewhere that has all the nameservers and dns records for every site on the internet? no… that would be ridiculous.
they are actually distributed all around the world. these nameservers are called the root nameservers and instead of storing every domain ever, they store the locations of the tld (top level domains).
tlds are the two or three character strings like .com that end a domain name. each tld has their own set of nameservers that store the information that says who is authoritative for storing the dns records for that domain.
the authoritative nameserver is typically the dns provider or the dns registrar (like godaddy that offers both dns registration and hosting). and here we can find the dns record that maps example.com to the ip address 127.66.122.88.
ttls and caches
let’s put that all together. when you query a domain name your first step won’t actually be at the root name servers. instead, your browser will ask your local resolving name server if they have the dns records for that domain cached.
the resolving name server is typically your isp (internet service provider), and if it’s a popular website like youtube.com they will likely have the record in their cache. in this case, you would skip the rest of the dns lookup process.
however, these records are only stored for a short period of time. whenever you create a record, you have the option to set a ttl (time to live). ttls tell resolving name servers how long they can store the record information. ttls can range anywhere from 30 seconds to a week.
what if the record we are looking for isn’t cached? then the resolving name server will ask the root name servers for the tld for that domain, which will point you to the provider that's authoritative for hosting the records.
how dns works
okay, that was a lot of steps to go through just to find the ip address. oh, and by the way, this process happens in just a couple milliseconds. a little perspective, you blink your eye in roughly 50 milliseconds. you can resolve most dns queries in under 30.
think this is cool? learn more about how the dns works .
basic stuff everyone should know
now we are going to take a look at how this very simple system can be used to help you keep your website online, improve website load times, email delivery, and so much more.
your first encounter with dns almost always happens at your domain registrar --where you bought your domain name.
ok, so you have a domain name, cool, but it doesn’t actually do anything yet. if you type your brand spanking new domain into your browser, i bet you’ll see a placeholder page courtesy of your registrar.
placeholder page for a new domain without a website.
first things first, get off your registrar’s dns. there are many reasons why, and you can read more about those here .
you will need to change your domain’s nameservers to those of a specialized dns provider.
how you do it depends on your current provider, but basically what you want to do is make note of all of your existing dns records. there may also be an option to export them into a zip file. though it’s likely it won’t have any records unless you bundled with web hosting or another service. if you do, you’ll want to create the exact same records at your new provider.
now you can add your domain to your new provider. you’ll be prompted to update your nameservers through your registrar. this tells the company who you bought your domain from and that you are going to be using another provider for dns hosting. you may need to wait a few minutes.
one thing to note, changing dns providers does not change who your registrar is. your domain is still registered through your registrar, but you are now hosting your dns information (records) through a different provider.
now that you have moved to an external provider, you can start adding records. let’s assume you don’t have any yet. you will need a handful to get your site up and running, which only takes a few minutes.
before we go any further, you need to decide whether you want to send your users to www.yourdomain.com or yourdomain.com. the latter is called a “naked domain” or “cname flattening” and it’s the latest craze because frankly, it just makes life simpler.
we’re going to assume you, like the rest of the world, prefer to not type in the extra 4 characters of www.
but what if someone enters www.yourdomain.com into their browser? where will they go?
nowhere. because we have to tell them that www is not the preferred version of our site. we can do this using a cname record.
cname records point domains (or hostnames) to other domains (or hostnames). they are also called alias records because they tell the user that the record information is actually hosted somewhere else.
you can even use cname records to point to external domains like www.yourdomain.com to www.google.com. just remember to add a dot at the end, or the hostname will be appended to the beginning of your domain like www.google.com.yourdomain.com .
keep in mind that cname records are different from web redirection because you can only point domains to other domains, no fancy url paths. you wouldn’t be able to say www.yourdomain.com points to www.google.com/this-is-not-a-page.html.
another thing to note, cname records cannot be used at the root level. so once we make our domain naked, we won’t be able to use a cname record to point our domain to another hostname… more on this in a few minutes.
for this use case, we need to point www.yourdomain.com to yourdomain.com . that means we are telling the user that the ip address for www.yourdomain.com is actually stored at yourdomain.com .
now we need to create a cname record that tells everyone who types in www.yourdomain.com to go to yourdomain.com .
for the record, name enter www and point that to the host: yourdomain.com. make sure you do not add a period/dot/stop at the end. you only want to use those if you are pointing to an external domain like google.com.
how to create a cname record to a naked domain
once you commit your changes, your domain will be naked! go ahead and try it.
add a web server
ok, we’re live on our new dns provider and our domain is naked… but you don’t have a website. it just shows a blank page.
you need to create a record that tells your users how to get to the web server where you are hosting the files for your website. let’s assume you already have a web server and at least a homepage (index.html) setup. your web hosting provider will give you an ip address or hostname for the server your site is hosted on.
you need to tell users who visit your domain that they need to actually be going to the web server to see your website files. you can do that with an a record which answers users with the ip address of the web server or an aname (read more about this later) record that points to the hostname of the server.
how a website loads
in this example, we are going to use constellix to show you how to create an a record. leave the name field blank, since we want this to work at the root (naked) level of our domain. in the ip field, enter the ip address of your web server.
how to add an a record
your changes will propagate instantly. now, when you type your domain name into your browser you will see the index.html page you have stored on your web server. all thanks to dns.
simplify your life
the next thing you need to do is setup your ftp (how you upload website files) and mail servers. both of these services will most likely need to point to your web server. there are two ways to do this. you can take the hard way and go and make more a or cname records that point mail.yourdomain.com and ftp.yourdomain.com to your web server.
or, you can create cname records that point to the root of our domain, which already has an a record that points to our web server.
how a cname to an a record works
that means, if you ever change your web server, all you have to do is update a single a record.
have you ever accidentally added an extra “o” to google.com? it’s okay, we all have. but did you notice that you still ended up at google.com? not gooogle.com or a blank page, but plain old google.com. that’s because google uses a cname record to send traffic that mistakenly adds an extra “o” to their intended destination.
when you set up your own site, you may want to do the same thing. this does mean that you have to buy domains that are similar or a letter off from your own domain. then, all you have to do is create a cname record that points users to the correct domain.
so far, we’ve only discussed basic website setup using dns, but dns does so much more than simply tell users how to reach your site. the system has evolved to become an optimization tool to decrease load times, deliver location-specific content, and so much more.
a few months ago, i was traveling from dc to seattle. while i was waiting in the airport, i took to yelp to find a place to get brunch once i landed. but almost every website i tried to load took over ten seconds just to get the first image to appear.
why? it’s simple physics, as distance increases between two points, it takes longer to reach each endpoint.
remember earlier, how our example website was hosted on a single web server? well, this has actually become outdated for any business that has customers outside of their immediate locale. i mean, sure, the brunch place i wanted to go to usually won’t have customers from dc that often; but if you have an online store or a blog that you want people in los angeles to see and you’re on the east coast… you’ll need more than one web server to get your visitors to stick around.
we can solve this with a cdn service. cdns (content delivery networks) are networks of web servers, each host a copy of your website. that means whenever a user wants to reach your site, they won’t just be sent to any old web server, but the one that is physically closest to them.
since users have to travel less distance to reach your content, it loads faster. this can turn load times from ten (or more) seconds to under two!
how a cdn works
but here’s the problem… right now we have an a record pointing to our web server. how do you point to the multiple web servers in a cdn?
you can’t. instead, you have to point to the cdn using a cname record. the cdn will then calculate where the closest web server is and use an a record to point users to that web server.
the naked problem
but wait, cname records can’t operate at the root of a domain. so if we have a naked domain (no www.) we won’t be able to use a cdn.
wrong! a few years ago, dns made easy invented a new record type called an aname record. this record is like a cname record, but it operates at the root level.
in constellix, all you have to do is create an aname record, leave the name field blank (because that’s the root) and point it to your cdn provider’s hostname (end with a dot).
aname record in constellix
ps: if you’re a reseller or manage a lot of domains with similar configurations, create a template with these records and whenever you add a new client (domain) apply this template.
when sh%t goes down
last but not least, we need to prepare for the worst. we already saw how to use a cdn to host multiple copies of our site across the world. each of these copies acts as a sort of backup, so if one of those web servers isn’t working, users will just go to the next closest web server. simple, right?
well, we can use a similar technique using dns records to automatically send users to another website or server if our main one isn’t working. this is called dns failover.
so what if we have an a record that points yourdomain.com to an ip address like 126.96.36.199. and then we want to add another ip address 188.8.131.52, and maybe another at 184.108.40.206 because we have a couple different web servers we like to use for this website.
when a user queries our domain, how do we know which ip address will be returned? dns records default to using round robin, which rotates which ip address is returned. so if we were to query our domain a bunch of times, one in every three times we would see 220.127.116.11 being returned.
failover uses round robin to cycle through available endpoints, but will only return endpoints that are detected as “up” or healthy.
you can use failover to send your users to backup web servers, email servers, cdn providers, and so much more.
let’s take a look at this in constellix. we are going to create a cname record that points to a cdn. we are also going to enable failover, which tells constellix that if our primary cdn is unavailable, then it should point to our backup cdn instead.
cname failover in constellix
see the two fields for hostnames? that’s where you specify your primary and secondary cdn services (or whatever you want).
to the right of each hostname is a drop-down for a sonar check. this is a simple monitoring check you can set up using our integrated network monitoring service. if sonar detects one of your endpoints as down (big red letters, can’t miss it) then it will no longer point users to that endpoint.
let’s say our primary cdn, mycdn.com , was unavailable. failover will automatically send all your traffic to othercdn.com . it’s not magic, it’s dns!
i know there’s a lot going on here, but bear with me. this little setting could decide whether or not your site is online or not.
sh%t happens, especially on the internet. i mean look at this calendar of just some of the major outages that happened last year. in almost every instance, a single service was responsible for knocking that website or application offline.
what aspect of your online business or website do you find crucial? if something failed, would you lose a day of revenue? how much is that worth to you?
take the time to set up backup services for what you find crucial and then use your dns provider to set up failover records. we only showed you how to do this for a cdn service, but you can use this for web servers, email servers, and a myriad of other online services.
dns is more than just the glue that holds the internet together, it’s an invaluable tool that developers and webmasters need to take advantage of.
Published at DZone with permission of Blair McKee. See the original article here.
Opinions expressed by DZone contributors are their own.