DNS 101 for developers

DNS stands for “Domain Name System”, but you could google that information yourself and find definition online. What you could not find online is very short and absolutely minimal DNS guide for developers. Here it is.

DNS Records

dns

Everything you should now about DNS boils down to one thing. DNS has records, and DNS server stores those records. Setting proper DNS record would ley you point your domain name(e.g. myblog.com) to your web application, your blog or your web service. Just think of DNS as one big lookup table. Domains names are keys, and server addresses are values.
Most likely you would deal with two types of records: A and CNAME record. learning how these two work, would be enough to successfully host your site or blog.

A record

‘A record’ points to the actual server on the Internet using its IP address. I guess this is the simplest form of DNS record, and it might look something like this:

A | myblog.com | 127.0.0.1

Domain name(myblog.com) is on the left side, and in case of A record, an IP address(127.0.0.1) is on the right side of the mapping. In real world example it would be an actual IP address of the server you are using to host your site, not the localhost’s address.
At this point you should begin to distinguish between domain name(myblog.com) and host/IP address(127.0.0.1). Domain name is just entry within DNS, and host is physical machine attached to the internet, identified by its IP address. Domain name associated with and IP address is called ‘hostname’. Typically there is one-to-many relationship between host(or its IP address) and domain name. One domain name points to one and only one address(if there is no Round-robin DNS in use), but one address could be associated with one or more different domain names. Take for the example two different blogs(myblog.com and someotherblog.com) hosted on save server having only one IP address.

CNAME record

CNAME stands for ‘canonical name’. To be honest I do knot know what that means(and do not care). What I care about is the fact that other than A record, CNAME is the most important type of DNS record, when it comes to hosting your blog. Basically it is an alias from your domain name to some another domain. For example:
me.myblog.com

CNAME | me.myblog.com | minime.someotherblog.com

Now your own domain name(me.myblog.com) points to completely different site and domain. For the end user this is 100% transparent. User sees me.myblog.com in the address bar, but actual content is fetched from minime.someotherblog.com.
But why is CNAME record that important? What would be the use case for this? If you want to host your blog on amazon S3, it is enough to create CNAME record pointing to your S3 bucket. Then you can use this new alias for your blog, not the original bucket’s name. Take for example this blog. It’s hosted on S3 and its bucket’s URL is http://www.brainfuck.rs.s3-website-us-east-1.amazonaws.com/. Yeah I know, it’s ugly. So I just created CNAME record that points www.brainfuck.rs => http://www.brainfuck.rs.s3-website-us-east-1.amazonaws.com/. Now, when you navigate to the www.brainfuck.rs what you actually see is the content of http://www.brainfuck.rs.s3-website-us-east-1.amazonaws.com/. Luckily for me, my business and amazon guys, browser in a no way acknowledges this :D Yes, you are reading this blog from Amazon S3!
But what about brainfuck.rs domain, one wihtout ‘www’ prefix. Unfortunately there is one slight issue with CNAME records. DNS providers usually won’t let you use CNAME with ‘naked’ domains. A naked domain is simply a domain name without the ‘www’ prefix. This means with CNAME record you can use www.myblog.com but not myblog.com. There is very simple and smart solution for this issue and it involves redirecting all request form myblog.com to www.myblog.com. In one the following posts I’ll talk about wwwizer, service that does this for free. Another rule with CNAME is to never use other CNAME as record’s value. This way you could create infinite ‘redirects’ or circular references that would take forever to resolve. Aliased domain name always has to resolve to an IP address.
Feel free to share this article with your colleagues and your team. There is still a lot of fuss about DNS among developers.