CDN Caching those Things

Source: Internet
Author: User
Tags ticket browser cache
CDN Caching those Things


What is a CDN.

When it comes to the role of CDN, it can be likened to the experience of buying train tickets for 8 years:

8 years ago, there is no fire ticket for sale Point one said, 12306.cn is not to mention. At that time the train tickets can only be bought in the railway station ticket hall, and I live in the small county does not pass the train, train tickets to the city's railway station to buy, and from the county to the city, back and forth is a 4-hour drive, is simply a waste of life. Later, the small county appeared in the train ticket consignment point, can directly in the consignment point to buy a train, convenient a lot, the city people no longer need to be in a bit bitter force queuing to buy a ticket.

CDN can be understood as distributed in each county train ticket consignment point, when users browse the site, CDN will choose a nearest to the user Cdn Edge node to respond to the user's request, So the request of Hainan Mobile users will not go all the way to Beijing Telecom Room server (assuming the source station deployed in Beijing Telecom room) on the.

The advantages of CDN are obvious: (1) The CDN node solves the problem of trans-operator and trans-geographical access, and the access delay is greatly reduced; (2) Most of the requests are completed at the Cdn Edge node, and the CDN has a diversion function, which reduces the load of the source station.

What the cache is.

Here does not delve into the large structure behind the CDN, nor discuss how CDN can achieve the global traffic scheduling strategy, this paper focuses on how the data is cached after the CDN is in place. Caching is a ubiquitous example of space-time change. By using extra space, we can get faster speeds. First, see how the user's browser interacts with the server when no Web site is connected to the CDN:

When users browse the site, the browser can save a copy of the pictures or other files in the Web site, so that when the user visits the site, the browser will not download all the files, reducing the amount of download means that the speed of the page load increased. If you add a layer of CDN to the middle, the user's browser interacts with the server as follows:
The client browser first checks whether there is a local cache expiration, if expired, to the CDN Edge node to initiate a request, CDN Edge node will detect the user request data cache is expired, if not expired, then directly respond to user requests, at this time a complete HTTP request end, if the data has expired, The CDN also needs to send back a source request to the source station to pull the latest data. The typical topology map of CDN is as follows:


Image source: http://grefr.iteye.com/blog/2004248

It can be seen that in the presence of CDN, the data experienced both the client (browser) cache and the CDN Edge node cache, and the following two phases of the cache were analyzed in detail.

Client (browser) caching

Disadvantages of client Caching

Client-side caching reduces server requests, avoids duplication of files, and significantly improves user locations. But when the site is updated (such as CSS, JS, and picture files), browsers still keep old versions of the files locally, resulting in unpredictable consequences.

Once upon a time, a page loaded out, the page elements of the Site Drift, button click Failure, the front-end GG will be accustomed to ask: "Cache clear." "And then Ctrl+f5, Everything is OK. But sometimes, if we simply knock a carriage return in the browser's address bar, or just press F5 to refresh, the problem still remains unresolved, and you know that these three different ways of doing things, you can determine the browser different refresh caching strategy.

How the browser determines whether to use local files or to use new files on the server. Here are some methods of judging.

Browser Caching policy

Expires

Expires:sat, 2015 20:30:54 GMT


If expires is set in the HTTP response message, we avoid the connection to the server until the expires expires. At this point, the browser does not need to make a request to the browser, only to determine whether the hands of the material expired on it, there is no need to increase the burden of the server.

Cache-control:max-age
Expires is a good way to go, but every time we have to calculate a precise time. Max-age tags can make it easier to deal with expiration times. We just need to say that you can only use this information in one weeks.

Max-age use seconds to measure, such as: cache-control:max-age=645672 specified page 645,672 seconds (7.47 days) after expiration.

Last-modified

In order to notify the browser of the current version of the file, the server sends a label for the last modified time, such as: Last-modified:tue, 2015 08:26:32 GMT
So that the browser knows the file creation time he received, in subsequent requests, the browser will follow the following rules to verify: 1. Browser: Hey, I need to jquery.min.js this file, if it is in Tue, 2015 08:26:32 GMT after modified, please send me.  2. Server: (Check the file modification time) 3. Server: Hey, this file hasn't been modified since that time, you've got the latest version. 4. Browser: Great, then I'll show it to the user.
In this case, the server simply returns a 304 response header, reducing the amount of data in response and increasing the speed of the response. For 304 responses, please refer to: http://www.cnblogs.com/ziyunfei/archive/2012/11/17/2772729.html the following figure is to refresh the page by F5, and the page returns 304 response headers.

ETag

In general, it is possible to compare files by modifying the time. However, in some special cases, such as the server clock error, the server clock to modify, the arrival of daylight saving DST server time is not updated in time, which will cause the modification time to compare the file version of the issue.

ETag can be used to solve this problem. ETag is a unique identifier for a file. Like a hash or fingerprint, each file has a separate flag, and as long as the file changes, the logo changes.

Server return ETag Label:
ETag: "39001d-1762a-50bf790757e00"
The following order of access is shown below: 1. Browser: Hey, I need to jquery.min.js this file, there is no mismatch "39001d-1762a-50bf790757e00" this string of 2. Server: (check etag ...) ) 3. Server: Hey, my version here is also "39001d-1762a-50bf790757e00", you are already the latest version of the 4. Browser: OK, then you can use the local cache. Like Last-modified, ETag solves the problem of file version comparisons. But the ETag level is higher than the last-modified.

Extra tags

The cache label will never stop working, but sometimes we need to control what we have already cached. L Cache-control:public indicates that a cached version can be identified by a proxy server or other intermediary server. L Cache-control:private means that this file is different for different users. The public proxy server does not allow caching only if the user's own browser is capable of caching. L Cache-control:no-cache means that the contents of a file should not be cached. This is useful in search or page-flipping results, because the same URL, the corresponding content will be changed.

Browser cache Refresh

1. Enter the URL in the Address bar and press Enter or click to go to the button browser to the minimum request to get the Web page data, the browser will not have expired content directly using the local cache, thereby reducing the request to the browser. Therefore, the expires,max-age tag is only valid for this method.

2. Press F5 or browser refresh button The browser will append the necessary cache negotiation to the request, but not allow the browser to use the local cache directly, it can make the last-modified, ETag play an effect, but expires invalid.

3. Press CTRL+F5 or press CTRL and click the Refresh button to force the refresh, always initiate a new request without using any caching.

CDN Cache

When the browser's local cache fails, the browser initiates a request to the CDN Edge node. Similar to browser caching, CDN Edge node also has a set of caching mechanism.
Disadvantages of CDN Cache

The streaming of CDN not only reduces the user's access delay, but also reduces the load of the source station. But its shortcomings are also obvious: when the site is updated, if the CDN node data is not updated in a timely manner, even if the user's browser using CTRL +F5 to disable the browser side of the cache, but also because the CDN Edge node does not synchronize the latest data and lead to user access exceptions.

CDN Caching Strategy

The strategy of CDN Edge node caching is different from service providers, but it usually follows the HTTP standard protocol, and sets CDN Edge node data cache time through the Cache-control:max-age fields in the HTTP response header.

When the client requests data from the CDN node, CDN node will determine whether the cached data expires, if the cached data does not expire, the cached data directly back to the client; otherwise, the CDN node will send back the source request to the source station, pull the newest data from the source station, update the local cache, and return the newest data to the client.

CDN Service providers typically provide a more granular cache management based on file suffixes and multiple dimensions of the directory to specify CDN cache time.

The CDN cache time will have a direct effect on the "back-source rate". If the CDN cache time is short, the data on the CDN Edge node will often fail, resulting in frequent return, increasing the load of the source station and increasing the access delay; If the CDN cache time is too long, it will bring the problem of slow data update time. Developers need to increase the number of specific business, to do a specific data cache time management.

CDN Cache Refresh

The CDN Edge node is transparent to the developer, the developer can clean up the CDN Edge node cache by the "Refresh cache" interface provided by CDN, compared with the forced refresh of the browser Ctrl+f5 to invalidate the browser local cache. This way, after updating the data, developers can use the Refresh Cache feature to force the data cache on the CDN node to expire, ensuring that the client pulls up the latest data while accessing it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.