Why is CDN designed this way?

2022.06.14
Why is CDN designed this way?

The construction cost of CDN network is still very high. Only large companies will build their own. Generally, we will buy third-party services, and domestic and overseas CDNs are separated. If you mainly serve domestic users, you can only buy domestic CDN services. . With this network of CDN nodes covering the whole country and the world, we can open the website faster.

In the past few decades, computer networks have connected almost all computers in the world. We only need to deploy static resources and dynamic code to the server, and then start the service to listen to a certain port, so that computers all over the world can access the server. website.

However, there is a problem with this. Resources are ultimately transmitted through physical layer network lines and devices. Every time a line or a network device passes through, it takes some time. Therefore, the farther the client and the server are, the slower the website opens.

It's like if you bought something from Hainan, if you are in Guangzhou, you may receive it soon because the transmission distance is close, but if you are in Beijing, you may have to wait a few days, because There are many lines and nodes passing through in the middle.

But this will definitely not work, and the user experience will be poor. How to solve this problem?

The farther away the website is, the slower the opening speed will be. It is easy to think that if it is deployed in many places, when users access the network, is it not enough to access the closest one?

It’s like express delivery has some transit warehouses, which can store some goods. If you are in Beijing and want to buy something from Hainan, and it happens to be in the warehouse in Beijing, wouldn’t it be possible to receive it soon.

The idea is fine, but how to implement it?

The user accesses the website through the domain name, can this function be achieved through the DNS server?

I wrote an article on the principle of DNS , here is a brief review:

picture

When the client accesses a domain name, it will first look for the local hosts file, and if it can find the IP, it will directly access it.

Otherwise, a request will be sent to the local DNS server, which is the DNS server provided by operators such as China Unicom and China Mobile in every city. It goes to the domain name server to send a request to resolve the domain name, and then returns the result to the client.

Domain names are parsed hierarchically, with three layers: root domain name server, top-level domain name server, and authoritative domain name server. For example, image.baidu.com will first send a request to the root domain name server to query the ip of the top-level domain name server of com, and then send a request to the top-level domain name server of com. The domain name server queries the ip of the authoritative domain name server of image.baidu.com. After the authoritative domain name server is queried, any level of domain name will be resolved here (so it is called the authoritative domain name server).

When I saw this authoritative domain name server, I wonder if everyone thought of how to implement a CDN network.

Can we do load balancing based on the client's ip at the authoritative domain name server level? For example, a DNS request from Beijing will return the ip of the server in the Beijing computer room, and a DNS request from Shanghai will return the ip of the server in the Shanghai computer room.

It is indeed possible to achieve the nearest distribution of content in this way. Such a load balancing network is called CDN (Conent Delivery Network)

However, to realize such a CDN network requires the establishment of multiple computer rooms across the country, and the cost is too high. Therefore, only large companies such as Baidu, Ali, and Tencent will build their own CDNs. Under normal circumstances, we will buy third-party CDN services for use. .

These companies have built CDN networks, but in fact they can't use them up, and they will also provide CDN acceleration services to the outside world.

The third-party CDN service naturally also provides a DNS server, that is, the one that returns the ip of servers in different cities according to ip.

For example, this is the schematic diagram of Baidu Cloud CDN:

picture

After a user sends a request to the local DNS server, it undergoes DNS resolution of the root domain name and top-level domain name, and will eventually be transferred to the authoritative DNS server. At this time, as long as the authoritative DNS server is transferred to the DNS server of baidu, you can access the CDN service.

The DNS server of baidu implements load balancing, and will return the ip of servers in different cities according to the city where the requested ip is located. It also realizes the network acceleration function of the nearest distribution.

How is this forwarding from authoritative DNS to baidu DNS achieved?

There are many types of DNS records, such as:

A stands for address, record the IP corresponding to the domain name.

CNAME represents the domain name and an alias, you can check the ip from that domain name.

MX represents the domain name or IP corresponding to the file name suffix

Seeing this CNAME type, everyone should think about how to implement forwarding.

As long as you configure a CNAME record on the DNS server, you can point to the domain name of the CDN server.

For example, when you use a cloud's CDN, the first step is to configure the CNAME of your own DNS server to point to it:

picture

In this way, when you visit a domain name, the authoritative server that resolves the domain name will return the domain name of the DNS server of the CDN service, and then send a request to resolve the domain name to the DNS server of the CDN. Come back to a server in the nearest city to you.

Of course, you can also do another layer of CNAME forwarding. For example, the DNS server of the CDN transfers the domain name resolution to the DNS server in the city, and then the DNS server in the city returns a device that is close and has a smaller load according to the load of different machines. The ip of the server is given to the client.

This allows the client to download static resources from the nearest server, opening the website faster.

If the accessed resource is not available, it will send a request to the origin server to get the corresponding resource and cache it. After that, you do not need to visit the origin.

How is this cache time specified?

picture

The CDN service will have a console, which can set the cache time of different resources. Of course, there are also expries and Cache-Control in the request header to control the cache time. The priority of these two can also be set.

Such CDN services are separated domestically and abroad. If the target users of the website are only domestic, you only need to buy the domestic CDN service, and the access of foreign users will be slower. If there are many foreign users of the website, you need to buy it. CDN service abroad.

For example, the distribution of domestic CDN nodes of a cloud is as follows:

picture

Overseas, it is another set of CDN networks:

picture

Summarize

In order to speed up the website opening speed, we will use the CDN service, which is not a network protocol, but a network based on the acceleration function implemented by the DNS protocol.

Its principle is that the authoritative DNS server of the domain name forwards the request to the load balancing DNS server of CND, and then returns the DNS server in different cities according to the IP, and then selects the IP of a nearby server to return according to the load.

This way the client can get resources from the nearest server with the least load.

CDN cache settings can be based on the header, or you can set some rules in the console, and the priority of the two can also be set.

The construction cost of CDN network is still very high. Only large companies will build their own. Generally, we will buy third-party services, and domestic and overseas CDNs are separated. If you mainly serve domestic users, you can only buy domestic CDN services. .

With this network of CDN nodes covering the whole country and the world, we can open the website faster.