The cache hit rate is the percentage of requests that result in cache hits. In Varnish it is evaluated as cache_hit / (cache_hit + cache_miss). The higher the hit rate the more effective your cache is. For example if the hit rate is above 80%, it means the majority of requests are fulfilled by an object already present in cache and no backend request is made. This, of course, makes the response time shorter and reduces or completely avoids the chance that your backend will be overwhelmed.
Varnish provides several tools that can be used to monitor the cache hit ratio and help to understand how Varnish decides if and how to cache a page.
Varnishstat:
While running varnishstat your cache hit rate is available on the upper right corner (circled in red in the figure).
To be precise, there are three different hit rate averages evaluated at intervals of 10, 100 and 1000 seconds.
Starting from the first red circle on the left:
Remember that in the hit rate calculation “pass” requests are not considered misses, so your hit ratio can be 100%, but you will still see backend requests.
Varnishtop:
This tool presents a continuously updated list of the most commonly occurring log entries. ‘varnishtop -i BereqURL’
shows you the URLs most requested to the backend.
Varnishlog:
Once you are aware of which URLs hit the backend frequently, you can use this tool to get a complete overview of the request you want to analyze. ‘varnishlog -q ‘BereqURL ~ “/foo”’
shows you the complete transaction matching the specified regular expression.
Combining these three tools can help you understand why Varnish is caching some pages and not others and if your cache policy is acting as you would expect and want it to do.
Some prudence should be used when developing your VCL and tuning Varnish:
if (req.http.Accept-Language ~ "en") { set req.http.Accept-Language = "en"; } elsif (req.http.Accept-Language ~ "de") { set req.http.Accept-Language = "de"; } elsif (req.http.Accept-Language ~ "fr") { set req.http.Accept-Language = "fr"; } else { # unknown language. Remove the accept-language header and # use the backend default. unset req.http.Accept-Language } }
N.B. the same logic applies to any Vary header.
Are you ready to learn more about monitoring your cache hit rate and other Varnish tools?
Photo (c) 2014 by Filipe Ramos used under Creative Commons license.