‫Hey, welcome back to another lecture.
‫In this lecture, we're going to talk about CDNs.
‫Although CDNs are not so much
‫an architectural building block but more of a service,
‫it is one of the most important technologies
‫that power the internet as we know today.
‫So what problem are we solving using a CDN?
‫Even with distributed web hosting
‫across multiple data centers,
‫which are powered by technologies
‫like Global Server Load Balancing,
‫there is still a significant latency
‫caused by the physical distance between an end user
‫and the location of the hosting servers.
‫Additionally, each request between an end user
‫and the destination server
‫has to go through multiple hops
‫between different network routers.
‫This adds even more to the overall latency.
‫To get a better idea of where a CDN comes in handy,
‫let's take an example of a user located in Brazil
‫who wants to load the homepage of our online store
‫hosted in a data center
‫somewhere on the east coast of the United States.
‫Let's assume that this HTML webpage
‫contains links to 10 different assets,
‫like Images, JavaScript, and CSS files.
‫Also, for simplicity, let's assume that the network latency
‫between the user's computer in Brazil
‫and the data center in the United States
‫is about 200 milliseconds.
‫Now, since HTTP uses TCP under the hood
‫because of the TCP three‐way handshake
‫who just established the connection
‫between a client and the server,
‫we need to make three network trips.
‫In this case, accounting to 600 milliseconds.
‫Only once the TCP connection is established
‫the client can send the first HTTP request
‫to get the homepage
‫and get the response with the HTML webpage.
‫So that network round trip
‫now added another 400 milliseconds of latency.
‫And now, when the user finally receives the webpage,
‫the browser makes an asynchronous request
‫to load the 10 different assets the webpage needs.
‫That adds another 200 milliseconds
‫for the request to arrive at the server
‫and another 2,000 milliseconds
‫to load all the assets to the page.
‫So if we add up all those numbers together,
‫we end up with a little over three seconds
‫before a user gets a complete webpage
‫that they can interact with.
‫Now, in the context of a study
‫published by Google Analytics in March 2016
‫that indicated that 53% of mobile users
‫abandoned a website
‫if it took longer than three seconds to load,
‫this number we just calculated
‫does not look good for our service.
‫Of course, we can try to improve our system's performance
‫by replicating our service
‫and running it on more data centers.
‫But if you think about it,
‫it's not our service that contains all the business logic
‫that the users need closer.
‫It's mostly the study content, like Images,
‫HTML pages, JavaScript, CSS files, and video files
‫that we need to get closer to the users
‫to get them load faster.
‫This is where CDNs come in.
‫A CDN, which stands for Content Delivery Network,
‫is a globally distributed network of servers
‫located in strategic places
‫with the main purpose of speeding up the delivery of content
‫to the end users.
‫CDNs were originally created to address the problem
‫often referred to as the Wide World Wait,
‫which is a term describing a bad user experience
‫caused by a slow network connection
‫or an overloaded web server.
‫So CDNs provide that service
‫by caching our website content on their servers,
‫often referred to as edge servers,
‫and are located at different Points of Presence.
‫Those edge servers are both physically closer to the user
‫and are more strategically located
‫in terms of network infrastructure.
‫This allows them to transfer the content
‫much quicker to the user
‫and improve our perceived system performance.
‫Content Delivery Networks can be used
‫to deliver webpage content and assets,
‫like Images, text, CSS, and JavaScript files,
‫but can also be used to deliver video streams,
‫both live and on-demand.
‫Today, CDNs are used by pretty much
‫all digital service companies that interact with users
‫through either a website or a mobile app.
‫A few examples include E-commerce services,
‫financial institutions like banks,
‫technology and software as a service companies,
‫media companies that deliver video streaming or news,
‫and social media companies.
‫Now, besides faster page loads,
‫the distributed nature of CDNs
‫improves the overall availability of our service.
‫The reason for that is any issue or slowness in our system
‫won't be as noticeable to the users
‫because most of the content will actually come from the CDN
‫and not from our servers directly.
‫CDNs also improve our system's security
‫and help protect us against DDoS attacks
‫since those malicious requests will not go to our system,
‫but instead, are going to be distributed
‫among a large number of servers hosted by the CDN provider,
‫their ability to impact us is very low.
‫So to get some intuition
‫of how much improvement we can get by using a CDN,
‫let's assume that that same user in Brazil
‫who's trying to load our online store's homepage
‫is now going to reach a cached version of our website
‫in a CDN close by.
‫So let's assume that now,
‫the latency between the user and the CDN server
‫is about 50 milliseconds.
‫So now, to establish the connection,
‫the user will have to pay only 150 milliseconds of latency,
‫which accounts for the TCP three-way handshake.
‫Then, to send the HTTP request for the webpage
‫and get it back,
‫we spend another 100 milliseconds.
‫And finally, to send a request for the different assets
‫that the webpage needs
‫and to get all those assets loaded by the browser,
‫we pay another 550 milliseconds.
‫This brings the total latency to load our webpage
‫to comfortably under one second,
‫which is an excellent user experience by all measures.
‫And we were able to achieve it by utilizing CDN
‫to catch the content of our homepage
‫on their network of servers.
‫In addition to being physically closer to the users,
‫CDN providers utilize other techniques
‫to transfer the content to the user a lot faster.
‫For example, the CDN servers use faster
‫and more optimized hard drives
‫to store our cached content.
‫In additions, CDNs also reduce the bandwidth
‫by compressing the content delivered over the network
‫using techniques like Gzip,
‫or minification of JavaScript files.
‫Now, there are generally two strategies that we can use
‫when integrating with Content Delivery Networks
‫in regards to caching content.
‫The first strategy is called a Pull Strategy.
‫In this strategy,
‫all we need to do is tell the CDN provider
‫which content we want on our website to be cached
‫and how often this cache needs to be invalidated,
‫which can be configured by setting the Time To Live property
‫on each asset or type of asset.
‫In this model, the first time a user requests
‫a certain asset,
‫the Content Delivery Network will have to populate its cache
‫by sending a request to a server in our system.
‫Once that asset is cached on the CDN,
‫subsequent request by users will be served
‫by the edge servers directly.
‫This will save all the network latency
‫associated with the communication with our servers.
‫Now, when I use a request on asset
‫that has already expired,
‫the CDN will send a request to our servers
‫to check if we have a new version for that asset.
‫If the version of the asset did not change,
‫the CDN will simply refresh
‫the expiration time for that asset
‫and serve it back to the user.
‫Otherwise, if a new version of that asset is available,
‫the server will send it back to the CDN
‫and the CDN will serve the new version
‫instead of the old one to the user.
‫Now, the second strategy is called the Push Strategy.
‫In this strategy, we manually or automatically
‫upload or publish the content
‫that we want to be delivered through a CDN.
‫And whenever that content changes in our system,
‫we are responsible to republish the new versions
‫to the edge servers.
‫Now, it's worth pointing out
‫that some CDN providers support this model directly
‫while others enable the strategy
‫by simply setting a very long TTL for our assets,
‫so the cache essentially never expires.
‫And whenever we want to publish a new version,
‫we simply purge the content from the cache,
‫which forces the CDN to fetch that content from our servers
‫whenever a user requests that content.
‫Now, before we can choose the right strategy for us,
‫we need to understand the advantages
‫and disadvantages of each of those strategies.
‫So the main advantage of the Pull Model
‫is, of course, lower maintenance on our part
‫because once we configure which assets
‫need to be cached by the CDN
‫and how often they need to expire,
‫we don't need to do anything
‫to keep those CDN caches up-to-date.
‫Essentially, from that point on,
‫everything is going to be taken care of by the CDN provider.
‫Now, the obvious drawbacks
‫are the fact that the first users to use a certain asset
‫that hasn't been cached by the CDN yet
‫will have a longer latency
‫because the CDN will need to fetch that asset first
‫from our system.
‫Another drawback is if we set the Time To Live
‫for all the assets to be the same,
‫we may see those frequent traffic spikes
‫when all the assets expire in the same time.
‫This will result in a large number of requests
‫coming from the CDNs to refresh its caches.
‫Now, while the Pull Strategy
‫definitely adds to our system's availability,
‫we still need to maintain
‫a general high availability of our system
‫because if certain assets expire on the CDN
‫and our system is not available
‫for the CDN to pull a new version,
‫the user will get an error.
‫On the other hand, in the Push Strategy,
‫if our content doesn't change too frequently,
‫we can simply push it once to the CDN.
‫And from that point on,
‫the traffic will go directly to the edge servers.
‫This significantly reduces the traffic to our system
‫and also reduces the burden for our system
‫to maintain high availability
‫because even if our system goes down temporarily,
‫users will still get all the data from the CDN
‫and won't be affected
‫by our system's internal issues at all.
‫On the other hand, if our content does change frequently,
‫we have to actively publish new versions
‫of the content to the CDN.
‫Otherwise, users will get stale and out-of-date content,
‫which is definitely not what we want.
‫In this lecture,
‫we learned about the Content Delivery Network,
‫which is a service that we used
‫in conjunction with our system
‫to improve our users' experience.
‫We talked about the different features
‫and capabilities of CDNs,
‫which add to our system's performance, availability,
‫and security.
‫And finally, we talked about the two strategies
‫for publishing our content to a CDN,
‫which are the Pull Strategy and the Push Strategy.
‫And after that, we compared the two
‫by listing the advantages and disadvantages
‫of each publishing model.
‫I hope you're having fun
‫and I will see you soon in the next lecture.