Loading…
Loading…
Charting your lesson…
Opening your lesson…
Caching Strategies
Lesson 3 of 4
6 steps•about 12 min•11 flashcards
Step 1/ 61 min
Caching is the highest-leverage performance lever in system design: storing frequently-accessed data in fast memory can cut latency by orders of magnitude and take huge load off your database. Almost every design answer improves when you add a cache — but only if you can also discuss invalidation and eviction, because a cache that serves stale data is worse than no cache.
What this covers / why it matters: where caching sits, the main patterns, eviction policies, and the hard part (invalidation). It's one of the most reliably-asked and high-impact system-design topics.
Step 2/ 62 min
A cache is a fast store (usually in-memory, like Redis or Memcached) holding copies of data that's expensive to fetch or compute, so repeated reads are near-instant. Caches live at many layers: browser, CDN (for static assets near the user), application-level (Redis in front of the DB), and inside the database itself. The classic win is putting a Redis cache between your app and a slow database for hot read queries.
A cache has limited memory, so it needs an eviction policy — usually LRU (evict the least-recently-used entry), sometimes LFU or TTL-based expiry. The genuinely hard part is invalidation: when the underlying data changes, stale cache entries must be removed or updated, or users see old data. Strategies include a TTL (entries expire after N seconds — simple, slightly stale) and explicit invalidation (delete/update the cache entry on write — fresh, but you must not miss any write path). Say the famous line and mean it: cache invalidation is one of the genuinely hard problems.
Step 3/ 61 min
"Reads on your product page are hammering the database. How do you speed it up?"
"I'd add a Redis cache in front of the database using the cache-aside pattern. On a product-page request, the app first checks Redis for that product; on a hit it returns instantly, on a miss it reads the database, stores the result in Redis with a TTL, and returns it. That offloads the vast majority of reads from the DB, since product data is read far more than it's written. For freshness I'd combine two things: a modest TTL (say 5 minutes) as a safety net, plus explicit invalidation — whenever a product is updated, the write path also deletes that product's cache key, so the next read repopulates fresh data. For memory, Redis would use LRU eviction so the hottest products stay cached. The trade-off is a small window of staleness bounded by the TTL, which is fine for product listings."
Pattern, freshness via TTL + explicit invalidation, eviction, and the stated trade-off — a complete caching answer.
Step 4/ 61 min
Step 5/ 61 min
Adding a cache but never addressing invalidation. A cache that serves stale data after the source changes causes bugs that are worse and harder to trace than a slow-but-correct system. Always pair a cache with a freshness strategy (TTL and/or explicit invalidation on write).
Step 6/ 6· Worked examples1 min
Problem/Question: "Reads on your product-details page are hammering the database. How would you use caching, and what are the pitfalls?"
Solution/Model answer:
Approach: put a cache (e.g., Redis) in front of the database for hot, read-heavy, relatively static data like product details.
Caching patterns:
Recall
Keep this
Caching stores frequently accessed data in fast memory to cut latency and offload the database. It is the single highest-leverage performance tool in system design — and interviewers expect you to know where to cache, which strategy, and how to handle staleness.
| Layer | Example |
|---|---|
| Client / browser | Cache static assets, API responses |
| CDN | Cache static content near users |
| Application | In-memory (local) cache |
| Distributed | Redis / Memcached shared across servers |
Write strategies: write-through (write cache+DB together, consistent, slower), write-back (write cache, async to DB, fast, risk of loss), cache-aside (app loads on miss — most common).
Eviction: LRU is the default; also LFU, TTL-based expiry.
Expected pattern: Comes up whenever reads dominate; interviewers probe write strategy and how you handle stale data.
Quick recap: Cache hot data in Redis/CDN, default to cache-aside + LRU, and always have a TTL/invalidation plan for staleness.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.