Android performance optimization network optimization DNS and HttpDNS knowledge detailed explanation

Android performance optimization network optimization DNS and HttpDNS knowledge detailed explanation

[[413589]]

This article is reprinted from the WeChat public account "Android Development Programming", the author is Android Development Programming. Please contact the Android Development Programming public account to reprint this article.

Preface Summary

When the App accesses the network, DNS resolution is the first step of the network request. By default, we use the operator's LocalDNS service. According to statistics, it takes 200-300ms on a 3G network and 100ms on a 4G network.

Slow resolution is not the biggest problem of LocalDNS. There are some more serious problems, such as DNS hijacking, inaccurate DNS scheduling (caching, forwarding, NAT) leading to performance degradation, etc. These are the problems that network optimization should solve most.

If you want to optimize DNS, the simplest and most mature solution now is to use HTTPDNS.

Let’s talk about DNS and HTTPDNS today.

1. What is DNS

  • DNS (Domain Name System) is the abbreviation of Domain Name System, which is a system management agency of an organization, maintaining the corresponding relationship between the IP and host name (domain name) of each host in the system;
  • DNS is the Domain Name Resolution System. It should be well known to all developers. To put it simply, the function of this system is to resolve domain names into IP addresses. Every time we make a network request, if it uses a domain name, it is a domain name resolution;
  • An excellent domain name service should be able to meet two requirements: one is to be able to correctly return the IP address, and the other is to be able to return the nearest server IP of the requested domain name according to the network conditions;

2. DNS domain name structure

1. DNS domain name naming

  • Each domain name is a sequence of numbers consisting of letters, numbers, and hyphens (-);
  • The total length of the label sequence cannot exceed 255 characters, and each label can be regarded as a hierarchical domain name;
  • The lowest level domain name is written on the left, and the highest level domain name is written on the right;
  • Domain name service is mainly implemented through UDP, and the server port is 53;

2. Domain name classification

Domain names can be divided into subdomains, and subdomains can be further divided into subdomains of subdomains, thus forming top-level domain names, second-level domain names, third-level domain names, etc.

Top-level domains can be divided into three categories:

  • National top-level domain names: cn, us, uk, etc.;
  • Common domain names: There are 7 common ones, com, net, org, edu, int, gov, mil;
  • Direction domain name: arpa, used to convert IP address to domain name;
  • Domain name server;

3. Domain Name Resolution Process

Two important points about domain name resolution:

  • The host usually uses recursive query to query the local domain name server. The so-called recursive query is: if the local server queried by the host does not know the IP address of the queried domain name, then the local domain name server will continue to send query request messages to other root domain name servers as a DNS client (that is, continue to query on behalf of the host), instead of letting the host perform the next query itself. Therefore, the query result returned by the recursive query is either the IP address to be queried, or an error, indicating that the required IP address cannot be queried;
  • The query from the local domain name server to the root domain name server is an iterative query. The so-called iterative query means that when the root domain name server receives the iterative query request message from the local domain name server, it either gives the queried IP address or tells the local server: "Which domain name server should you query next." Then let the local domain name server perform subsequent queries. The root domain name server usually tells the local domain name server the IP address of the top-level domain name server it knows, and asks the local domain name server to query the top-level domain name server again. After receiving the query request from the local domain name server, the top-level domain name server either gives the IP address to be queried or tells the local server which authoritative domain name server to query next. Finally, the IP address to be resolved is known or an error is reported, and then the result is returned to the host that initiated the query;

The above two points are two important steps in domain name resolution. However, this is not the complete process of resolving the IP address. If the browser has the IP address corresponding to the domain name in the cache, there is no need to request the local domain name server, etc. Let's take a look at the detailed process:

For example, to resolve: the IP address of the domain name www.example.com;

  • Browser cache: When a user accesses a domain name through a browser, the browser first searches its cache to see if there is an IP address corresponding to the domain name;
  • Operating system cache: When there is no IP corresponding to the domain name in the browser cache, the user's computer system hosts file will be automatically checked to see if there is an IP address corresponding to the domain name;
  • Router cache: When there is no domain name corresponding to the IP address in the browser and system cache, enter the router cache to check. The above three points are the client's DNS cache;
  • ISP's (Internet Service Provider) LDNS (Local Domain Name Server): If the above three points do not find the corresponding address, you need to query the local domain name server. For example, if you are using a telecom network, you will enter the telecom DNS cache server for search;
  • Root domain name server: If the local domain name server is not found, the local domain name server will query the root domain name. There are only 13 root domain name servers in the world. After receiving the request, the root domain name server will check the zone file record. If it is not found, it will return the IP address of the corresponding top-level domain name under its jurisdiction. Here, the address of .com is returned;
  • Top-level domain name server: If the root domain name server does not have it, the local domain name server sends a request to the top-level domain name server, and then returns the IP address of the secondary domain name server, which will return the address of .example;
  • Primary domain name server: After receiving the request, the primary domain name server queries its own cache. If it does not find the record, it goes to the next level domain name server to search and repeats this step until the correct record is found.
  • Save the result to cache: The local domain name server saves the returned result to cache for next use, and feeds the result back to the client. The client establishes a connection with the web server through this IP address;

4. DNS security and optimization

1. DNS security issues

  • DNS reflection/amplification attacks;
  • DDOS attacks may cause domain name resolution to be paralyzed;
  • DNS/Domain Name Hijacking: intercepting domain name resolution requests within the hijacked network, analyzing the requested domain name, returning a fake IP address or making the request unresponsive. DNS hijacking is achieved by tampering with the data on the DNS server and returning an erroneous query result to the user;
  • DNS pollution: DNS pollution occurs when a user initiates a domain name resolution request and a server (non-DNS) monitors the marked address accessed by the user, and the server pretends to be a DNS server and sends back an incorrect address to the user.
  • The difference between DNS pollution and DNS hijacking: DNS hijacking modifies the result of DNS resolution, while DNS pollution does not go through the DNS server but directly returns the wrong address;
  • DNS information is modified;

2. DNS Optimization

DNS resolution is a long process, so what are its optimizations?

1. Web page

Before a user requests a link, the browser first tries to resolve the domain name of the link and then caches it.

You can do this:

(1) Set the value of X-DNS-Prefetch-Control to on in the server response to start pre-resolution

(2) In HTML,

(3) Add the link tag in the head:

  1. Such as <link rel= "dns-prefetch" href= "//tj.koudaitong.com/" />

However, the current Chrome browser will automatically prefetch all the DNS with href in the current page. The scenario where you need to manually add the link tag above is: the domain name you visit later is not in all the links on the current page;

Correct way to use the link tag:

  • Manually dns-prefetch the static resource domain name
  • Manually perform dns-prefetch on redirects and requests initiated in js
  • Manually prefetch the new domain name for redirection
  • No need to do manual dns-prefetch for the hyperlinks of the current page

Domain name convergence: It is recommended to place static resources under only one domain name to reduce DNS requests

2. Client

HttpDNS

HttpDNS uses HTTP protocol to directly request port 80 of Alibaba Cloud's HTTPDNS server, instead of traditional DNS protocol to request port 53 of LDNS server. This can bypass LDNS and avoid domain name hijacking and inaccurate scheduling by operators;

5. Introduction to HttpDNS

  • HttpDNS is actually another way to implement DNS resolution. It just changes the domain name resolution protocol from DNS protocol to Http protocol. It is not complicated. Use HTTP protocol to request the 80th port of D+ server instead of the traditional DNS protocol to request the 53th port of DNS server, bypassing the local DNS of the operator, thus avoiding the hijacking and cross-network problems caused by the use of the local DNS of the operator.
  • It is also very simple to access HttpDNS. When using ordinary DNS, when the client sends a network request, it is sent directly, and the underlying network framework performs domain name resolution. When accessing HttpDNS, you need to send an HTTP request for domain name resolution yourself. When the client gets the IP corresponding to the domain name, it sends a business protocol request directly to this IP;
  • In this way, you no longer have to consider the problems that traditional DNS resolution will bring. Because it uses the HTTP protocol, you don’t have to worry about domain name hijacking. Moreover, if you choose a good DNS server provider, it will also ensure that the IDC node with the fastest access speed that users are directed to is connected to HttpDNS.

Summarize:

There are many knowledge points about network optimization. Today we mainly introduce the knowledge points of DNS.

Next time, I will continue to introduce the specific implementation plan of Android network optimization.

<<:  Urgent release! iOS14.7.1 official version update, fix issues

>>:  Apple urgently releases iOS 14.7.1 official version to fix two major bugs

Recommend

Fan support: a way to commercialize entertainment products

In the early stages of an Internet product’s laun...

2018 new Android interview questions from big companies

Preface I started looking at it at the end of las...

Golden rules for creating hit products for new consumer brands

For a long time, many people have had certain mis...

What did the Internet giants say at the Boao Forum?

[[130720]] From March 26 to March 29, the 2015 Bo...

The role of KOL marketing in brand operation and promotion!

What is a KOL? KOL: Key Opinion Leader (KOL for s...

Samsung Mobile Business President: To regain China's top customers are operators

According to the technology website ZDNet, after ...

Check yourself! Come and see if your ID card has been stolen by others!

appendix: Official website of the Credit Informat...

Breaking down the planning logic of big promotion membership activities!

1. Case Study Xiao Ming has been signing in at a ...

B-side product operation skills!

The C-end is for all users and can be used by any...