What Is Caching?
Overview
A cache is a small, fast storage that holds copies of data so the next request for it does not have to go all the way to the slow source. It is the cheapest big speed-up in system design.
In this lesson you learn the basic vocabulary, why caching works, and where caches live.
Detailed Notes
What is a cache
A cache is a fast, temporary store holding copies of data that is expensive to get from its original place. The original place is called the source of truth, usually a database.
- Cache hit: the data was in the cache. Fast.
- Cache miss: it was not there, so we fetch from the source (and usually save a copy).
- Hit ratio: hits divided by total lookups. The main score of a cache.
- TTL (time to live): how long an entry may stay before it expires.
- Stale data: a cached copy that no longer matches the source.
The problem it solves
Reading from a database or a far-away server is slow compared with reading from memory. And databases are hard to scale (more servers do not make one slow query fast). Yet users keep asking for the same things: the same product page, the same profile, the same top stories.
Caching works because of skewed popularity: a small share of items gets most of the traffic. If you keep just those hot items close, most requests never touch the database.
How it works
The basic read flow:
- A request comes in for some key (for example
product:42). - Look in the cache.
- Hit: return it immediately.
- Miss: read from the database, store a copy in the cache with a TTL, return it.
- The next request for the same key is a hit.
How much does it help? Average latency = hit ratio x cache time + (1 - hit ratio) x source time. With a 1 ms cache and a 50 ms database:
- 50% hits: about 25 ms
- 90% hits: about 6 ms
- 99% hits: about 1.5 ms
Going from 90% to 99% also cuts database load tenfold (10% of requests reach it, then 1%). Small hit-ratio gains matter a lot.
Where caches live (closer to the user is faster, but harder to keep correct):
- Browser cache: on the user's device, controlled by HTTP headers such as
Cache-Control. - CDN: servers near users (next chapter).
- Reverse proxy or gateway cache: full responses stored in front of your app.
- Application cache: inside one app server's memory. Fast, but every server has its own copy.
- Distributed cache: a separate shared cluster such as Redis or Memcached. Redis supports richer data types and can save data to disk; Memcached is a simpler pure cache.
- Database cache: the database keeps hot data in memory by itself.
A simple example
A librarian keeps the ten most-requested books on the desk instead of walking to the warehouse every time. Most readers ask for those ten, so most requests are answered instantly. A rare book still needs the trip to the warehouse.
System design example
A news site's front page runs a database query taking 80 ms, at 5,000 views per second. The data changes once a minute. The team caches the rendered list in Redis for 30 seconds. Now the database sees about two queries a minute for that page, and the page loads in a couple of milliseconds. The trade-off: readers may see news up to 30 seconds old.
Summary
- A cache is a fast copy of data; a hit is served from it, a miss goes to the source.
- Caching works because a few items get most of the traffic.
- Hit ratio decides how useful a cache is.
- Caches exist at many layers: browser, CDN, proxy, app, shared cache, database.
- The price is stale data: speed and scale are traded for freshness.
Architecture Diagram
Each layer answers what it can and passes misses inward.
- 1The browser answers from its own cache if it can.
- 2Misses go to the CDN, then to the application.
- 3The app checks local and shared caches.
- 4Only remaining misses reach the database, which then fills the caches.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

