The cache hit percentage (CHP) gives precise information about the amount of files served from the CDN without any request to the origin server. In this post we will highlight what has an impact on CHP and what can be done to improve it. The cache hit ratio will only apply for pull zones.
The CHP is measured among all files served from a pull zone regardless of the file size or file type. The CHP can significantly vary depending on the environment and the setup. A typical CHP for a normal website can easily be as high as 95%.
A number of factors can influence the CHP in various ways:
- Query strings: If query strings (QS) are not ignored, each QS will create a new cache entry. This can be an important feature to distinguish different version of a file but it will cause extra cache entries if not used properly. QS can be ignored (see zone settings) and regardless of the query string, it will always be cached as the same file.
- Expire headers: KeyCDN will honor all HTTP headers from the origin server (and overwrite them as defined in the dashboard). It’s important to define meaningful expire values.
- Purge: Each purge (either the entire zone or one particular file) will delete the content on all POPs globally. The file(s) will then be cached again.
- New files: Every new file needs to be initially fetched and cached from each POP all around the world. This will result in one cache miss for each file and POP.
- User generated content (long tail content): The traffic pattern will also have a great impact on the CHP. In case of user generated content, new files need to be cached all the time (which will lead to a lower CHP).
- Not cacheable content: Not all files are cacheable. Files will only be cached based on the cache-control-header. Files with cookies will not be cache if the cookie is not stripped away (see zone settings).
How to Check a Cache Hit/Miss
A separate HTTP header (“X-Cache”) will indicate, if the request has been a cache hit or miss. This can easily be checked with the “curl” command on any Linux/Unix machine with the following command:
curl -I https://website-7.kxcdn.com/img/logo.png HTTP/1.1 200 OK Server: nginx Date: Sun, 02 Nov 2014 04:48:02 GMT Content-Type: image/png Content-Length: 6396 Connection: keep-alive Vary: Accept-Encoding Last-Modified: Sat, 26 Apr 2014 12:11:29 GMT ETag: "535ba271-18fc" Alternate-Protocol: 443:npn-spdy/3,443:npn-spdy/2 Strict-Transport-Security: max-age=31536000; includeSubdomains; preload Expires: Sun, 09 Nov 2014 04:48:02 GMT Cache-Control: max-age=604800 Link: <https://www.keycdn.com/img/logo.png>; rel="canonical" X-Cache: MISS X-Edge-Location: nlam Access-Control-Allow-Origin: * Accept-Ranges: bytes
Alternatively, the HTTP headers can also be check online with services like Pingdom
If the CHP is lower then expected, it’s important to take a closer look and narrow down the possible root cause. In most cases it can be improved with the steps proposed above.