How does DNS (Domain Name System) work?
What is DNS? How does the browser fetch application content based on the domain name (URL)?
Hey, Happy New Year!
I wish you the best to achieve all your desired goals!
Let’s keep learning and growing, together!
I hope you all enjoyed your holidays. I took a 2-week break from sending out my newsletter to get some things sorted.
Let’s continue from where we left off!
In my previous post, we discussed how we make our application accessible to the public by hosting it on a server. We also looked at some popular web hosting services we can use for hosting our website/application.
When we host our application in a server or cloud, we are essentially storing our code on another machine that will serve our application content to the public.
How does a user know which server to request our application from?
Should we access our app using the machine’s IP address every time?
No. This is where DNS (Domain Name System) plays a vital role.
In this week’s post, we’ll discuss the following:
URL
Domain Name System (DNS)
How is our application fetched when we type our URL in the browser?
URL Components
Let us first deconstruct a sample URL and understand its components. Consider the following URL:
https
→ Scheme
The Scheme determines the protocol used for connection between the browser and our server. In our case, the browser will use the HTTP scheme.productdetails.com
→ Domain
The domain represents the primary domain name of our site.www
→ Subdomain
A subdomain name is an additional information added to the beginning of a domain name. It allows websites to separate and organize content for a specific function. For example, we can also haveblog.productdetails.com
orcheckout.productdetails.com.
/categories/phone/product
→ Path
Path is the specified location in the server where the browser will be requesting our desired resource./iphone
→ Resource
This is the particular resource the URL points to, which will be sent back to the browser as a server response.?color=spacegrey
→ Query parameters
Query parameters are defined set of parameters attached to the end of a URL. It can be used to fetch specific content or actions based on the requested resource.
DNS (Domain Name System)
Domain Name System (DNS) is the phonebook of the internet. Every device connected to the internet will have a unique IP address.
Without DNS, we should remember the IP address for every machine to access the content. It is impossible to remember IP addresses like 198.162.1.1 (in case of IPV4) and 2400:cb00:2048:1::c629:d7a2 (in case of IPV6)
DNS eliminates the need to remember IP addresses to access the contents of the corresponding devices. It maps domain names with the IP address of the device.
What happens when we type in our URL?
Our browser handles the functionality to fetch web content based on the URL we enter. Let us see what the browser does behind the screens once we type in a URL.
1) User enters the URL in the browser
This functionality is common across all browsers. The browser checks the format of the URL and proceeds to step 2.
2) IP Lookup in DNS Cache
Now, the browser checks the DNS cache to see if the domain name to IP address mapping is in cache memory.
If the mapping is available, then the corresponding server IP address is returned to the browser. If not, then the browser proceeds to step 3.
Data is cached at multiple layers of the process to reduce the IP lookup latency. This involves browser cache, DNS cache, ISP cache, etc.
3) IP Lookup in DNS server
If the DNS cache does not contain the IP address mapping for the given URL, the browser looks for the IP address in the DNS server.
The DNS resolver is a machine that performs a series of requests until the records for the given URL is found.
You can read more about recursive DNS resolvers here.
By the end of step 3, the browser will now have the server information to send the request to fetch the website data.
4) Establish TCP connection with the server
With the server information for the URL, the browser now establishes a TCP connection with the server.
If this connection is not successful, the browser immediately shows an error page explaining that it was an unreachable server.
5) Send HTTP request to server
Once the TCP connection is established, the browser send a HTTP request based on the Scheme of our URL.
6) Server sends website content back as response
The server responds to the HTTP request with a 200 status code if it is successful. The response contains the content-type and the HTML response for the browser to render.
I hope this gives an overview of how the URL we type in our browsers links to the application code we host on servers.
Sources:
https://www.cloudflare.com/en-gb/learning/dns/what-is-dns/
https://www.ibm.com/docs/en/cics-ts/5.1?topic=concepts-components-url