Skip to main content

Building Blocks in JS

What is the DNS?

What is the DNS? Well to understand this, let’s revisit the good old days when we had a phonebook!

Do you know what a phonebook is? A phonebook is a book or an application that lists people's phone numbers/contact numbers in a certain region.

Back in the day (I am sure the millennials would connect), there used to be a huge book, with thousands of pages where the contact numbers, usually the landline numbers of the people were listed by name and region. Imagine this to be like a dictionary for phone numbers, just like a dictionary lists words alphabetically from A to Z, a phonebook lists the phone numbers.

What is the DNS?

DNS, meaning Domain Name System is like a phonebook of the internet. It resolves human-readable domain names to machine-readable IP addresses.

What is an IP address?

An IP (Internet Protocol) address is a unique identifier assigned to each device connected to a network. It functions much like a postal address does in real life, allowing data to be sent to and received from the correct devices.

Imagine you live in an apartment building. Each apartment has a unique number, which helps in delivering mail to the correct apartment, correct? Similarly, in a network, each device (computer, smartphone, printer, etc.) has a unique IP address that ensures data is sent to the right place.

I heard "What is a Domain?", sure, so - A domain name is a unique, easy-to-remember address used to access websites, such as ‘google.com’, and ‘instagram.com’. This is a human-readable addresses that lead to a particular website on the Internet.

Definition: A domain name is a string of text that maps to an alphanumeric IP address, used to access a website from client software.

For example, in a house with 4 bedrooms, a kitchen, a hall, etc, we could call the hall as room -1, the kitchen room 2, and the bedrooms numbered 3,4,5 and 6, right? But is it easy to remember? No! The conversations would go like, hey, is the phone in room 6? It's funny and hard to remember at the same time!

Similarly, on the Internet, would you prefer saying, "Hey, let's connect on Instagram?" or "Hey, let's connect on 157.240.208.174?", firstly, I would not remember this after a while, but wouldn't it be so hard to remember all the 100s of the IP address of the websites that we visit daily? That is why we need the DNS!

How does the DNS work?

DNS resolves human-readable domain names to machine-readable IP addresses.

Every domain is associated with an IP address and since it is easy to remember the domain names rather than the IP address we usually search the domain such as microsoft.com, google.com, etc. rather than the actual IP address associated with it.

All of us search for websites day-in day-out right? but do you know what happens under the hood when you search for a domain?

When the client sends a request to reach a domain such as www.microsoft.com, the following steps are performed:

  • The client's request is received by a DNS resolver. DNS resolvers could be popular resolvers such as Google's 8.8.8.8 or Cloudflare etc. A DNS resolver is a specific type of DNS server responsible for translating domain names into internet protocol (IP) addresses. A type of DNS resolver is the recursive resolver. DNS recursor has a cache where it stores the domain to IP mapping ( in simple words ) to avoid further calls to servers each time which saves computation power. If the mapping has the IP, it will be returned to the requestor.

Imagine someone asks you a question, what is 12345 times 124 and you find the answer to be 15,30,780. You know that this friend of yours forgets fast and might ask the same question again, what do you do? Write it down so that you don't have to actually multiply it again, right? Now that is why the DNS resolver "remembers" it as well!

  • If the cache already has the requested IP address, the recursor returns the IP to the requestor. And if the Cache does not have it, it requests the right Authoritative NameServers.
    • There are 3 kinds of Name servers.
      • Root name servers
      • Top-level Domain name servers ( TLD )
      • Authoritative nameServers.
  • The recursor first makes a call to the Root nameServers. There are 13 root name servers kept strategically placed around the world. For now, let's say that "something" called "Anycast" helps us reach the nearest one from where the request is made. The root name servers hold the IP addresses of the Top-level domain name servers. Some examples of TLDs are .com, .org, .edu, .eu, etc. And this is returned to the recursor.

So imagine, you need detergents, what do you do? The first thing we would do is to check our store room for an existing detergent bag, which is like the recursive resolver cache from the previous step, and if not, the next step to be done is to go to the nearest supermarket, not the one that is a 100 miles away, right? The nearest one! Similarly, we go to the nearest root name server from our location.

Now, imagine you've reached the supermarket and you observe many racks kept segregated by type in the supermarket. One for cereal, another for detergents, and so on. Similar are the TLDs in the world of DNS.

  • The recursor now hits the right TLD whose IP address is returned in the last step. The top-level domain name servers have the IP addresses of the Authoritative name servers and this is again returned to the recursor.

Now, among the other rows like the cookies row, and the cereals row, you find the detergents row, the one you need, and hope to find SurfExcel in!

  • The recursor now requests the Authoritative name server asking for the IP of the domain. If it is present, it returns the IP to the recursor. The recursor then performs the caching mechanism (so that these steps do not get repeated for a similar request. ) and returns it to the browser. Now the browser has the IP address and it can load the necessary page.

Now you need SurfExcel! So if it is present you add it to your cart and take it back home and if there is an extra bag ( which you got from the buy 1, get 1 free offer, you fill your store room for a spare )

Some additional advanced steps happen under the hood too!

  • Browsers cache the IP address of a website for a limited period, if the cache has the IP, the browser requests to load the page using the IP address, and all the steps starting with the recursor are skipped.
  • If the browser does not have the IP, the browser reaches the OS-level DNS resolver to check if it has the record. This is called the "stub resolver". It checks if the requested host to the IP address translation is present or not. Remember that this is the last place to look for before the recursor is requested for the IP.

Great, with that understanding of DNS, let's proceed!