How DNS works?
In order to understand the process, let's have some background knowledge.
Let’s say, If you want to access a site (e.g. webdevelopertuts.com), you only type
webdevelopertuts.com and you don't know the IP Address of that server where this domain/site is hosted. But as said, every server is identified by their IP Address. So, we need the name to be translated/resolved to their corresponding IP Address in order to know the server where this site is hosted. There is a process to resolve domain name into an IP Address, which is known as dns resolution. This also answers our two questions: why DNS resolution is necessary and how DNS works?
To understand this process of resolution, you should have understanding of some of the terms that are described in the article What is DNS and DNS hierarchy.
Now let’s start by taking an example and see how the domain name is resolved into an IP Address. The diagram below will be very helpful in understanding.
A user types a domain name (www.webdevelopertuts.com) into a web browser and press Enter.
The request goes to the nearest Local DNS Server to find the IP Address of the typed domain name.If the IP Address is found then it is returned back to the requesting computer/client Otherwise
The local DNS server send request to the root servers to find the IP Address of the asked domain name.
Local Name Server is the next nearer DNS Server to you that resolve IP Address of domains, if it contains one. If it finds the IP Address of that domain then it sends back that IP Address to browser.
The root servers will in return send the IP Address of the TLD Servers to local DNS Server.
The Local DNS Server will then request to top level domain (TLD) Server i.e.
.comfor the said domain name i.e.
The TLD Server will return the IP Address of the authoritative nameserver which has the IP Address stored in it.
The local DNS Server will then request the authoritative nameserver to provide the IP Address of the domain name.
The authoritative nameserver will provide the IP Address of the needed domain to local DNS Server. The local DNS Server will store the IP Address into its memory (cache).
The local DNS Server will then provide the IP Address of the domain to the computer which initiated the request. The browser will then save the IP Address into its cache for next request to the same domain.
The browser or the client computer will then directly query/request the domain name i.e. webdevelopertuts.com for its webpage.
Now the server i.e.
webdevelopertuts.comwill provide the requested page to the client computer.
There is another action that took place during the above steps that I didn't tell you. What was that? Let's see it.
In the process of finding the IP Address of the actual domain, it was jumping to and fro from DNS servers to browser. Wherever it found the DNS server and local name server, it left a copy of the IP address against the domain name and lastly it stored that IP address in cache of browser too, so that it should not go all the way across to root servers to find the IP Address of that domain again.
If the client computer asks for the same domain's webpage again, then the browser will skip the steps from 1 to 8 and will directly query the domain's webpage because now the IP Address has been stored in its cache.
If the client computer clear its cache then the request will again go to the local dns server and that dns server will provide the IP Address of the domain from its cache (if present), otherwise it will repeat all the steps from 1 to 8.
There is another question that might be in your mind and that is:
Why browser takes a little bit more time in fetching the page of the firstly visited domain
The reason is that, first time it goes all the way across from root server to TLD and then finding the actual domain and then storing the IP Address in the browser cache. But in the 2nd request, it has the IP Address of that domain and it doesn't have to go all the way across from root server to the actual domain. That's why it doesn't take more time in fetching the page after the 1st request.