This is a follow up post on my previous post about optimizing your website. Few weeks ago I was planning to move some files in my website to CDN(content delivery network) for faster download of files. I thought about it and I was reminded of the issues faced by me and my friends when used CDN for my client websites. Here are few of the issues we faced when we started using CDN. My first advice when using CDN is be cautious about billing. Track the usage and billing on a daily basis initially.

Static contents

It is better to push your static files to CDN. These file don’t change frequently and there won’t be too many versions of these files to store and download. This should be your first step in experimenting with CDN. Track the bandwidth usage and your site’s performance once you push your static content to CDN and also CDN billing.

Identify list of pages

Not all pages on the website need to be moved to CDN. For example the privacy policy page, disclaimer, site terms page etc does not need to be on CDN because these pages don’t get high traffic. Mostly users host their home page content on CDN. Use Google Analytics or other analytics tools to figure out which pages are most frequently visited by your users and also which ones are very slow. Optimize your page for faster performance before pushing them to CDN.

Dynamic Content

If your page loads different content based on various query string parameter combination then CDN will cache the content of the page for all possible query string parameters combination that have been requested by the users so far. This will increase your CDN bill. Some sites have their home page content changed based on the cookies in the user’s system from their previous visit to their site. This complicates the caching in CDN because the CDN feels that the page is changing very frequently and it keeps the latest copy. It is very important to decide on how you code your dynamic content pages without any query string parameters.

Protection against BOT

Having mentioned about the dynamic content I would like to highlight that BOTs can increase your CDN billing. Some hackers write bots that will start fetching pages on your site using random query string parameters. If the page is accessed via CDN then CDN will start caching all the copies of the page for various query string parameter. You should have rules in place at load balancer level or alerts in place so that if you get unusual traffic then look at it immediately and block those bots. Google’s crawling bots are intelligent but some unruly bots are very bad. I have seen some sites hosted on shared hosting run out of bandwidth because of this.

CDN having outdated content

One of the problems that sites using CDN face is that the CDN has outdated content. It is because when sites release new version the team forgets to reset the CDN cache. Sometimes it is big risk to reset all the contents in CDN after a new release because your site will be getting a flood of requests from CDN if it is a high volume website. It is better to set expiry date in HTTP header for all the contents provided by your website to CDN so that when the content expires CDN will automatically fetch its newer version from your website. It is better to decide which items to expire hourly, daily, weekly and upon certain events like new release.

SSL for CDN domains

If your site is on HTTPS then it is better to have the CDN domain also on HTTPS so that users don’t get warning about unsecured content on the site. A note of caution is that don’t post sensitive and individual information on CDN. There is no way you can control users who access the CDN domain directly.

Setting region for CDN domains

If most of your traffic is from your country then setup the CDN to serve only the geographic regions you intend too so that it will save some money on your billing.

Redirects for resources on CDN domain

Almost all the sites I have worked on have 301 and 302 redirects to new pages. The CDN also caches these redirects so set the redirect codes correctly. If it is set to 301 then you cannot expect CDN to come back to your site to see if that 301 is changed or not until CDN cache is cleared. When you set 302 setup expiry time for that redirection so that CDN does not check for the status of the redirection for every request.

I hope these tips help you to setup CDN for your website and improve the performance of your website.

If you need help or suggestions for your products you can get it touch with me here or you can post your question to my social accounts below.