The above question is most of the time asked in the interviews.
During our engineering courses, we often do DSA or backend/frontend development which is mostly concentrated towards building services or pages. But we miss out on how everything is linked and working underneath, how browsers work, how a page is rendered, how connections are made and so on.
With the help of this article, I will try to explain the question What happens when you enter a URL in the browser?
So the browser performs these 3 steps when you type a URL and hit enter-
- Find the IP address(location) of the server hosting the site- The browser needs to figure out which server on the Internet to connect to. The domain will have an IP address mapped to it.
- Make a connection to this server - The browser uses the IP address retrieved to connect to the server, it makes a TCP connection.
- The browser sends the HTTP request to the server to get the data - The browser sends an HTTP request to get the required data to be shown on the website. The server then returns the response and the browser displays this response.
1. Find the IP address of the server hosting the site
The browser follows a series of steps to get the IP address as per the below flow diagram. The IP address resolution follows checking caches at different levels. If the Domain to IP mapping is present in any cache level, the IP address is returned and is used to make a connection with the server.
Here is an explanation of the above flow -
The IP address is firstly checked in the different caches. The caches are -
- Browser cache
- Operating system cache
- Router cache
- ISP DNS server cache (4.1)
If the IP address is not found in any of the caches then the ISP will start the DNS lookup(4.2) by requesting other DNS servers to get the IP address.
Here are the steps involved in DNS lookup -
- Firstly it will request the ROOT Nameserver with a query hey, what's the IP address of www.<website>.com? The ROOT Nameserver will return the IP address of the .com Nameserver since the URL contains a .com top-level domain.
- Next, it will request the .com Nameserver(called Top Level Domain Nameserver) with the same query as above. This server will then return the IP address of the <website>.com Nameserver.
- Finally, it will make a request to the <website>.com Nameserver(called Authoritative Nameserver) with the same question. At this point, the server will return the IP address of www.<website>.com. Now our browser knows the IP address of the server hosting the www.<website>.com.
2. Make a connection to this server
After getting the IP address of the server, the browser makes the connection with the server using the IP address. The browser creates a TCP connection with the server. TCP stands for Transmission Control Protocol. This connection is created using the TCP 3-way handshake mechanism.
It is called 3-way handshake because it involves transfer of 3 messages.
- The client sends the SYN (Synchronization Sequence Number) message. This is a 32-bit random number that the client generates and sends.
- The server receives the SYN message and responds with a SYN+ACK message. The server sets the ACK Flag to true indicating that it has received the SYN message sent from the client, and also sends an Acknowledgement number which is received SYN+1. The server also generates a new 32-bit random number as part of the SYN message. The server sends the SYN+ACK message which is [SYN = new SYN, ACK = old SYN + 1, ACK_FLAG = true].
- The client after receiving the SYN+ACK message will respond with an ACK message which is received SYN + 1.
- After receiving the ACK message from the client, a connection is established between the client and the server.
In case HTTPS is used, a TLS handshake takes place to secure the communication. I'll cover them in a separate post.
3. The browser sends the HTTP request to the server to get the data
Now once the connection to the server is made, browser follows the HTTP(s) protocol and sends HTTP requests to get the contents of the website. The HTTP requests contains data that helps the server to figure out what resources/data the client has the requested.
A HTTP request contains the following -
- The method type like GET, POST, PUT, DELETE etc
- The path to the resource(data)
- HTTP version to use.
- Headers
- Body
Server processes the HTTP requests and returns the response. This response is then rendered by the browsers.
This is how in the backend everything works, from typing a URL into the browser to IP address resolution to a TCP handshake and finally HTTP methods to get the data.
Please share it with others, if you liked the content.