Everyday we visit tens, even hundreds of websites. We type in a URL, hit enter and, like magic, a website appears. Yes, we know that the page is made up of HTML, this little piece had to go fetch something from the database to display my name and all the code someone had to write in order for the site to function properly. But do you ever think about what happens in the small amount of time between when you hit ‘enter’ and when the browser starts to render the page?
Domain Name System
Every website has an IP address of the computer it is running on linked to it. The DNS (Domain Name System) is the infrastructure that stores the IP addresses of every website. The DNS also maintains various types of records to make it possible to find the IP address of the URL you just entered into the browser. Below are only a couple of the more commonly used records the DNS maintains. There are also records with data related to emails, reverse DNS lookup and more.
Address (A) Record
The A Record points directly the IP address of a given domain name.
Canonical (CNAME) Record
A CNAME Record is an alias for another record. This record is used when multiple domains should point to the same IP address.
DNS Lookup Walkthrough
To understand how these records are used in a DNS lookup lets take an example URL and walk through the steps. Say we have a URL testsite.com and unknown to us it actually redirects to example.com.
- After typing testsite.com into the address bar the browser sends a request my local DNS server for the IP address.
- My local DNS server doesn’t actually know the answer so it goes up one level and asks its DNS server.
- This DNS server does have an answer but it is a CNAME Record. The CNAME record says to look for the URL example.com. This DNS server sends this answer to my DNS server.
- My DNS server caches this answer so next time it knows to look for example.com when a request comes in for testsite.com.
- My DNS server sends the answer back to the browser.
- Since the browser doesn’t have an IP address yet, it sends a request back to my local DNS server for the IP address of example.com.
- This time my local DNS server does know the answer and sends the IP address stored in the A Record to the browser.
- The browser can now use the IP address to find where the data is stored and render the webpage.
Time To Live (TTL)
Each DNS record also includes a TTL, or Time To Live, property. This property specifies how long a requesting DNS server should cache the answer it is receiving. If you have ever run into situations where, after changing the IP address of a web site, some areas see the change sooner than others – the TTL setting and caching is the reason. DNS servers cache answers to a domain name request and will not always reflect changes until this cache expires and sends a new request up the server chain.
Sometimes it is good to stop and take a deeper look at things that even as developers we take for granted that “just happen”. Learning something new can be a fun, rewarding experience and will only make you a better developer.