What Is Caching?
Definition
Cache
- In plain English
- A cache is a small, fast store that keeps copies of data you'll probably need again.
- Think of it like
- It is like keeping your most-used spices on the counter instead of in the cellar.
Cache hit and miss
- In plain English
- A cache hit means the data was found in the cache, while a cache miss means it had to be fetched from the slower source.
- Think of it like
- It is like finding milk in your fridge versus having to go to the shop.
Why it exists
Your database needs 40 ms to answer a query. Thousands of people keep asking for the same vote total.
A nearby memory store can answer much faster. Reusing one answer leaves the database free for other work.
How it works
Hit ratio
- In plain English
- The hit ratio is the share of requests the cache answers without going to the source.
- Think of it like
- Like how often the spice you want is already on the counter.
Stale data
- In plain English
- Stale data is a cached copy that no longer matches the source.
- Think of it like
- Like an old timetable pinned to a fridge.
| Layer, from user outward | What it keeps |
|---|---|
| Browser cache | Files on your device |
| Worldwide delivery cache | Public files near users; lesson 4 names this network |
| Application memory | Copies inside one running application |
| Shared cache server | Copies available to several application servers |
| Database memory | Recently used database pages |
The app checks the cache, returns hits, and loads misses from the database before saving and returning them.
- Assume each cache lookup takes 1 ms and each database read takes 40 ms.
- With 90% hits, average time is 0.9 × 1 + 0.1 × 41 = 5 ms.
- This excludes other request work and includes the cache lookup on a miss.
Examples
Tv voting
During a live final, vote totals receive one million reads per minute. Viewers accept totals up to one second old.
One shared total, refreshed once per second, needs about 60 database reads per minute.
- This assumes one coordinated refresh for that total.
- Separate totals or independent caches need additional reads.
The insight
A tiny freshness allowance removes huge load.
Pet adoption
Pet profile pages change a few times a day but are viewed thousands of times. A shared cache holds each rendered profile.
Keep a rendered profile in a shared cache. Remove its copy when shelter staff update the profile.
Browser → Application → Shared cache → Database on miss.
The insight
Cache data that is read far more often than it changes.
Weather sensors
City forecasts are cached for ten minutes. For this app, fresher forecasts would not change users’ plans.
The product team accepts a ten-minute delay for this forecast. Choose freshness from the user need.
key: forecast:pune
value: cloudy, 26 °C
expires_in_seconds: 600The insight
Let the business decide how stale is acceptable.
Trade-offs
Pros
- Faster repeated reads.
- Fewer database queries.
- More read capacity.
Cons
- Copies can become stale.
- Another store needs care.
- An empty cache sends reads to the database.
Use when
- Data is read far more often than it changes.
- Users accept a defined freshness delay.
Avoid when
- A transfer needs the authoritative balance.
- Reads rarely repeat.
Quick check
Takeaways
- A cache keeps a fast nearby copy.
- Hit ratio measures the share of requests answered by the cache.
- Choose acceptable staleness before choosing a cache.