CDN to accelerate static file access

 

	CDN是Content Delivery Network的缩写,意思是内容分发网络。CDN的作用是把用户需要的内容分发到离用户近的地方,这样可以使用户
	能够就近获取所需内容。	
	整个CDN系统(如图1-1所示)分为CDN源站和CDN节点,CDN源站提供CDN节点使用的数据源头,而CDN节点则部署在距离最终用户比较
	近的地方,加速用户对站点的访问。
	CDN其实就是一种网络缓存技术,能够把一些相对稳定的资源放到距离最终用户较近的机房,一方面可以节省整个广域网的带宽消耗,另外
	一方面可以提升用户的访问速度,改进用户体验。我们一般把一些相对静态的文件(例如图片、视频、JS脚本、一些页面框架)放在CDN中。

Figure 1-1
Here Insert Picture Description
we visit a website through a browser process is as shown in Figure 1-2.
Figure 1-2
Here Insert Picture Description
(1) the user submits the domain name you want to access to the browser.
(2) the browser parses the domain name, the domain name to get the corresponding IP address.
(3) the browser sends a request to the obtained IP address.
(4) the browser displays the contents of a web page based on the data returned.

而在有了CDN以后,用户通过浏览器访问网站的过程会产生一些变化。如图1-3

Figure 1-3
Here Insert Picture Description
(1) the user submits the domain name you want to access to the browser.
(2) the browser parses the domain name, the domain name resolution process since CDN adjusted, so that the obtained CNAME record corresponding to the domain name.
(3) for CNAME parsed again, get the actual IP address. In this resolution, the global load balancing uses DNS resolution, that is, we need to return to
a different specific IP addresses, you need to determine the returned results based on location information and where the ISP, a process to make living in different regions, connected access provider
users get CDN addresses the most suitable access, in order to achieve the nearest access, so as to enhance speed.
After (4) to give the actual IP address, issuing an access request to the server.
(5) CDN will be treated differently if a local cache according to the contents of the request:
1) if present, returns the result directly.
2) If not, then the source station CDN request for acquiring the content, and then returns the result.

Through this process, we can see several key technology in the CDN.

Global scheduling

全局调度是完成用户就近访问的第一步,我们需要根据用户地域、接入运营商以及CDN机房的负载情况去调度。前面两个调度因素需要一个
尽可能精准的IP地址库,这是正确调用的前提(误识别的IP地址到地理位置的对应可能会把东北的用户调度到华南的站点去),当然,做到
100%的精确是不现实的。IP地址库的维护是一个持续和变化的过程,并且调度的策略随着CDN机房的增加也会变化。例如,我们不可能在
所有城市都设置CDN机房,假设刚开始河南整个省份没有CDN机房,可能河南靠北的城市使用天津的CDN,同时河南靠南的城市使用湖北
的CDN会比较好,而如果后来在郑州市建设了CDN机房的话,那么原来的调度策略就会修改了。CDN的负载也是调度中的一个影响因素,
举例来说,如果一个CDN机房举例你的位置比较近,但是它的负载已经很高,响应很慢,那么你的请求送到距离稍远的CDN机房反而会更快。

Caching

	从上面的流程中我们看到,如果用户请求的内容不在CDN中的话,CDN会回到源站去加载内容,然后返回给用户。所以,如果CDN机房的请求
命中率不高的话,那么起到的加速效果也是相对有限的。
	要提升命中率,就需要CDN机房中有尽可能全面的数据,这要求CDN机房的缓存容量要足够大,我们可以使用“内容+SSD+机械硬盘”的混合存
储方式来提升整体的缓存容量,并且我们做好冷热数据的交换,在提升命中率时也尽量降低缓存的响应时间。
	此外,当CDN的Cache没有命中要回源加载数据时,合并同样数据的请求也是一个很重要的优化,这样可以减少重复的请求,降低源站的压力。
	最后,新增、变更数据后的CDN预加载也是一个提升命中率的办法。也就是在没有请求进来时,CDN主动去加载数据,做好准备。当然这个主
动加载一般也需要源站有一个通知过来。

Content distribution

	这里提到的内容分发主要是对内容全部在CDN上不用回源的数据的管理和分发,例如一些静态页面等。具体做法是在内容管理系统中进行编辑
修改后,通过分发系统分发到各个CDN的节点上。分发的效率以及对分发文件一致性、正确性的校验是需要关注的点。

Bandwidth Optimization

	CDN提供了内容加速,很多请求和流量都压到了CDN上,那么如何能够比较有效地节省带宽会是一个很重要的事情,因为这直接关系到流量
成本。优化的思路是只返回必要的数据、用更好的压缩算法等。

In the application of the CDN, from the traditional sense, mainly to user needs to access the content on the user from the local close. Can be found in most of the traffic to flow from the source station CDN room, we can also take advantage of the room from CDN location near the target user, so that some of the work from CDN access to upload, and then spread from the source station CDN
, in this regard you can enhance the user upload speed, on the other hand also make good use of the upstream bandwidth of the source station to the room from the CDN.

Reference books: large sites system and Java middleware practice

Guess you like

Origin www.cnblogs.com/haizai/p/11098186.html