Cache Hit vs Cache Miss
Table of Contents + −
Have you ever noticed this? Like, you open a web page for the first time and it feels a little slow. Then you refresh it, or come back to it a minute later, and boom, it’s instant.
So what changed? The page didn’t get smaller. The internet didn’t get faster. Here’s the real reason:
- The first time, the app had to go fetch the data from somewhere far and slow, like a database or another server.
- After that, it kept a copy nearby in a cache, which is just a small, fast store of data sitting close by.
- So the second time, it grabbed the copy instead of doing all that work again.
That first slow trip is a cache miss. That second instant trip is a cache hit. These two words are the heart of how caching works, and once you really get them, a lot of system design clicks into place. Let’s go through them one at a time.
✅ What is a Cache Hit
A cache hit is the happy case. Let’s define it clearly first:
- A cache hit means the data you asked for was already sitting in the cache, so it got returned straight from there.
- No trip to the database, no call to a far-away server. Just grab and go.
- This is the whole point of having a cache, because reading from the cache is way faster than reading from the original source.
Think of it like your fridge at home. If you want milk and there’s already milk in the fridge, you just open it and pour. That’s a hit. You didn’t have to drive to the store.
Why hits feel instant
A cache usually lives in fast memory (RAM) and sits close to the app. So a hit can be hundreds of times faster than going all the way to a database on disk. That speed gap is exactly why we cache in the first place.
❌ What is a Cache Miss
A cache miss is the other case. Here’s what it means:
- A cache miss means the data you asked for was not in the cache.
- So the app has to go fetch it from the real source, like the database or another service. That extra trip is slower, and the wait it adds is called the cache miss penalty.
- Then, and this is the key part, the app usually stores that fetched data in the cache on the way back. So the next time someone asks for the same thing, it’ll be a hit.
Back to the fridge. You want milk, but the fridge is empty. So now you drive to the store, buy milk, come home, and put it in the fridge. That trip is the miss. But notice, now the milk is in the fridge for next time.
There are a few different reasons a miss can happen, and it helps to know their names:
- Compulsory miss. The very first time anyone asks for a piece of data, it can’t be in the cache yet, because nothing has loaded it. This first-touch miss is unavoidable.
- Capacity miss. The cache is full and only holds so much. So older data got pushed out to make room, and now it’s gone when you ask for it again. (Pushing data out is called eviction.)
- Expiration miss. The data was there, but it sat past its time limit and got cleared out. That time limit is the TTL, short for time to live.
A miss is not an error
A cache miss does not mean something broke. It just means the data wasn’t cached, so the app fetched it the normal way. Your app still returns the right answer, it just takes a bit longer this one time.
🔁 The Hit-Then-Store Flow
Let’s put both cases into one picture. Every request asks the cache first, and then one of two things happens. Walk through this slowly:
See the nice part? Every miss quietly fills the cache for next time. So the more a popular piece of data gets requested, the more it ends up living in the cache as a hit. The system kind of warms itself up as people use it.
Here’s the same thing side by side, so the difference is crystal clear.
| Cache Hit ✅ | Cache Miss ❌ | |
|---|---|---|
| Is the data in the cache? | Yes | No |
| What happens | Returned straight from the cache | Fetched from the source, then stored in the cache |
| Speed | Very fast | Slower (pays the miss penalty) |
| Load on the database | None | Adds a query to the source |
📊 Hit Ratio
So how do we measure whether a cache is doing its job? We use the hit ratio, also called the hit rate. Here’s the definition:
- Hit ratio is the share of requests that were hits, out of all the requests.
- The formula is simple: hits ÷ total requests.
- Higher is better, because a higher hit ratio means more requests are getting served the fast way and fewer are bothering the database.
Let’s do a tiny example so the number feels real. Say your cache handled 1000 requests in an hour:
- 900 of them were hits (found in the cache).
- 100 of them were misses (had to go fetch).
- Hit ratio = 900 ÷ 1000 = 0.9, which is 90%.
So 90% of the time, users got the fast path. That’s a healthy cache. People also talk about the miss ratio, which is just the flip side: 100 ÷ 1000 = 10%. Hit ratio and miss ratio always add up to 100%.
What's a good hit ratio?
There’s no single magic number, it depends on the workload. But generally, the higher the better. Many real systems aim for somewhere in the 80 to 95 percent range. If your hit ratio is very low, your cache might be caching the wrong data, or it might be too small.
🥶 Cold Cache and Cache Warming
Now here’s a situation that catches a lot of people. When does a cache have the most misses? Right at the start. Let’s define this:
- A cold cache is an empty cache. It happens right after the app starts up, or after a restart, or when you deploy a fresh server.
- Because it’s empty, the first requests all miss. There’s simply nothing in there yet to hit.
- This is why an app can feel sluggish for the first few minutes after a restart, and then settle into being fast.
So the cache starts cold, and it slowly heats up as real traffic fills it with data. That’s normal. But sometimes you don’t want users to feel that slow start, especially right after a big deploy.
That’s where cache warming comes in:
- Cache warming means pre-loading the popular data into the cache before real users show up.
- So instead of waiting for users to trigger misses, you load the hot items yourself ahead of time.
- Then when traffic arrives, those first requests are already hits. The cache is warm from the very first second.
Think of it like preheating an oven before you bake. You don’t wait for the cake to warm the oven up. You warm it first, so the moment the cake goes in, it’s ready.
🎯 How to Improve Hit Rate
Okay so we want a high hit ratio. How do we actually get there? A few practical moves help the most:
- Cache the right data. Focus on your hot data, the stuff that gets requested again and again. Caching things nobody asks twice just wastes space and barely helps your hit rate.
- Set a sensible TTL. TTL is how long an item stays before it expires. Too short and things expire before they get reused, causing needless misses. Too long and users see stale, out-of-date data. You want the sweet spot for your data.
- Give it enough memory. If the cache is too small, it fills up fast and keeps evicting useful data, which turns into capacity misses. A bit more room can lift the hit ratio a lot.
- Warm it up. Pre-load popular items after a restart or deploy, so you skip that cold, miss-heavy start.
The thing is, these all work together. A right-sized cache, holding the right data, with the right TTL, warmed at startup, tends to give you a strong hit ratio without much fuss.
⚠️ Common Mistakes and Misconceptions
A few ideas trip people up early. Let’s clear them out:
- “A cache miss is a failure.” No. A miss just means the data wasn’t cached, so the app fetched it normally and returned the correct result. It costs a little time, that’s all.
- “A 100% hit ratio is the goal.” Not really, and it’s basically impossible anyway. There’s always a first request for new data (a compulsory miss), and data changes over time. Chasing a perfect 100% usually means caching stale data or wasting memory. A high, healthy ratio is the real target.
- “The cache is always fast, so I don’t need to think about cold starts.” Watch out. Right after a restart the cache is cold and empty, so everything misses for a bit. If you ignore that, your app can feel slow at exactly the wrong moment, like right after a deploy under heavy traffic.
- “More caching always helps.” Caching rarely-used data adds overhead and clutters the cache without lifting your hit rate. Cache the hot data, not everything.
🛠️ Design Challenge
Try this one on your own to test yourself.
Imagine you run a news website. The homepage shows the top 10 trending articles, and millions of people hit it every hour. You add a cache, but the hit ratio is only around 40%, which is low. Walk through what could be wrong. For example:
- Maybe the TTL is too short, so the trending articles keep expiring before the next reader arrives.
- Maybe the cache is too small, so it keeps evicting the trending articles to store one-off pages nobody re-reads.
- Maybe the cache goes cold every deploy, and you deploy often, so it’s always restarting from empty.
See how many causes you can name, and write down one fix for each. This is exactly how you’d reason about cache performance in a real interview.
🧩 What You’ve Learned
You can now talk about how a cache really performs. Here’s what you’ve picked up.
- ✅ A cache hit means the data was in the cache and got returned fast.
- ✅ A cache miss means the data wasn’t there, so it got fetched from the source and then stored for next time.
- ✅ Hit ratio is hits ÷ total requests, and higher is better.
- ✅ Misses come in flavors: compulsory (first touch), capacity (cache full), and expiration (TTL ran out).
- ✅ A cold cache is empty after a start or restart, so everything misses until it warms up.
- ✅ Cache warming pre-loads popular data so the cache is fast from the first second.
- ✅ You lift hit rate by caching hot data, setting a sensible TTL, giving enough memory, and warming the cache.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is a cache hit?
Why: A hit means the data you asked for was already in the cache, so it is returned straight from there without a source trip.
- 2
If a cache handles 1000 requests and 900 are hits, what is the hit ratio?
Why: Hit ratio is hits divided by total requests, so 900 divided by 1000 is 0.9, which is 90%.
- 3
What is a cold cache?
Why: A cold cache is empty, so until traffic fills it the first requests all miss and the app feels slow for a bit.
- 4
Which is a reason a cache miss can happen?
Why: An expiration miss happens when the item sat past its TTL and was cleared out, alongside compulsory and capacity misses.
🚀 What’s Next?
You now understand the two outcomes every cache lookup leads to, and how to measure and improve them. Next, let’s see what happens when a cache fills up and has to decide what to throw out.
- Cache Eviction Policies explains how a cache picks which data to remove when it’s out of room, with strategies like LRU and LFU.
- Introduction to Caching is a good refresher on what caching is and why it makes systems fast at scale.
Once you’ve got those, you’ll have a solid grip on how caching keeps real systems fast.