Differences Between Push And Pull CDNs

Introduction 

 
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 the 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 CDN?

 
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 the below example. Suppose I am developing a web application and I need to use bootstrap css. Then I will use a CDN url.
  1. <head>    
  2.   <title>Bootstrap Example</title>    
  3.   <meta charset="utf-8">    
  4.   <meta name="viewport" content="width=device-width, initial-scale=1">    
  5.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">    
  6.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>    
  7.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>    
  8. </head>     
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 that the server is in a different region. Then it definitely takes more time to download these files from our server. Let's take an example of a user who is requesting in Japan and the server is in the USA. Then it takes time to load the web page for the application. However, in the case of CDN, if the user is from Japan, then these files might be hosted/cached somewhere close to Japan, like Hong Kong. These files will be served to user quickly.
 

Advantages of using CDN

  1. Content can be serve fast to the user because packets needs to travel less distance which means faster download.
  2. 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

 
Pull CDN
 
When a user requests any file and it's not available on CDN, then CDN will request those files to your server. CDN will cache these files and then it will serve to user. The first time, the user won't see any much speed, but the next time onwards, as the CDN has already cached that file, it will directly serve the user from the CDN itself.
 
The problem with this approach is that the first request of the file won't see much of a difference. Another problem with this approach is that as CDN has cached the file, the user won't get the updated file. After the cache expires, when the CDN again is requested to your server, then it will get the updated file from your server. Let's take an example of some JS files being cached for 24 hours in CDNs. If there is new content available on your server for that file, it won't be served and the user will get new content after the cache expires on CDNs.
 
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 static content and can serve your user.
 
The 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 types. It's recommended that you keep track of your usage over the first couple of months as well as loading times.