Transcript for:
Understanding Content Delivery Networks

imagine a company is hosting a website on a server in an AWS data center in California it may take around 100 milliseconds to load for users in the US but it takes three to four seconds to load for users in China fortunately there are strategies to minimize this request latency for users that are far away and you always have to keep these strategies in mind when designing or Building Systems on a global scale what are cdns cdns or content distribution and delivery networks are a modern and popular solution for minimizing request latency when fetching static assets from a server an ideal CDN is composed of a group of servers spread out globally hence no matter how far away a user is from your server they'll always be close to a CDN so then instead of having to fetch static assets like images videos and HTML CSS JavaScript from the origin server users can quickly fetch cached copies of these files from the CDN remember that static assets can be pretty large in size think of an HD wallpaper image by fetching that file from a nearby CDN server we also save a lot of network bandwidth popular cdns Cloud providers typically offer their own CDN Solutions since it's so popular and easy to integrate with their other service offerings some popular cdns include cloudflare CDN AWS cloudfront gcp Cloud CDN Azure CDN and Oracle CDN how do cdns work a CDN is a globally distributed group of servers that cache static assets for your origin server every CDN server has its own local cache which should all be in sync there are two primary ways for a CDN cache to be populated which creates a distinction between push and pull cdns in a push CDN the engineers must push new or updated files to the CDN propagating them to all of the CDN server caches in a pull CDN the server cache is lazily updated that is when a user sends a static asset request to the CDN server and it doesn't have it it'll fetch the asset from the origin server populate its cache with that asset and then send it to the user if the CDN has the asset in its cache it Returns the cached asset there are advantages and disadvantages to both approaches in a push CDN it's more engineering work for the developers to ensure that assets are always up to date on the other hand pull cdns require less maintenance since the CDN will automatically fetch assets from the origin server that are not in its cache the downside of pull cdns is that if they already have have your asset cached they won't know if you decide to update it or not and then fetch this updated asset so for some time a pool cdn's cash will become stale after assets are updated on the origin server another downside is that the first request to a pull CDN will always take a while since it has to make a trip to the origin server even with their disadvantages though pull cdns are still much more popular than push cdns because they're much easier to maintain there are also several ways to reduce the time a static asset is still for example pull cdns usually attach a timestamp to an asset when cached and they typically only cash the asset for up to 24 hours by default if a user requests an expired cash in the CDN cache the CDN will refetch the asset from the origin server and get an updated asset if there is one another solution is Cash busting where you cash assets with a unique hash or e-tag compared to previous asset versions when should you not use cdns cdns are gen generally a good service to add to your system for reducing request latency for static files but not most API requests however there are some situations where you do not want to use cdns if your services Target users are in a specific region then there won't be any benefit of using a CDN as you can just host your origin servers there instead cdns are also not a good idea if the assets being served are Dynamic and sensitive for example you don't want to serve stale data for sensitive situations such as when working with financial or government services an example interview question about cdns might be imagine you're building Amazon's product listing service which serves a collection of product metadata and images to online Shoppers browsers where would a CDN fit in the following design you can answer in the comments below and let us know what you think I hope this video gave you a little bit of insight into cdns and how they work