Caching Strategies Explained
A cache can make a slow site fast in an afternoon, and then show customers the wrong price. The difference between those two outcomes is the strategy: how data gets into the cache, how it leaves, and what happens when the original changes.
System design · 2026-11-25 · 7 min read
What a cache does
A cache is fast storage, usually memory (Redis, Memcached, or the app’s own memory), that keeps copies of answers that are slow to produce: database queries, API responses, rendered pages. Reading from memory takes well under a millisecond; a database query can take tens of milliseconds and only so many can run at once.
Caching works best when the same data is read far more often than it changes: product pages, profiles, settings, feeds.
Ways to read and write through a cache
| Strategy | How it works | Good for |
|---|---|---|
| Cache-aside | App checks the cache; on a miss it reads the database and stores the result | Most apps; the default choice |
| Read-through | The cache itself loads missing data from the database | When a library or service handles loading for you |
| Write-through | Every write goes to the cache and the database together | Data read right after it is written |
| Write-back | Writes go to the cache first, the database later | Very write-heavy data that can tolerate some risk |
If you are unsure, start with cache-aside: the app stays in control, a cache outage only makes things slower, and it is easy to reason about.
Keeping data fresh: TTL and invalidation
The hard part of caching is stale data. Two tools work together:
- Invalidation: when data changes, delete (or update) its cached copy, so the next read fetches the new value.
- TTL (time to live): every item expires after a set time anyway, so anything you forget to invalidate fixes itself.
Short TTLs give fresher data but more misses and more database load. Long TTLs are efficient but rely on invalidation being right. A common setup is invalidating on every write plus a TTL of minutes to hours as a safety net.
Tip: Never cache anything you would be in trouble for showing wrong for a moment, without invalidation: prices at checkout, account balances, permissions.
The cache stampede
When a very popular item expires, thousands of requests can miss at the same moment and all query the database for the same thing. This cache stampede (or thundering herd) can knock a database over for a few seconds every time a hot key expires.
- One rebuild at a time: the first request takes a short lock and refreshes the item; the others wait briefly or get the old copy.
- Refresh early: refresh popular items shortly before they expire.
- Add jitter: give TTLs a little randomness so many items don’t expire together.
Choosing what to cache
- Find the slowest or most repeated reads (database slow-query logs, APM traces).
- Cache whole answers the app uses, not tiny pieces, so one read serves a page.
- Pick a TTL from how stale the data may be, and invalidate on writes.
- Watch the hit rate: if it is low, the data is too varied or the TTL too short.
See it happen
In the interactive lesson a shop’s database drowns during a sale. You add a cache, then deal with a wrong price and a stampede, picking each fix and watching it play out on a live diagram.
Frequently asked questions
What is the best caching strategy?
For most apps, cache-aside with a TTL and invalidation on writes. Use write-through when data must be cached right after it is written, and write-back only when you can accept some risk of loss.
What is a cache hit rate?
The share of reads answered by the cache. 80 to 95% is common for good cache candidates; a low rate means the data is too varied or expires too soon.
Redis or Memcached?
Both are fast in-memory caches. Redis adds data types, persistence options and replication, so it is the usual choice; Memcached is a simpler pure cache.
What is cache invalidation?
Removing or updating a cached item when the underlying data changes, so readers don’t get stale data. It is hard because every write path must do it.