DNS & GoLinks
Why DNS, suddenly?
GoLinks, a URL shortener service where if you were to input a short link, e.g. go/docs
in the URL of your web browser, you would be brought to the long link, e.g. your-company.intranet.org/very-long-documentation-link
that was hidden behind the short link.
After coming across a self-hosted GoLinks service, I wondered how go/docs
actually worked. The url didn’t even contain any of the domain suffixes that I am used to like .com
, .sg
or .org
.
I guessed that DNS had a part to play, and confirmed it after looking at the setup guide of GoLinks.
You have to add an A record
Hmmm. That sounds like DNS alright, and I know what DNS does! It translates easy to remember names like www.example.com
to its IP address. Our web browser contacts a DNS server whenever we try to access something like www.example.com
to fetch its corresponding IP address.
What are A records?
A records? It’s a DNS thing… What my reply would be, if I were to be asked about it. Using this little run-in with DNS, I decided to stretch my understanding for a bit.
DNS lookup
Although we refer to them usually as just DNS servers, there are actually 4 types.
- Recursive resolvers
- Root nameservers
- TLD nameservers
- Authoritative nameservers
When a web client wants to resolve www.example.com
, it queries a DNS recursive resolver (e.g. Google’s 8.8.8.8
). The DNS recursive resolver could respond with a cached result if it has recently answered another www.example.com
query. If not, it will:
- Query a root nameserver (there are 13 in the world) for the .com top-level domain (TLD) nameserver
- Query the .com TLD nameserver, for the authoritative nameserver responsible for the domain example.com
- Query the example nameserver for the IP address of
www.example.com
and returns it to the web client
Types of DNS records
NS records - Records that indicate which DNS nameserver will know IP address of the hostname.
A records - Records that map a hostname and its corresponding IP address. www.example.com
to 93.184.216.34
There are more (i.e. CNAME, MX, etc) but we don’t need them here.
With the DNS lookup flow and the two types of DNS records in mind, what do the various touchpoints (e.g. the root nameserver, .com TLD nameserver, ..) have to contain?
DNS Records
The root nameserver should contain NS records of the .com TLD nameservers
The .com TLD nameserver should contain NS records of the example.com authoritative nameservers
The example.com nameserver should an A record of www.example.com
Using dig
dig +trace ww.example.com @8.8.8.8
allows us to follow how a DNS query to 8.8.8.8 for www.example.com gets resolved from the start to the end. The trace can be seen in the eyes of the DNS recursive resolver 8.8.8.8.
Let’s verify this trace with our understanding from before.
We see that we get first a list of root nameservers. This are stored in a roots hint file in the DNS recursive resolver 8.8.8.8
.
We then query the root nameserver h.root-servers.net
for the .com TLD nameservers
Next, we query a .com TLD nameserver f.gtld-servers.net
for the NS record of the example.com authoritative nameserver
Finally, we query the authoritative nameserver a.iana-servers.net for the A record of www.example.com
, getting 93.184.216.34
back.
Why is there an extra . at the back?
Adding a . at the end makes the domain name absolute. I think it is another rabbit hole that I would want to avoid for now, but here is my way of thinking about it.
If we were to look at a domain name wwww.example.com, we could say that the domain name (from the right to the left) represents the domain name records you have to traverse to resolve it. However, the query does not start with the records in the .com TLD nameserver, but the root nameserver. Hence, by including the trailing . as in www.example.com.
, it accurately represents the traversal of domain name records to resolve it.
GoLinks
Back to GoLinks. This is why you need an A record in your authoritative DNS server for the go
in go/docs
to resolve to the GoLinks server so that the GoLinks server can redirect users to the long url given the short url.
References
https://www.cloudflare.com/en-ca/learning/dns/dns-server-types/ https://superuser.com/questions/715632/how-does-dig-trace-actually-work https://github.com/GoLinks/golinks