How DNS works - Part 1
Tags: dns, networking, devops
Published: 2023-12-27 15:13:27

The Domain Name System (DNS) functions as the Internet's equivalent of a phonebook. When humans seek information online, they use domain names such as nytimes.com. However, web browsers communicate with **Internet Protocol (IP) addresses**. DNS plays a crucial role by translating domain names into corresponding IP addresses. The DNS lookup takes place seamlessly in the background for the web browser, requiring no user interaction beyond the initial request from the computer. But let's see how it works.

### How DNS works in the background
When you type a domain name in the browser, the first thing that your DNS resolver look for before reaching DNS servers is checking if there is a match in your :
- **Browser DNS cache** : for example in chrome you can check the DNS cache on _chrome://net-internals/#dns_.
- **Host file** : located in _/etc/hosts_ on Linux and macOS.

If no match is found, it will try to resolve the domain name through a **DNS lookup**, there are 4 DNS servers involved :

**DNS Recursor**: Imagine the DNS recursor as a helpful librarian in a library. When you ask this librarian (server) to find a specific book (IP address) for you, it gets queries from your computer (through applications like web browsers). The librarian, or DNS recursor, then makes additional requests to fulfill your request for information.

**Root Nameserver**: Think of the root server as the first guide in a library that helps you find the right section of books. It's like an index pointing to different racks of books. The root server is the starting point for translating human-readable names (like website addresses) into IP addresses.

**TLD Nameserver**: Picture the top-level domain (TLD) server as a specific section of books in a library. It's like a particular rack. This server is the next step in the search for a specific IP address, and it handles the last part of a website address (like "com" in example.com).

**Authoritative Nameserver**: Envision the authoritative nameserver as a dictionary in a library, where a specific name (hostname) can be translated into its definition (IP address). This nameserver is the final stop in the query. If it has the information you're looking for, it returns the IP address to the DNS recursor (the librarian) that initially made the request.


## The 8 steps in a DNS lookup:

1. A user types ‘example.com’ into a web browser and the query travels into the Internet and is received by a **DNS recursive resolver**.
2. The resolver then queries a **DNS root nameserver**.
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. When searching for example.com, our request is pointed toward the .com TLD.
4. The resolver then makes a request to the .com TLD.
5. The TLD server then responds with the IP address of the **Authoritative nameserver**, example.com.
6. Lastly, the recursive resolver sends a query to the **Authoritative nameserver**.
7. The IP address for example.com is then returned to the resolver from the **Authoritative nameserver**.
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 example.com, the browser is able to make the request for the web page:

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