A request needs to go through three stages: request => processing => response, local cache, browser cache is to minimize requests, use local cache to improve response speed. If the local cache will not be able to use, so we have to launch a request to the server, so this request is involved in the process of the DNS, url parsing into IP addresses (iterative query, recursive query), and then launched a connection, but if the same server receives the processing request more and more, the greater the pressure on the server, the response time delay may be longer At the same time, the access delay in different regions is different (server distance), which also affects the response delay. So you need a CDN.

Content Delivery Network (CDN) Content Delivery Network

Through the server placed throughout the network node based on the existing Internet build a layer of intelligent network, through the center platform based on real-time network traffic and connection, the load of each node and the distance to the client and comprehensive information such as response time, the client will request to point to the nearest node server, as much as possible in order to avoid the Internet may affect the data transmission The link of transmission speed and stability (distribution, bandwidth, server performance, etc.), so that content transmission is faster and more stable.

The basic principle of

The basic principle of CDN is widely used in a variety of the cache, the cache server points less than the user to access relatively concentrated area or in the network, when the user access, using the global load technology to the user’s access point to the nearest work cache server, directly by the cache server response (refer to the reverse proxy). The key technology of CDN is content Storage and distribution technology.

CDN hierarchy division

  • Edge layer: In the CDN system, Cache devices that directly face the client and provide services to the client are located at the edge of the entire CDN network

  • The central layer is responsible for global management and control, and also holds the largest content Cache. If the edge layer does not hit the cache, the request is made to the central layer, and if the central layer does not hit the cache, the request is made to the source site. In different CDN systems, some central layers may also have the ability to serve clients, and some may only provide services for the next layer

  • Region layer: If the CDN system is large, the edge layer requests too much content from the center layer, which will cause excessive load pressure on the center layer. In this case, a region layer needs to be set between the center layer and the edge layer, responsible for the management and control of a region, and some content Cache can also be provided for the service of the edge layer.

  • CNAME: alias: sets an alias for a domain name. URL is the name of the IP address and CNAME is the alias of the URL. Find the URL through the CNAME record.

  • Back to source: When the client sends a request to the CDN node, if there is no cache or the cache has expired, it needs to send a request to the source to obtain the latest data. This process is called back to source.

CDN cache

The Cache policies of CDN edge nodes vary with service providers, but generally follow the STANDARD HTTP protocol and set the data Cache time of CDN edge nodes through cache-control :max-age= XXX in the HTTP response header. When the client initiates a request to the CDN node, the CDN node will judge whether the cached data has expired. If not, the CDN node directly returns the cached data to the client. Otherwise, the CDN node initiates a request to the source station, obtains the latest data from the source station, updates the node cache, and returns the latest data to the client. CDN service providers will provide multiple dimensions based on file suffixes and directories to control CDN cache time and provide users with more refined cache management. CDN cache time has a direct impact on the “back to source rate”. If the CDN cache time is short, the cache on the edge nodes of CDN will often fail, resulting in frequent back to source, increasing the load of the source station and increasing the access delay. If the CDN cache time is too long, the data update will not be timely.

When a client makes a request to the server (CDN)
  1. DNS resolution Because the CDN adjusts domain name resolution, the DNS server assigns domain name resolution rights to the DNS server dedicated to the CDN based on the CNAME. That is, the DNS server returns the DNS server IP address dedicated to the CDN after resolving the domain name
  2. The client requests the CDN dedicated DNS server to obtain the IP address of the load balancing device in the CDN
  3. The client connects to the load balancing device, and the CDN load balancing device selects an appropriate server to provide services for the client, that is, returns the CDN edge node IP(cache server)
  4. The client sends a request to the IP address of the CDN node
  5. The CDN node determines whether to obtain data from the source station and update the node cache according to whether the cache is expired
  6. The CDN node returns the request data
The CDN cache is refreshed

CDN nodes are transparent to developers. The interface of “refresh cache” provided by CDN service providers can be used to clear CDN node cache and force data to expire, so as to obtain the latest data.