3

The full name of CDN is "Con te nt Delivery Network", and the Chinese is called content delivery network.

In fact, the concept of CDN was put forward in 1996 by a research team of the Massachusetts Institute of Technology in the United States to improve the quality of Internet services. So how does it improve the quality of Internet service?

Principle analysis

We know that when we use a domain name to visit a certain website, we actually transmit the request packet (take Http request as an example) to a certain server over the network, such as when visiting "www.b ai du.com":

First resolve the IP address corresponding to the domain name (DNS domain name resolution)

Then route the Http request packet through the network to the server corresponding to the IP address

We usually say "the IP address of the server", which is actually not very accurate. The IP address is bound to the network card. A server can have multiple network cards, that is, it may have multiple IP addresses.

Let’s look at the first step: domain name resolution

DNS

There are two types of domain name resolution:

Resolve a domain name into an IP address

Resolve one domain name into another domain name

In fact, it is not difficult to resolve the idea. After we purchase a domain name from a domain name service provider, we need to map an IP address. We can use Map to express this relationship: {domain name: IP}.

At the same time, we can also give a domain name an alias, for example, "www.baidu.com" takes an alias "test.baidu.com", this relationship can also be expressed by Map: {domain name: alias}. The alias here is professionally called CNAME. I believe everyone is familiar with this term, which means it.

The domain name resolution is actually to resolve the IP address corresponding to the specified domain name, or a CNAME of the domain name.

The DNS system is responsible for domain name resolution. The DNS service accepts external requests and extracts the domain name from the request.

If this domain name corresponds to an IP address, return this IP address,

If this domain name corresponds to a CNAME, continue to look up the IP address of the CNAME domain name, and then return the address to the sender of the request.

After the request sender obtains the IP address, the actual request call is completed.

In fact, the DNS system is very large. I don’t want to go here. Everyone treats it as a black box. The function of this box is as described above. Here is a simple diagram to show it.

Without CNAME:

img

In the case of CNAME:

img

Special attention: In the case of CNAME, we can find that CNAME actually assumes the role of an intermediary (or proxy) in the process of domain name resolution, which is the key to the realization of CDN.

CDN principle

First of all, CDN is to improve the quality of Internet service. In layman's terms, it is actually to improve the access speed.

Suppose there is only one server on the Baidu website, and there is a person visiting Baidu in Shanghai. If the server is also in Shanghai, the access is usually faster. If the server is in Lhasa, the access is relatively slow. Then the root cause of this problem is that network transmission depends on the network cable . The longer the network cable, the longer the time will definitely be.

So how to solve this problem? In fact, the idea is very simple, Baidu deploys exactly the same servers all over the country, and professionalism is called redundancy.

The idea is very simple, but the implementation is still more troublesome. The resources on the server are divided into two types: static resources and dynamic resources.

Static resources: This kind of resources are usually rarely changed, such as pictures, videos, css, javascript etc.

Dynamic resources: This kind of resource is usually different for different users at different times, such as ftl, jsp, etc.

So if Baidu wants to deploy servers all over the country, if each server has the same dynamic resources, you may also need to configure the corresponding database , because the information recorded by dynamic resources is usually stored in the database, then This involves data synchronization and other issues, which will lead to high costs. This practice is actually clustering. At present, the cluster architecture is at most three locations and five centers. It does not mean that clusters in multiple locations across the country are impossible. the cost is too high.

There is a relatively low way, that is, only static resources are deployed on each server. Static resources usually do not involve the database, so the cost is relatively low, and it can also increase the user's access speed.

So far, I have introduced the goal that CDN wants to achieve, so how to achieve this goal?

Now if we want to compare CDN systems, we can consider two points:

What is the performance and network speed of the static resource storage server in the CDN system?

The number and deployment of server nodes in the CDN system nationwide or even globally.

The first point is easy to understand, and the second point should be understood by everyone. If there are many server nodes for static resources, each user can access these static resources without having to "travel a long distance" to obtain them, then Naturally, this is the advantage of the CDN system.

Some companies have seen this demand, so there are actually many CDN providers, such as Ali, Tencent, etc., have their own CDN services. As long as your own system is connected to the CDN service provided by these major manufacturers, and you pass your own static resources to the CDN service, these static resources will be automatically distributed all over the world.

Okay, then the problem now is that when users access static resources, they also use domain names. The domain name will be resolved to a certain IP address. The key question is how does the DNS system resolve a domain name that is out of the user’s The most recent IP address.

Ordinary DNS system can’t do it, it needs a special DNS server, this special DNS needs to know

User's current location

You also need to know which IP address corresponds to the domain name that the user is currently visiting, and where is the IP address?

For the first problem, the user’s ip address is directly extracted from the user’s request. For example, this ip address is resolved to Beijing Telecom, Shanghai Mobile, and so on.

Who will solve the second problem? We are now considering CDN. CDN providers must know where their companies have deployed machines and their IP addresses, so this problem can only be solved by CDN providers, and CDN providers will Provide this special DNS server, which we call CDN dedicated DNS server.

In this case, as long as the user uses a certain domain name to access static resources, if the user directly configures the DNS address of his computer as a CDN dedicated DNS server. Then the problem is solved naturally, but when we need to consider, we cannot ask all users in the world to modify the DNS addresses of their computers. So at this time we must use the CNAME in DNS.

When a user uses a certain domain name to access static resources (this domain name is called "accelerated domain name" in the Ali CDN service), for example, this domain name is "image.baidu.com", which corresponds to a CNAME called "cdn.ali.com" , Then the ordinary DNS server (different from the CDN dedicated DNS server) will first resolve to “cdn.ali.com” when resolving “image.baidu.com”. If the ordinary DNS server finds that the domain name corresponds to a DNS server, it will The domain name resolution work is transferred to the DNS server, which is the CDN dedicated DNS server. The CDN dedicated DNS server parses "cdn.ali.com", and then selects a CDN server address closest to the user based on all the CDN server address information recorded on the server, and returns it to the user, the user can access away from himself The nearest CDN server.

img

add:

There are many types of records when resolving domain names, the most commonly used are:

A record: One domain name corresponds to one IP address

CNAME: One domain name corresponds to another domain name

NS: Assign the subdomain name to other DNS servers for resolution

img


MangoGoing
780 声望1.2k 粉丝

开源项目:详见个人详情