This series of articles will focus on optimization, but will also mention what steps are taken.

Simple understanding of DNS system

CNAME

1. A record

Address records can be understood as mappings between domain names and IP addresses. These mappings are A records.

Domain name: www.baid.com ----> 111.111.111.111 // Just assumingCopy the code

2, CNAME

Multiple Cnames can correspond to the domain name recorded by A, for example:

www.bai1.com ---> www.baidu.com
www.bai2.com ---> www.baidu.com
www.bai3.com ---> www.baidu.com
Copy the code

DNS IP search process

The domain name will go to the DNS server to look up the IP and then cache it. The next request will be in the cache, but the first request will still take a lot of time, how to optimize?

Before we optimize, let’s understand how DNS lookup works. And then you can figure out how to optimize.

  • First, the browser will look it up from its DNS cache (Chrome ://net-internals/# DNS).
  • The browser will look for the DNS cache in the operating system (window: run ipconfig/displaydns) (NSCD cache service on Linux)
  • Find it in the computer host file.
  • Request local domain name server (such as: Ali cloud server), 80% by this step is over (you apply for a domain name, go to Ali cloud, you will definitely write the IP that the domain name points to).
  • If none of the above sequential lookups are successful, you need to iterate to the root DNS server.

Root DNS server iteration request:

  • Our actual website is actually: www.baidu.com. (using Baidu as an example), you will find that there is a dot (.) after com.
  • How to parse the IP: order is. — — – >. Com. — – > baidu.com – > www.baidu.com.
  • They represent: root DNS -> top-level DNS -> secondary DNS -> tertiary DNS
  • Each request carries: www.baidu.com. Then slowly find the IP and return.

Front-end DNS optimization

1. Reduce the number of DNS queries

Solution: Put all the requested resources under the same domain so that a single DNS lookup is required to access the site.

But there is a problem: HTTP/1.1 recommends that clients only have a certain amount of parallelism per domain (he recommends 2). Then there will be queues when downloading resources.

Final approach: Use at least 2 domains, but no more than 4 domains, within a site to provide resources.

2. DNS preresolution

When an HTML file is loaded, it will automatically parse the href link of the A tag to the IP address and cache it.

// index.html
<html>
    <a href="test.baidu.com"></a>
</html>
Copy the code

This code, when loading the HTML file, preparses the A tag href and caches the IP. When you click the A TAB, DNS resolution is not required.

Problem: HTTPS does not automatically preparse.

Solution: Need to manually add the entry page throughout the site.

Add a single parse:

<link rel="dns-prefetch" href="//img.alicdn.com"></link>
Copy the code

Parse all links:

<meta http-equiv="x-dns-prefetch-control" content="on">
Copy the code

CDN

The DNS mentioned above is the process without the introduction of CDN. If a CDN is used, the process changes.

What is CDN?

Full name of CND: Content Delivery Network. It can redirect the user’s request to the nearest service node according to the comprehensive information such as network traffic and the link of each node, load, the distance to the user and response time.

Objective: to solve the problem of Internet network congestion and improve the speed of user’s request to visit the website.

How does CDN achieve optimization?

A typical CDN system consists of the following three parts:

  • Distribution service system

The most basic unit of work is the Cache device, which is responsible for directly responding to the end user’s access request and quickly providing the local cached content to the user. The cache is also responsible for synchronizing content with the source site, retrieving updated content and content that is not locally available from the source site.

  • Load balancing system

It is mainly responsible for scheduling access to all users who initiate service requests and determining the final actual access address provided to users. The two-level scheduling system is divided into global load balancing (GSLB) and local load balancing (SLB). GSLB determines the physical location of the cache that provides services to users based on the principle of proximity based on optimal judgment (such as load and response data) of each service node. SLB is responsible for device load balancing within nodes.

  • Operation management system

It is divided into operation management subsystem and network management subsystem, which is responsible for the collection, sorting and delivery work necessary for the interaction between business level and external system. Including customer management, product management, billing management, statistical analysis and other functions.

CDN usage and process

1. Use of CDN

Use is actually very simple, just need to modify their DNS resolution, set a CNAME to point to the CDN service provider.

2. The overall process of DNS and CDN

  1. First, the browser looks it up from its OWN DNS cache.
  2. The browser looks it up from the OPERATING system’s DNS.
  3. Look in the computer’s host file.
  4. Request local domain name server (for example: aliyun domain name provider’s server).
  5. If you find that you have configured CNAME, find the corresponding domain name (for example: sunkuangdong.com –> sun.com), notify the browser to request to change the found domain name (sun.com). Repeat steps 1 to 3 above.
  6. Then request the local DNS server, find the corresponding configuration of this domain name, (for example: sun.com –> 111.111.111), and return the IP address to the browser.
  7. The browser obtains the IP address (actually the ADDRESS of the CDN load balancing server) and continues to request. The CDN selects a load balancing device near the user based on the USER’s IP address and the CONTENT URL requested by the user, and tells the user to initiate a request to this device.
  8. The LAN load balancer returns the IP address of a cache server to the global load balancer.
  9. The GLOBAL load balancing device returns the IP address to the user.
  10. The user finally makes a request to the cache server. The cache server responds with the data, but does not request it from the upper-level cache server. The source server, which traces back to the site, pulls the content locally.

The DNS and CDN

If CDN is used, DNS resolution time increases.

Reference article:

First of all, SORRY for not contacting the big guys in advance, because the location of the private message was not found…

Then express thanks to each big brother ~~~, each big brother correct.

Sunny 663: juejin.cn/post/685457…

Hpoenixf: juejin. Cn/post / 684490…