Website Performance
Cache Hit vs Cache Miss: Hit Ratio and Its Effect on Speed
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيInstalling a cache does not guarantee a faster site. What matters is how often the cache actually answers. Every request either finds what it needs in the cache, a hit, or has to go to the slower source behind it, a miss. The share of hits, called the hit ratio, is one of the most useful numbers in web performance, because it tells you whether your caching setup is working or just consuming memory. This article explains hits and misses, why misses happen, how to measure your ratio on a VPS, and how to improve it.
Hit and miss in plain terms
- Cache hit: the requested item is in the cache and still valid. It is returned immediately.
- Cache miss: the item is absent or expired. The system fetches it from the origin (PHP, the database, the disk), returns it and usually stores it for next time.
A miss is not just "no speedup". It is often slightly slower than having no cache at all, because the system checks the cache first, then does the full work, then writes the result.
Why the hit ratio matters so much
The hit ratio is:
hit ratio = hits / (hits + misses)Average response time depends heavily on it. Consider an example where a hit takes 5 ms and a miss takes 300 ms:
| Hit ratio | Average response time |
|---|---|
| 50% | about 153 ms |
| 80% | about 64 ms |
| 90% | about 35 ms |
| 99% | about 8 ms |
These numbers are illustrative, but the pattern holds for any real system: the last few percentage points matter enormously, because each remaining miss is so much more expensive than a hit. Going from 90% to 99% cuts misses from ten in a hundred to one in a hundred, and the load on your backend by the same factor.
Types of cache misses
Understanding why a miss happened tells you how to fix it.
- Cold miss: the item was never cached. This happens after a restart, a deploy or a full purge, and on the first visit to any page.
- Capacity miss: the cache is too small, so useful items get evicted to make room for others.
- Expiry miss: the item was cached but its TTL ran out. Very short TTLs cause many of these.
- Bypass miss: rules intentionally skip the cache, for example for logged-in users, POST requests or URLs with query strings.
- Fragmentation miss: the same content is stored under many different keys, for example when tracking parameters such as
utm_sourceorfbclidmake each URL unique.
Measuring your hit ratio
Nginx page cache
Add the cache status to a response header and to your logs. In the http context:
log_format cache '$remote_addr [$time_local] "$request" $status $upstream_cache_status';Then, in the server block, access_log /var/log/nginx/cache.log cache;. After some traffic, count the results:
awk '{print $NF}' /var/log/nginx/cache.log | sort | uniq -c | sort -rnYou will see counts for HIT, MISS, BYPASS, EXPIRED, STALE and others. A large BYPASS count points to your skip rules; a large EXPIRED count suggests TTLs are too short.
Redis
Redis keeps counters since its last restart:
redis-cli info stats | grep -E "keyspace_hits|keyspace_misses|evicted_keys"Divide hits by hits plus misses. A rising evicted_keys value means Redis is running out of its configured memory and you are seeing capacity misses.
OPcache
Your application or a small PHP status script can call opcache_get_status(), which reports opcache_hit_rate. On a stable site it should stay very high; a low value usually means the cache is full or max_accelerated_files is too small.
How to raise your hit ratio
- Normalise cache keys: strip or ignore marketing parameters so
/page?utm_source=xand/pageshare one entry. In Varnish this is done in VCL; in Nginx you can build the cache key from selected variables instead of the full$request_uri. - Review bypass rules: make sure a cookie that every visitor gets does not disable caching for everyone.
- Use sensible TTLs: longer lifetimes for content that rarely changes, combined with targeted purges as described in our cache invalidation guide.
- Size the cache properly: increase
max_size, Redismaxmemoryor OPcache memory if evictions are frequent and the server has RAM to spare. - Warm the cache after deploys: request your most important URLs with a simple script so real visitors do not pay for cold misses.
- Protect against stampedes:
fastcgi_cache_lock onensures only one request rebuilds a missing page.
A simple warm-up script:
while read url; do curl -s -o /dev/null "$url"; done < urls.txtIf you want to see how the layers fit together first, read our website caching guide.
FAQ
What is a good cache hit ratio?
It depends on the layer and the site. A public content site with a page cache can often achieve a very high ratio, while a store with many logged-in users will be lower at the page level. Rather than chasing a fixed number, track the trend and investigate the reasons behind the misses you see.
Why is my hit ratio low right after a deploy?
Deploys and restarts usually empty caches, so every first request is a cold miss. The ratio recovers as traffic refills the cache. Warming key URLs after each deploy and avoiding unnecessary full purges shortens this period.
Can a high hit ratio still hide problems?
Yes. A cache can return stale or wrong content with a perfect hit ratio. Monitor freshness too: verify that edits appear when expected, and make sure personalised pages are never served from a shared cache.