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
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
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
Real world
Google CDN
Last updated