Many people will ask you in interviews what exactly happens when you type google.com in your browser? And they want you to go in detail as possible. Or simply: How does dns work?
1. You type google.com into the address bar of your browser.
2. Once you type it, the browser starts looking for the DNS record of the address in the cache to find the corresponding IP address. There are 4 places it checks for cache:
- Browser Cache
- OS Cache
- Router Cache
- ISP Cache (ISP maintains its’ own DNS server which includes a cache of DNS records which the browser would check with the last hope of finding your requested URL)
3a. Before the request going to the ISP, the request goes through your default gateway (Usually on the system). The request is coming from your private address (192.168.X.X), to go outside, NAT comes into picture aka Network Address Translation where the private IP address is mapped to the public IP address and the request is sent out to the ISP.
3b. If the requested URL is not in the cache, ISP’s DNS server initiates a DNS query to find the IP address of the server that hosts google.com.
The purpose of a DNS query is to search multiple DNS servers on the internet until it finds the correct IP address for the website. It is a recursive search since the search will continue repeatedly from DNS server to DNS server until it either finds the IP address we need or returns an error response saying it was unable to find it.
DNS servers are called name servers since they perform a DNS search based on the domain architecture of the website domain name.
The DNS recursor will contact the root name server ( DOT “.”). The root name server will redirect it to .com domain name server. .com name server will redirect it to google.com name server. google.com name server will find the IP address in its’ DNS records and return it to your DNS recursor which will send it back to your browser.
4. The Browser initiates a TCP connection with the server.
Once the browser receives the correct IP address it will build a connection with the server that matches IP address to transfer information. In order to transfer data packets between your computer(client) and the server, it is important to have a TCP connection established. This connection is established using a process called the TCP/IP three-way handshake. This is a three-step process where the client and the server exchange SYN(synchronize) and ACK(acknowledge) messages to establish a connection.
6. Once the SYN, SYN/ACK and ACK connection is established for data transmission, the browser sends an HTTP request to the webserver. The browser will send a GET request asking for google.com web page.
7. The server handles the request and sends back a response. The server contains a web server which receives it’s request and passes it on to its request handler. The request handler is a program that reads the request, its’ headers, and cookies to check what is being requested and also update the information on the server if needed.
8. The server response contains the web page you requested as well as the status code, compression type (Content-Encoding), how to cache the page (Cache-Control), any cookies to set, privacy information, etc. If the server does not contain the requested webpage, it will send a status response to the browser using a numerical code.
9. The browser displays the HTML content. Then it will check the HTML tags and sends out GET requests for additional elements on the web page, such as images, CSS stylesheets, JavaScript files etc.