What is a cache hit ratio?

缓存命中率是通过将缓存命中次数除以缓存命中和未命中的总数来计算的,它可以衡量缓存在满足内容请求方面的有效性。

学习目标

阅读本文后,您将能够:

  • 了解缓存命中和缓存未命中之间的区别
  • 了解缓存命中率的计算方法
  • 了解缓存命中率对 CDN 的重要性

复制文章链接

What is a cache hit ratio?

Cache hit ratio is a measurement of how many content requests a cache is able to fill successfully, compared to how many requests it receives. A content delivery network (CDN) provides a type of cache, and a high-performing CDN will have a high cache hit ratio.

The formula for calculating a cache hit ratio is as follows:

Cache hit ratio equation

For example, if a CDN has 39 cache hits and 2 cache misses over a given timeframe, then the cache hit ratio is equal to 39 divided by 41, or 0.951. The cache hit ratio can also be expressed as a percentage by multiplying this result by 100. As a percentage, this would be a cache hit ratio of 95.1%.

Cache hit ratio is a metric that applies to any cache; it's not just for measuring CDN performance. However, it is an especially important benchmark for CDNs. Most CDN services will provide customers with this metric within their user interface or dashboard.

What is a cache hit?

A "cache hit" occurs when a file is requested from a cache and the cache is able to fulfill that request. For instance, if a user visits a webpage that's supposed to display a picture of a cat playing a piano, the browser may send a request to the webpage's CDN for this picture. If the CDN has a copy of the picture in its storage, then the request results in a cache hit, and the picture is sent to the browser.

What is a cache miss?

A cache miss is when the cache does not contain the requested content. If a copy of the cat-playing-piano picture is not currently in the CDN cache, this request results in a cache miss, and the request is passed along to the origin server for the original picture. The CDN server will cache the photo once the origin server responds, so that additional requests for it will result in a cache hit.

What is a good CDN cache hit ratio for most websites?

A typical website that's mostly made up of static content could easily have a cache hit ratio in the 95-99% range. However, getting this metric as high as possible isn't the only goal of a CDN. Additionally, a website with lots of dynamic content may have a much lower cache hit ratio (although caching dynamic content is becoming possible).

Does a high cache hit ratio always mean a CDN is effective?

Cache hit ratio is not the last word in CDN performance; other factors are extremely important in assessing a CDN's effectiveness as well. For instance, where the content is served from is also important. Ideally, a CDN will serve content from the CDN server closest to the end user. If this doesn't occur, the CDN's performance will not be optimal. The Cloudflare CDN is built to serve any content from any of our 200 locations around the world. Any content that is cached in one data center) can be served from every other data center as well.

Caching is an important part of what a CDN does, but its main purpose is to make web properties faster and more reliable in general. A variety of performance metrics help measure how much a CDN has helped to speed up a web app or website.