Loading…
Loading…
Charting your lesson…
Opening your lesson…
Caching
Lesson 2 of 6
Storing frequently used data in fast storage to cut latency and load — cache strategies, eviction, and invalidation.
6 steps•about 11 min•8 flashcards
Step 1/ 61 min
"How would you make this read 100x faster?" In almost every design interview the winning first move is caching. It is the highest-leverage lever you have — and interviewers want to see that you also understand its costs.
Step 2/ 62 min
A cache is a small, fast store that keeps copies of frequently accessed data close to where it is needed, so you avoid a slow, expensive operation (a database query, an API call, a computation). Caches trade memory and freshness for speed. A request that finds its data in the cache is a cache hit; one that doesn't is a cache miss and must fall back to the source.
The closer the cache is to the user, the bigger the latency win.
How the cache stays in sync with the database defines the pattern:
A cache has limited size, so it needs an eviction policy: LRU (evict least-recently-used) is the default; others are LFU (least frequently used) and FIFO. Entries also carry a TTL (time to live) after which they expire. The hardest problem is invalidation — keeping cached data from going stale. Watch for a cache stampede (many simultaneous misses hammering the DB when a hot key expires); mitigate with staggered TTLs, locking, or refresh-ahead.
Step 3/ 61 min
A product page runs a heavy join taking 200 ms and is read 50,000 times/min. With cache-aside in Redis and a 60-second TTL, the first request is a miss (200 ms) and the rest are hits (~1 ms), cutting database load by well over 99% and dropping typical latency from 200 ms to a couple of milliseconds.
Step 4/ 61 min
Cache read-heavy data that is expensive to produce and tolerant of slight staleness — product catalogs, user profiles, computed feeds. Avoid caching data that must always be perfectly fresh (e.g., a bank balance at the moment of a transaction) unless you invalidate carefully.
Step 5/ 61 min
Remember the trade with "caches make reads fast, but freshness and memory pay the bill." For the write patterns: aside = lazy, through = safe, back = fast-but-risky.
Don't cache without an invalidation or TTL plan. A cache with no expiry and no invalidation silently serves stale data — one of the most common production bugs.
Step 6/ 6· Worked examples1 min
Problem
A product page issues a database query that takes ~200 ms. It is called 10,000 times/minute, and 90% of calls hit the same 100 popular products. Design a cache and estimate the database-load reduction.
Try it on paper first.
Recall
Keep this
Fast revision of caching — the highest-leverage performance lever.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.