CDN
stands for Content Delivery Network. The basic principle of CDN is to widely use various caching servers, distributing these caching servers to regions or networks where user access is relatively concentrated. When users access a website, global load balancing technology is used to direct their access to the nearest functioning caching server. The caching server directly responds to user requests. The basic idea of CDN is to avoid potential bottlenecks and issues that may affect data transmission speed and stability on the internet, making content delivery faster and more stable. By placing node servers in various locations on the network, CDN systems can dynamically redirect user requests to the nearest service node based on network traffic, node connections, load conditions, distance to users, response time, and other comprehensive information. The goal is to allow users to obtain the required content nearby, solve congestion issues on the internet, and improve the response speed of accessing websites.
When using CDN services provided by a CDN service provider, some configurations need to be made:
cdn.example.com
.www.example.com
.cdn.example.com.service.com
.The access process of a simple CDN, using a pull method to fetch the cache, is as follows:
There are two major challenges in computing: when the cache expires and how to name it. In the context of CDN, cache expiration is a tricky problem. If the resource files on the origin server change and the user retrieves the resources from the cache node, it will result in inconsistent resource files. One way to solve this problem is to refresh all CDN caches by actively pushing them, but this method is costly. A simpler solution is to invalidate the cache after a fixed period of time. In addition to controlling the cache on the nodes, the user's local cache also needs to be controlled. The HTTP protocol provides the following cache control methods:
Strong cache controls the validity period of the cache stored locally through Expires
and Cache-Control
.
Expires
is a header introduced by HTTP 1.0 to indicate the expiration time of a resource. It represents an absolute time returned by the server. Expires
is limited by the local time, so modifying the local time may cause the cache to expire. For a resource request, if it is within the Expires
period, the browser will directly read the cache and will not request the server.
Cache-Control
appeared in HTTP 1.1 and has a higher priority than Expires
. It represents a relative time and is supported by both request and response headers. Different values provided by Cache-Control
define different caching strategies.
Cache-Control: no-store
: The cache cannot store any content related to client requests and server responses. Each request initiated by the client will download the complete response content.Cache-Control: no-cache
: The cache stores the server's response content, but this cache cannot be provided to the browser until the freshness is revalidated with the server. In simple terms, the browser caches the server's response resources, but for each request, the cache needs to evaluate the validity of the cached response with the server. Based on whether the response is 304
or 200
, it determines whether to use the local cached resource or the server's response resource.Cache-Control: public || private
: public
indicates that the response can be cached by any intermediary, such as a proxy or CDN. The default response is private
, which means that the response is private and intermediaries cannot cache it. The response can only be applied to the browser's private cache.Cache-Control: max-age=31536000
: The response has the maximum expiration time, and the directive is max-age=<seconds>
, which indicates the maximum time the resource can be cached and kept fresh, in seconds from the time the request is initiated.Cache-Control: must-revalidate
: When the must-revalidate
directive is used, it means that when the cache considers using a stale resource, it must first validate its status. Expired caches will not be used. In normal cases, it is not necessary to use this directive because in the case of strong cache expiration, negotiated caching will be performed. However, the HTTP specification allows clients to use expired caches in certain special cases, such as when the validation request fails or when there are some special directives configured, such as stale-while-revalidate
and stale-if-error
. The must-revalidate
directive ensures that the cache must be revalidated in any case after expiration.When the browser's request for a resource does not hit the strong cache, it sends a request to the server to verify if the negotiated cache is hit. If the negotiated cache is hit, the response status returned is 304 (Not Modified)
, and the request does not carry entity data. If it is not hit, the response is 200
and carries the resource entity data. Negotiated caching is managed using the Last-Modified, If-Modified-Since
and ETag, If-None-Match
pairs of headers.
Last-Modified
and If-Modified-Since
were introduced in HTTP 1.0
. Last-Modified
indicates the last modification date of the local file. The browser adds If-Modified-Since
to the request header, which is the value of the previous response's Last-Modified
, to ask the server if the resource has been updated since that date. If there are updates, the server will send the new resource back. However, if the cached file is opened locally, the Last-Modified
will be modified. Therefore, ETag
was introduced in HTTP 1.1
.
ETag
is like a fingerprint. Any changes to the resource will cause the ETag
to change, regardless of the last modification time. ETag
ensures that each resource is unique. The request header field If-None-Match
sends the previously returned ETag
to the server to inquire if the resource's ETag
has been updated. If there are changes, the server will send the new resource back. ETag
has a higher priority than Last-Modified
. The use of ETag
is mainly considered in the following situations:
GET
it.N
modifications within 1s
. The granularity that If-Modified-Since
can detect is in seconds, so it cannot detect such rapid modifications.