A few days ago face byte
👦🏻 : “What happens to the browser from entering the URL to displaying the page?”
👧🏻 : %^&@#^& (How could I not have prepared for this? From the Web to rendering.)
👦🏻 : “You just mentioned DNS, how about DNS query process?”
👧🏻 : “DNS query is a recursive + iterative process…”
👦🏻 : “What about the specific recursive and iterative processes?”
👧 🏻 : “…”
At that time, I had a general process in mind, but the details can not remember, so today I will comb through DNS related content, if there is something wrong, I hope you point out.
What is the DNS
DNS is the Domain Name System. When we enter a URL in the browser, the browser needs to know the IP address of the server in order to send a request to the host name of the URL. For the browser, DNS is used to translate the host name into the IP address. Here are the concepts from Computer Networking: A Top-down Approach:
DNS is:
- A distributed database implemented by a hierarchical DNS server
- An application layer protocol that enables hosts to query distributed databases
In other words, DNS is an application-layer protocol. We send a request that contains the host name we want to query, and it will return the IP address for that host name.
Secondly, DNS is a distributed database. The whole DNS system is composed of many DNS servers scattered around the world. Each DNS server has some data, which can enable us to finally find the IP address corresponding to the host name.
So the DNS lookup process, basically, is to go to these DNS servers and say, do you know what the IP of this host name is? No? Do you know which DNS server to go to? Until I find the IP I want.
Distributed, hierarchical database
What is distributed?
No DNS server in the world has the mapping of all the hosts on the Internet. Each DNS is responsible for only part of the mapping.
What is hierarchy?
There are three types of DNS servers: root DNS server, top-level Domain (TLD) DNS server, and authoritative DNS server. Their hierarchy is shown in the figure below:
Image credit: Computer Networking: A Top-down approach
- Root DNS server
First of all, we need to know what the root domain name is, such as www.baidu.com. Some students may mistake com as the root domain name, but actually com is the top-level domain name. The complete spelling of www.baidu.com is www.baidu.com. That’s the root domain name.
What does the root DNS server do? The next level that manages it, the top-level domain DNS server. By asking the root DNS server, you can know the IP address of the DNS server of the TOP-LEVEL domain corresponding to a host name, and then continue to send query requests to the DNS server of the top-level domain.
- Top-level domain DNS server
In addition to com, common top-level domains include cn, org, and edu. A top-level domain DNS server, also known as a TLD, provides the IP address of its next-level, authoritative DNS server.
- Authoritative DNS server
Authoritative DNS servers can return the final mapping of host-IP.
How these layers of servers interact with each other, we will talk about the specific DNS query process, combined with the query process, it is not difficult to understand the relationship between them.
Local DNS Server
If you’ve seen DNS before, why didn’t the DNS hierarchy in the last section mention the local DNS server? Because, strictly speaking, the local DNS server does not belong to the DNS hierarchy, but it is critical to the DNS hierarchy. So what is a local DNS server?
Each ISP has a local DNS server, such as an ISP in a residential area, an ISP in a university, an ISP in an institution, one or more local DNS servers. When a host makes a DNS request, the request is sent to the local DNS server, which acts as a proxy and forwards the request to the DNS server hierarchy.
Let’s take a look at how the DNS query process works and how the client, local DNS server, and DNS server hierarchy interact with each other through a simple example.
Recursive query, iterative query
As shown in the figure below, suppose that host m.n.com wants to obtain the IP address of host A.b.com by following the following steps:
-
First, the host m.n.com sends a DNS query packet to its local DNS server containing the host name a.b.com that is expected to be translated.
-
The local DNS server forwards the packet to the root DNS server.
-
The root DNS server notices the COM prefix and returns a list of IP addresses for the top level domain DNS server (TLD) corresponding to com to the local DNS server.
I don’t know the IP of a.b.com, but these TLD servers probably do. Ask them.
-
The local DNS server sends query packets to one OF the TLD servers.
-
The TLD server notices the b.com prefix and returns the IP address of the authoritative DNS server to the local DNS server.
I don’t know the IP of a.b.com, but these authoritative servers may know it. Ask them.
-
The local DNS server sends query packets to one of the authoritative servers.
-
Finally, the authoritative server returned the IP address of A.b.com;
-
The local DNS server returns the mapping between a.b.com and the IP address to the host m.n.com. Then m.n.com can send requests to A.b.com using the IP address.
“You talk so much, what about recursion? What about iteration?”
Don’t worry, but recursion and iteration are already part of the process.
The query m.n.com sends to the local DNS server dns.n.com is a recursive query. In this query, the host M.n.com requests the desired IP mapping from the local DNS server in its own name, and the local DNS server directly returns the mapping result to the host.
The following three queries are iterative queries, including the local DNS server sending query requests to the root DNS server, the local DNS server sending query requests to the TLD server, and the local DNS server sending query requests to the authoritative DNS server. All the queries are sent by the local DNS server. All responses are returned directly to the local DNS server.
So the question is, do all DNS queries have to follow this recursive + iterative pattern?
Of course not.
In theory, any DNS query can be either recursive or iterative. All queries in the figure below are recursive and do not involve iteration.
You may have a question: does the TLD know the IP address of the authoritative DNS server?
emmm…
Sometimes the TLD just knows a DNS server in the middle, and the intermediate DNS server is used to find the authoritative DNS server. In this case, the entire query process requires more DNS packets.
DNS cache
In order to get the IP we want faster, DNS makes extensive use of caching technology. The principle of DNS cache is very simple. During a DNS query, when a DNS server receives a DNS response (for example, a mapping between a host name and an IP address), it caches the mapping to the local server. In the next query, the content in the cache can be directly used. Of course, the cache is not permanent. Every mapping record has a lifetime, and once the lifetime has passed, the record should be removed from the cache.
In fact, with caching, most DNS queries bypass the root DNS server, and very few queries need to be sent to the root DNS server.
The interview feeling
The interview harvest is quite big, some things think they understand, think they can say clearly, but when it comes to really say, and no way to comb out a complete description of stumbling, in the interview will be very reduced points.
So don’t be lazy, don’t have a lucky psychology, steadfast learning. ‘.
reference
Computer Networks (7th edition)