Content delivery networks (CDNs) are most useful when we want to serve static files to our users like CSS, JS, HTML or any image files. It gives better user experience to user when they frequently access these static content. In this blog, we are going to cover about CDN, benefits of using CDN, types of CDNs and differences between those CDNs.
What is CDNs?
Content delivery networks hosts the static content which are being used by all the users all round the world. What kind of content? These could be any static files like CSS, JS, HTML etc. See below example. Suppose I am developing a web application and I need to use bootstrap css then I will use CDN url.
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
In above example, bootstrap css, bootstrap js and jquery js, all these static files we have referenced from CDN instead of providing relative path of our server. If we provide our server's path then each time browser will request to our server and use those files. Assume if server is in difference region then definitely it takes more time to download those files from our server. Lets take an example that if user who is requesting is in Japan and server is in USA then it takes time to load that web page of application. Where as in case of CDN, if user is from Japan then these files might be hosted / cached near somewhere to Japan like Hong kong. So these files will be served to user quickly.
Advantages of using CDN
- Content can be serve fast to the user because packets needs to travel less distance which means faster download.
- Our own server can serve other request as these request won't come to our server so its saves overall computing power of our server.
We can also setup our own CDN server However there are other paid cloud services available in the market Like Amazon's Cloudfront, Azure's Content delivery network service etc. With paid CDNs, generally you are charged by the amount of data transferred from your server to client each month.
Types of CDNs
1. Pull CDN
When user request for any file and if its not available on CDN then CDN will request for those files to your server. CDN will cache these files and then it will serve to user. So first time user won't see any much speed but next time onwards, as CDN has already cached that file, it will be directly serve to the user from CDN itself.
Problem with this approach is that first request of the file won't see any much difference. Another problem with this approach is that as CDN has cached the file, user won't get the updated file. After cache expires, when CDN again request to your server then only it will get the updated file from your server. Lets take an example that if some JS file being cached for 24 hours in CDNs then if there is new content available on your server for that file However, it won't be served and user will get new content after only cache expires on CDNs.
2. Push CDN
Instead of pulling the content from your server whenever its needed by CDNs, you will upload the content to CDN beforehand. So CDNs can cache those static content and can serve to your user.
Problem with this approach is that if you frequently update the content and if your server is already serving other heavy traffic also then this sync might create more trouble than good.
Generally, Pull CDN is is easier to configure than Push CDN. You need to choose either of the CDN type, recommendation is that keeps track of your usage over the first couple of months as well as loading times.