This paper will discuss three issues:

  • Why do we have DNS
  • When do DNS queries take place
  • How DNS works

DNS is the abbreviation of Domain Name System. It is very simple to query the corresponding IP address according to the Domain Name.

Why do we have DNS

TCP/IP convention Each network request packet identifies the recipient by IP address. It’s the same as when you make a phone call and need to know the number.

Because IP addresses are hard to remember, there is a layer of domain name mapping.

Why not design it to acknowledge recipients directly by domain name?

In iPv4, IP addresses are four numbers from 0 to 255, two to the eighth, eight bits, and one byte. It’s only 4 bytes in size. If strings are supported, even with the simplest ASCII encoding, a character is 1 byte. A common domain name is usually a dozen characters long and can support up to 255 characters, which is many times larger than the IP representation.

Some people may feel that even though it’s many times larger, it doesn’t matter if it’s just a request that’s a few bytes larger. Another misconception is that TCP/IP is just a protocol, and there are many different devices involved in networking and communication. Let’s say I send a 5 KB string, and the middle of it might go through fiber optics, or it might go through early copper cables. The bandwidth of different connection materials varies greatly. It is possible that the maxSize of a packet can only be 1 KB (MTU at the IP layer). During communication, data at the application layer is divided into many packets according to the bandwidth of communication devices. An HTTP request does not send only one packet; it can be broken into many packets at a time. So if you use a domain name, the amount of data that the network requests will increase a lot.

When do DNS queries take place

Now we know that the last time we communicated we got an IP address through DNS. When and by whom did we enter a URL into the browser?

The browser gets the request and passes it off to the operating system, which passes it off to the network adapter, which, as the real person doing the work, sends the packet out over the link-layer protocol.

DNS predates TCP/IP. As mentioned earlier, when you get to the TCP/IP stack, you already have IP addresses. Why not define DNS in TCP/IP? TCP/IP handles all network requests, so if you find a domain name, parse it. That’s the beauty of architectural design. Think of the scene in our real life, you want to send a gift to a distant friend, the receiver comes, he will care about how you check the address of the recipient? Does he provide an address based on the recipient’s name? It’s a good thing he did it. Not because it’s not their core business. Similarly, TCP/IP is only responsible for communication, not IP address query.

The truth is DNS queries made by the operating system. Therefore, you can configure the DNS server in the network Settings of the operating system. The operating system also manages the DNS cache. DNS Mapping for iOS Network Request Optimization:

On iOS, the DNS cache will expire after 24 hours. Switching back to airplane mode, switching on and off, and resetting network Settings will also cause the DNS cache to be cleared. So typically the next day the user opens your app and experiences a full DNS resolution request.

How DNS works

In a simple version, there’s a server at the center of the world, and in that server there’s a database, and in that database there’s a table, and in that table there’s a record of domain names and IP addresses. I’ll give you whatever server you want.

If the product manager describes this as what it should be, then it will say that this is a small requirement and when it will be available.

DNS doesn’t just look up IP

In addition to the A record of domain name resolution. It also includes the MX (Mail eXchange) Mail server address and the alias CNAME record. In fact, there are many other functions, such as reverse lookup domain name by IP address (PTR), query domain name DNS server IP address (NS).

It’s not a server, it’s a distributed server

There are so many domain names on the Internet that it is obviously impossible to keep them all in one server. There are so many new and modified domain names in the world every day. If all of them go to one server to do IO, the performance will not be able to carry.

DNS is a hierarchical database structure.

For example, a domain name, live.apple.com, is divided into four layers: root domain, com, Apple, live. The root domain name is denoted by., because the last domain name. Can be ignored, all too often forgotten, the full domain name representation is live.apple.com.

If we hold the Apple domain name, we can control the sub-domain name under apple. There are two control methods. One is to directly register the sub-domain name together with apple domain name and record it in com domain name server. One is the self-built name Server, which is the NS record mentioned earlier. At this time to com domain name query apple, will return your own configuration of the name server server address, resolution by your own control.

The self-built name Server is usually not satisfied with the NS performance of the domain name vendor. DNS Basics says:

Although TLD registrars have their own Nameserver, the nameserver provided by registrars is not professional and cannot meet the needs of enterprises in terms of performance and stability. At this time, enterprises need to build their own high-performance Nameserver, such as adding intelligent parsing function. Improve the quality of service by allowing users in different geographies to access the nearest IP address.

Root DNS server

Why does the DNS server know the address of the root DNS server? The address of the root DNS server is fixed. Currently, there are 13 root DNS servers in the world, and the 13 records are persisted in each DNS server.

Why 13? Because DNS queries use UDP, UDP has a maximum effective size of 512 bytes in IPv4. To ensure that all root domain names are included in a packet, you can only limit the number to 13. The root domain name server is a single letter. In mainland China, six DNS mirrors in root domains F, I, J, and L (L has three mirrors) are providing services.

Fortunately, using anyCast technology to set up a mirror server solves this problem and increases the number of actual running root DNS servers considerably. As of November 2017, there were 800 root DNS servers in operation worldwide.

Smart parsing

If a domain name has multiple IP addresses, the nearest IP address is returned when you query the IP address of the domain name. Due to the low bandwidth between different operators in China, it is a disaster for telecom users to access Unicom’s IP, and intelligent DNS resolution can solve this problem.

Intelligent resolution relies on THE EDNS protocol, which is a DNS extension protocol drafted by Google. It is relatively easy to modify. If the origin client IP address is added to the DNS package, Nameserver can return the server IP address closest to the client based on the client IP address.

The latest domestic support for EDNS is DNSPod, DNSPod is a popular domestic domain name resolution manufacturers, many companies will use DNSPod to accelerate the domain name, it has been acquired by goose factory.

DNS intelligent resolution is also used in load balancing and CDN.


Reference:

  • Ruan Yifeng: Introduction to DNS principles
  • Principle of DNS and CDN and optimization scheme in network processing
  • DNS Basics
  • IOS network request optimized DNS mapping
  • A DNS cache disaster

Welcome to my micro blog: @Zhuo who has no story

Nuggets blog: juejin.cn/user/192600…

If you want to talk to me more closely, you can also join my little secret circle: The Programmer’s Survival Guide