Top 4 mistakes when setting up a CDN
When speaking of websites, faster is always better. Don't let the customers leave your website because of the page load time. Utilization of a CDN is a very effective way to deliver your data faster. However, if you don't set your CDN up correctly, you can't fully exploit its potential. The most common mistakes in a CDN setup cause a decreased or zero cache-hit ratio. That means the requests are not hitting the cache on our edge servers and are rerouted to the origin server.
Cache hit ratio
The goal is to optimize your cache hit ratio so the requested data can be delivered from the cache servers and reach your end-users faster. Cache hit ratio is a percentage value between cache hits and cache misses. The higher your cache hit ratio is, the faster is the data delivery. Cache hit - requested data is retrieved from the CDN Cache miss - requested data are not in the cache, so the request is forwarded to the origin server. The data are retrieved from the origin and passed to the end-user.
Hot Cache - a state in which the CDN effectively delivers the cached assets from edge servers.
Cold Cache - a state in which the CDN has no assets in the cache to provide upon request.
Headers - parts of the files used for object/request information exchange between client and server devices.
Setting the cache-control headers
CDN providers would love to cache all your data. However, you might want to exclude some of your files from being stored with them. To do that, you would usually set up a special “cache-control: no-cache” header forbidding CDN/proxy/ browser to store the specific files.
Sometimes, unfortunately, it happens that this header is present at every single file. CDN servers respect your orders and as a result, none of your content is cached and many advantages that a CDN brings are lost.
This situation is easy to spot in most CDNs reports by seeing most or all of your traffic as uncached.
Only set "cache-control: no-cache” headers for files that you truly want to exclude from your content delivery network. Remove these headers from every other file on your website.
Navigate to the “Reports” tab in your dashboard and click on the “Bandwidth” button. You can see all the uncached traffic in purple color.
Setting the expiry / max-age headers
Being able to control the maximum period of time that a CDN is allowed to store your content is great. However, some websites are sending “cache-control: maxage=0” for every file.
Even if other settings are fine, no content will be cached and the advantages of using a CDN are lost. Same as in the previous case, you will see most or all the traffic in your reports uncached.
Check if you are sending the proper “cache-control: max-age=" or "expiry:" values in your headers.
Caching the site while setting cookies
This may be a problem since most CDN providers don't cache any files with set-cookies by default.
If you are setting cookies for all your files - be that for advertising or any other reason - you would see all traffic uncached.
If you want your files cached on the CDN, even though you send cookies with them, the easiest way is to take a look in your CDN settings and find an option for ignoring cookies.
If your CDN doesn't support this general setting, check the files on your origin. You may be sending “set cookies” headers with the files you want to be cached on the CDN. If that's your case, simply remove them.
Click on CDN tab and select CDN Resource. In “Configuration”, check the “Ignore set cookies” checkbox and save the settings.
Setting the expiration time
You have the option to set up the cache expiry even without using the HTTP headers.
You can set this value generally for the whole CDN Resource in your dashboard. If you have a high miss ratio (a big part of your traffic is non-cached), it might be (in very specific cases) due to the incorrect setting of this value (data being cached for only a short period of time). However, if you’re sending max-age and expiry headers, then we will respect the value of these rather than the value you set in the dashboard.
Set a longer period for your cache expiry.
Navigate to the CDN tab in your dashboard and select CDN Resource. Click on “Configuration” button. In the “Cache expiry” part, select a longer period of time and save the changes. Please also note that if there is a conflict between the max-age and the expiration time header, “max-age” has a higher priority.