Every frontend developer needs to understand what DNS is
Published: 2022-08-02 06:32:59


The Domain Name System (DNS) is the phonebook of the Internet. When you open up a browser and type google.com the DNS translates the domain name(google) to an IP addresses enabling the web browser to load resources. In absence of the DNS we would instead need to know googles IP(172.217.204.102) to find it.

Each device on the Internet has an IP, and that address is necessary to find the appropriate Internet device - like a street address is used to find a particular home. In order to understand the process behind the DNS resolution, it’s important to learn about the different hardware components a DNS query must pass between.

### There are 4 DNS servers involved in loading a webpage:

1. 
**DNS recursor**  is a server designed to receive queries from client machines through applications such as web browsers. 

2. 
**Root nameserver** - The root server is the first step in translating (resolving) human readable host names into IP addresses. 

3. 
**TLD nameserver**- The nameserver is the next step in the search for a specific IP address, and it hosts the last portion of a hostname (In google.com, the TLD server is “com”).

4. 
**The authoritative nameserver** is the last stop in the nameserver query. If the authoritative name server has access to the requested record, it will return the IP address for the requested hostname back to the DNS Recursor (the librarian) that made the initial request.

**Recursive DNS resolver**

The recursive resolver is the computer that responds to a recursive request from a client and takes the time to track down the DNS record. It does this by making a series of requests until it reaches the authoritative DNS nameserver for the requested record (or times out or returns an error if no record is found). 

Recursive DNS resolvers do not always need to make multiple requests in order to track down the records needed to respond to a client; caching is a data persistence process that helps short-circuit the necessary requests by serving the requested resource record earlier in the DNS lookup.

**Authoritative DNS server**

Put simply, an authoritative DNS server is a server that actually holds, and is responsible for, DNS resource records. It allows the web browser making the request to reach the IP address needed to access a website or other web resources.In instances where the query is for a subdomain such as blog.google.com, an additional nameserver will be added to the sequence after the authoritative nameserver, which is responsible for storing the subdomain’s CNAME record.

**The 8 steps in a DNS lookup:**

**Step 1:**
1. A user types ‘google.com’ into a web browser and the query travels into the Internet and is received by a DNS recursive resolver.

**Step 2:**
2. The resolver then queries a DNS root nameserver (.).

**Step 3:**
3. The root server then responds to the resolver with the address of a Top Level Domain (TLD) DNS server (such as .com or .net), which stores the information for its domains. 

**Step 4**
The resolver then makes a request to the .com TLD.

**Step 5**
The TLD server then responds with the IP address of the domain’s nameserver, example.com.

**Step 6**
Lastly, the recursive resolver sends a query to the domain’s nameserver.

**Step 7**
The IP address for example.com is then returned to the resolver from the nameserver.

**Step 8**
The DNS resolver then responds to the web browser with the IP address of the domain requested initially.

Once the 8 steps of the DNS lookup have returned the IP address for google.com, the browser is able to make the request for the web page:

**Step 9**
The browser makes a HTTP request to the IP address.
**Step 10**
The server at that IP returns the webpage to be rendered in the browser.


![DNS Lookup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w0aiogdvulq85eet97x2.PNG)

**What is a DNS resolver?**

The DNS resolver is the first stop in the DNS lookup, and it is responsible for dealing with the client that made the initial request. The resolver starts the sequence of queries that ultimately leads to a URL being translated into the necessary IP address.

**What are the types of DNS queries?**

**Recursive query **- In a recursive query, a DNS client requires that a DNS server (typically a DNS recursive resolver) will respond to the client with either the requested resource record or an error message if the resolver can't find the record.
**Iterative query** - in this situation the DNS client will allow a DNS server to return the best answer it can. If the queried DNS server does not have a match for the query name, it will return a referral to a DNS server authoritative for a lower level of the domain namespace. The DNS client will then make a query to the referral address. This process continues with additional DNS servers down the query chain until either an error or timeout occurs.
**Non-recursive query** - typically this will occur when a DNS resolver client queries a DNS server for a record that it has access to either because it's authoritative for the record or the record exists inside of its cache. Typically, a DNS server will cache DNS records to prevent additional bandwidth consumption and load on upstream servers.

**What is DNS caching? Where does DNS caching occur?**

The purpose of caching is to temporarily stored data in a location that results in improvements in performance and reliability for data requests. DNS caching involves storing data closer to the requesting client so that the DNS query can be resolved earlier and additional queries further down the DNS lookup chain can be avoided, thereby improving load times and reducing bandwidth/CPU consumption. DNS data can be cached in a variety of locations, each of which will store DNS records for a set amount of time determined by a time-to-live (TTL).

**Browser DNS caching**
Modern web browsers are designed by default to cache DNS records for a set amount of time. The purpose here is obvious; the closer the DNS caching occurs to the web browser, the fewer processing steps must be taken in order to check the cache and make the correct requests to an IP address. When a request is made for a DNS record, the browser cache is the first location checked for the requested record.

In Chrome, you can see the status of your DNS cache by going to chrome://net-internals/#dns

**Operating system (OS) level DNS caching**

The operating system level DNS resolver is the second and last local stop before a DNS query leaves your machine. The process inside your operating system that is designed to handle this query is commonly called a “stub resolver” or DNS client. When a stub resolver gets a request from an application, it first checks its own cache to see if it has the record. If it does not, it then sends a DNS query (with a recursive flag set), outside the local network to a DNS recursive resolver inside the Internet service provider (ISP).

**This content is quoted from [](CloudFlare)https://www.cloudflare.com/learning/dns/what-is-dns/**