Content Delivery Network
Content delivery network is a network of servers which are geographically dispersed and are used to deliver static content to the user. Some examples of static content include: Images, videos, CSS, etc.
How does CDN works?
Whenever a user visits website then CDN server which is closest to the user is going to deliver above mentioned static content. This has two advantages: First, user requests are served faster and second website loads are faster. But with increase in the distance of user from the server will increase this website load time.
Whenever user tried to static content from the website using it’s URL. CDN server will try to fulfil this request by checking for this content in it’s storage. If CDN server doesn’t have it, then it will request the file from the origin or online storage. The origin is going to provide the requested content and will provide an optional HTTP header TTL (Time to live) which will describe for how long your content is cached. CDN server caches the content/file and returns it to user. File will stay in CDN server until TTL expires. Now if there’s any user who requests for the same file then CDN sever will return the file from its cache if it’s TTL has not expired.
Things to Consider before using CDN:
CDNs are usually run by third party service providers and you will be charged for the data transfer that happens in and out of CDN. So cost is one of the factors you should consider before using CDN.
Incase CDN server goes down your application should have backup plan and it should detect the issue and do the needful like if CDN server is down serve requests directly from origin till CDN server is restored.
TTL for your static objects should be set as per the content which has to be stored. There can be time sensitive content and having a short TTL for such content is really important. Similarly, TTL should not be vert small or big. As this will simply either increase the loading of content from the server instead of CDN Cache or will serve stale content to the user.