Speed up reads with caching
Choose a caching pattern, an eviction policy, and a way to stay fresh.
- Describe cache-aside, write-through, and write-back caching.
- Explain LRU eviction and TTL expiry.
- Recognize invalidation problems such as stale data and stampedes.
A cache keeps a copy of data somewhere faster - in memory rather than on disk, or at the network edge rather than across an ocean. It trades freshness and memory for speed and reduced load on the source of truth. Caches appear at every layer: the browser, a CDN for static assets, an application cache such as Redis or Memcached, and the database’s own buffer pool.
Caching patterns
- Cache-aside (lazy loading): the app checks the cache; on a miss it reads the database and populates the cache. The most common pattern.
- Write-through: writes go to the cache and the database together. Reads are fresh, but writes are slower.
- Write-back (write-behind): writes go to the cache and are flushed to the database later. Fast, but data can be lost if the cache node fails first.
Caches are finite, so they evict. LRU (least recently used) drops the entry untouched for longest; LFU drops the least frequently used. A TTL expires entries after a set time, bounding how stale they can get.
The benefit depends on the hit ratio. With a 90% hit ratio, a 1 ms cache, and a 20 ms database, the average read takes 0.9 × 1 + 0.1 × 20 = 2.9 ms - and the database sees only a tenth of the reads. Invalidation is the hard part: when data changes you must update or delete the cached copy, or accept staleness up to the TTL.
Practice: tune a cache
Try it
Up to 15 XPKeep the hot key in the cache
Most requests are for home, as on a real site. Green is a hit, and red is a miss that reaches the database.
- With a capacity of 2, compare FIFO and LRU. What happens to
home? - Raise the capacity one step at a time. When does adding memory stop helping, and why are there still misses?
- homemiss
- amiss
- homehit
- bmiss
- homemiss
- cmiss
- homehit
- amiss
- homemiss
- dmiss
- homehit
- bmiss
- homemiss
- cmiss
- homehit
- homehit
In the cache at the end: home, c
- Hit ratio
- 31%
- Reads reaching the database
- 11 of 16
- Average read
- 14.1 ms
Try it
Up to 15 XPWhich caching pattern fits?
State the pattern and say why. That’s what interviewers listen for.
“User profiles that are read far more than they’re written.”
“A product price that must be fresh straight after an update.”
“A view counter with huge write volume, where losing a few is fine.”
“An article page where a few minutes of staleness is fine.”
Key takeaways
Cache-aside is the default; write-through favors freshness; write-back favors write speed at a durability risk.
LRU and TTL keep a cache bounded in size and staleness.
Always plan invalidation and protect hot keys from stampedes.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…