CDN
Benefits of CDN?
Improve latency
- Users accessing the CDN servers instead of the main server leads to better performance on both sides. Users who are farther from the main server can expect faster load times due to their requests traveling less distance. The edge servers are dividing and conquering the amount of traffic coming into the site, therefore the load on the main server is lifted. Even if the client needs to access the main server, theyβll get better speeds due to Dynamic Site Acceleration β a blanket term for techniques CDNs employ to make requests more efficient. 
- Remember that 80-90% of the end-user response time is spent downloading all the components in the page: images, stylesheets, scripts, Flash, etc. 
- Why not web storage / distributed cache? - Web storage or distributed cache could not necessarily be deployed as close as CDN to the end user. 
- Static resource such as video or images are so big. 
- If they were to serve from web storage / distributed cache, it will be a huge requirement for network bandwidth and introduce high latency for such content 
 

Improve security
- The layer of abstraction CDN servers offer help to keep security attacks away from your siteβs main server. Edge servers are typically equipped with a WAF (web application firewall) that can filter out malicious incoming and outgoing requests. The firewall can analyze the requests at the edge, and block them from ever touching the main server. In case your site is already compromised, it can analyze outgoing attacks to prevent further spread of malicious messages. 
- Node servers also offer DDoS protection by filtering and absorbing these attacks. Since the edge servers are built to distribute requests to other servers in case of overload or need to access something not in the cache, DDoS attacks can be neutralized. Networks can also handle this load in case of a giant uptick in legitimate requests. 

Improve availability
- The graph above shows the traffic on Princeβs Wikipedia page a few hours after his death was announced. Wikipedia had such a large amount of requests to edit and view that some people couldnβt access the page at all. Luckily, Wikipedia had measures put in place after experiencing similar CPU load spikes and with Michael Jackson and David Bowieβs deaths. 
- In cases like these, the divide and conquer design of Content Delivery Networks can help mitigate the load by distributing requests and serving pages quickly thanks to caching. Even if the main server goes down for some reason, the site wonβt go down for everyone due to the cached versions hosted on the CDNβs node servers. 

CDN cost
- CDN is expensive, especially when the data size is large. - Using Amazon CDN as example https://aws.amazon.com/cloudfront/pricing/ 
- Assume 100% of traffic is served from the United States. The average cost per GB is $0.02. For simplicity, we only calculate the cost of video streaming. - β’ 5 million _ 5 videos _ 0.3GB * $0.02 = $150,000 per day. 
 

- How to reduce the CDN cost - Only serve the most popular contents from CDN and other videos from webserver 
- Some videos are popular only in certain regions. There is no need to distribute these videos to other regions. 
- Build your own CDN like Netflix and partner with Internet Service Providers ( Comcast, AT&T, Verizon, etc.). Building your CDN is a giant project; however, this could make sense for large streaming companies. 
 
Flowchart
How to put an item on CDN
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β        1. A user has a video xxx.avi to put on CDN and wants to access it as        β
β                        https://video.yourcompany.com/xxx.avi                        β
β                                                                                     β
ββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββ
                                           β                                           
                                           βΌ                                           
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 
β                                                                                    β 
β     2. CDN Provider provides a domain name for you such as xxx.akamai.cdn.com      β 
β                                                                                    β 
βββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββ 
                                            β                                          
                                            β                                          
                                            βΌ                                          
 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
 β 3. a CName mapping between your preferred domain name and the one provided by CDN  β
 β              provider is configured on the .com DNS authority server               β
 β                                                                                    β
 β                    video.yourcompany.com => xxx.akamai.cdn.com                     β
 β                                                                                    β
 ββββββββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ
                                            β                                          
                                            β                                          
                                            βΌ                                          
 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
 β 4. A CName mapping between cdn domain name returned from 3 and the CDN global load β
 β        balancer address is configured on the .cdn.com DNS authority server         β
 β                                                                                    β
 β                 xxx.akamai.cdn.com => global.loadbalancer.cdn.com                  β
 β                                                                                    β
 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
                                           β                                           
                                           β                                           
                                           β                                           
                                           βΌ                                           
  βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ  
  β           5. The video is uploaded to CDN, which could be accessed by           β  
  β                      https://video.yourcompany.com/xxx.avi                      β  
  β                                                                                 β  
  βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββHow to get an item from
- The overall load balancing algorithm (step3 and step4) depends on a couple of factors and is not only based on physical distance. - Based on local DNS's IP address 
- Round trip time 
- Based on bandwidth and cost 
- Based on the tier of service 
 
 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
 β                                                                                    β
 β              1. A user accesses https://video.yourcompany.com/xxx.avi              β
 β                                                                                    β
 βββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββ
                                           β                                           
                                           β                                           
                                           β                                           
                                           βΌ                                           
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 
β          2. A request is sent to .com DNS authority server for resolving           β 
β    video.yourcompany.com. Based on the CName mapping configured, a domain name     β 
β                          xxx.akamai.cdn.com is returned.                           β 
β                                                                                    β 
β                    video.yourcompany.com => xxx.akamai.cdn.com                     β 
β                                                                                    β 
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 
                                           β                                           
                                           β                                           
                                           β                                           
                                           βΌ                                           
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 
β        3. A request is sent to .cdn.com DNS authority server for resolving         β 
β      xxx.akamai.cdn.com. Based on the CName mapping configured, a domain name      β 
β                    xxx.global.loadbalander.cdn.com is returned.                    β 
β                                                                                    β 
β               xxx.akamai.cdn.com => xxx.global.loadbalancer.cdn.com                β 
β                                                                                    β 
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 
                                           β                                           
                                           β                                           
                                           β                                           
                                           β                                           
                                           βΌ                                           
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 
β4. The CDN Global balancer xxx.global.loadbalancer.cdn.com returns an IP address    β 
βbased on the following conditions:                                                  β 
β                                                                                    β 
βa). User's IP address                                                               β 
βb). User's network operator such as ATT                                             β 
βc). Request url to determine which CDN server has it                                β 
βd). Current traffic distribution condition                                          β 
β                                                                                    β 
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 
                                           β                                           
                                           β                                           
                                           β                                           
                                           βΌ                                           
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 
β                                                                                    β 
β   5. A user accesses the resource by using https://{returned ip address}/xxx.avi   β 
β                                                                                    β 
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββCDN Operation Mode
- For most cases CDN uses Pull mode. However, in some special cases such as Netflix videos or super large chatrooms, push is preferred. 
Pull based CDN
- Pull based CDN relies on cache control and conditional get headers for determining when an item will expire. 
Push based CDN
- Netflix uses precaching to push content to CDN nodes during off-peak hours. Please read here for further details 
- How netflix use customized consistency hashing to distribute content on CDN. 
CDN internal
Global server load balance - GSLB
Cache proxy
Architecture
- There could be multiple layer of cache clusters - L1 cache cluster is also called edge node. 
- There are more L1 cache cluster than L2 cache cluster. In addition, L1 cache cluster are usually closer to the end user. 
 
- Components within each cache cluster - Level 4 load balancer: - Is faster than level 7 load balancer 
- Could only load balance on transport layer properties such as Source IP address, source port number, dest IP address, dest port number. These propperties necessarily guarantee a high cache hit ratio. 
 
- Level 7 load balancer - Could load balance on HTTP layer properties such as Cookie, URL, method, parameter. 
 
- Cache server 
 
 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ  
 β                                                                                        β  
 β                                    Source of truth                                     β  
 β                                                                                        β  
 ββββββββββββββββββββββββββ²βββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββ  
                          β                                          β                       
                          β                                          β                       
βββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββ  
β                                                                    βΌ                    β  
β                               ...... Level N cache ......                               β  
β                                                                                         β  
βββββββββββββββββββββββββββ²βββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββ  
                          β                                          β                       
                          β                                          β                       
                          β                                          β                       
 βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ 
 β                                      CDN L2 Cache                                       β 
 β ββββββββββββββ   ββββββββββββββ        ββββββββββββββ     ββββββββββββββ  βββββββββββββββ 
 β β            β   β            β        β            β     βLVS with VIPβ  βLVS with VIPββ 
 β βLVS with VIPβ   βLVS with VIPβ        β   ......   β     β  address   β  β  address   ββ 
 β βaddress VIP3β   βaddress VIP4β        β            β     β   VIP10    β  β   VIP11    ββ 
 β β            β   β            β        β            β     β            β  β            ββ 
 β ββββββββββββββ   ββββββββββββββ        ββββββββββββββ     ββββββββββββββ  βββββββββββββββ 
 β                         β²                                        β²                      β 
 βββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββ 
                           β                                        β                        
                      Request 1                                     β                        
                           β                                   Request 2                     
                           β                                        β                        
                           β                                        β                        
                           β                                        β                        
  βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββ
  β Usually two L1 CDN cache cluster is deployed within a single region for resiliency and  β
  β                                   performance purpose                                   β
  β                                                                                         β
  βββββββββββββββββββββββββββββββββββ                     βββββββββββββββββββββββββββββββββββ
  ββ           Cluster 1            β                     β           Cluster 2            ββ
  ββ                                β     .βββββββββ.     β                                ββ
  ββ                                β _.β'           `ββ. β                                ββ
  ββ  ββββββββ  ββββββββ  ββββββββ  ββ±Local cache server β²β ββββββββ  ββββββββ  ββββββββ   ββ
  ββ  βCache β  βCache β  βCache β  β   such as Nginx,    β βCache β  βCache β  βCache β   ββ
  ββ  βserverβ  βserverβ  βserverβ  β  Varnish, Traffic   β βserverβ  βserverβ  βserverβ   ββ
  ββ  ββββββββ  ββββββββ  ββββββββ  ββ²      server       β±β ββββββββ  ββββββββ  ββββββββ   ββ
  ββ      β²         β²         β²     β β²                 β± β     β²         β²         β²      ββ
  ββ      β         β         β     β  `ββ.         _.β'  β     β         β         β      ββ
  ββ      βββββββββββΌββββββββββ€     β      `βββββββ'      β     βββββββββββΌββββββββββ€      ββ
  ββ      β         β         β     β                     β     β         β         β      ββ
  ββ      β         β         β     β                     β     β         β         β      ββ
  ββ      β         β         β     β    .βββββββββββ.    β     β         β         β      ββ
  ββ  ββββββββ  ββββββββ  ββββββββ  β_.β'             `ββ.β ββββββββ  ββββββββ  ββββββββ   ββ
  ββ  βNginx β  βNginx β  βNginx β  β  L7 load balancer   β βNginx β  βNginx β  βNginx β   ββ
  ββ  ββββββββ  ββββββββ  ββββββββ  β   such as Nginx,    β ββββββββ  ββββββββ  ββββββββ   ββ
  ββ      β²         β²         β²     ββ²      HAProxy      β±β     β²         β²         β²      ββ
  ββ      β         β         β     β `ββ.           _.β' β     β         β         β      ββ
  ββ      βββββββββββΌββββββββββ     β     `βββββββββ'     β     βββββββββββΌββββββββββ      ββ
  ββ                β               β                     β               β                ββ
  ββ                β               β                     β               β                ββ
  ββ                β               β                     β               β                ββ
  ββ         ββββββββββββββ         β   .βββββββββββ.     β        ββββββββββββββ          ββ
  ββ         βLVS with    β         _.β'             `ββ. β        βLVS with    β          ββ
  ββ         βvirtual IP  β        ;    Level 4 load     :β        βvirtual IP  β          ββ
  ββ         βaddress VIP1β        :  balancer such as   ;β        βaddress VIP2β          ββ
  ββ         β            β         β²     LVS / F5      β± β        β            β          ββ
  ββ         ββββββββββββββ         β`ββ.           _.β'  β        ββββββββββββββ          ββ
  ββ                                β    `βββββββββ'      β                                ββ
  βββββββββββββββββββββββββββββββββββ                     βββββββββββββββββββββββββββββββββββ
  β                 β²                                                      β²                β
  βββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββ
                    β                                                      β                 
                    β                                                      β                 
                    β                                                      β                 
                    β                                                      β                 
                    β                                                      β                 
                    β                                                      β                 
                    β                                                      β                 
                    β            return  ββββββββββββββββ       return     β                 
                  Request 1       VIP1   βCDN global    β        VIP2    Request 2           
                    β         ββββββββββ βload balancer ββββββββββββββββΆ   β                 
                    β                    β              β                  β                 
                    β                    ββββββββββββββββ                  β                 
                    β                                                      β                 
                    β                                                      β                 
                    β                                                      β                 
                    β                                                      β                 
                    β                                                      β                 
                    β                                                      βReal world
Google CDN
Last updated
Was this helpful?