End-to-end caching: layers, what to cache, when to evict, and write strategies with TTL and stampede guards.
Skip to solutionKEEP THE
mediumSystem Design
How does caching work end-to-end (where, what, when) — cache-aside vs write-through?
331 views
01
Understand the problem
cachingcache-asidewrite-throughcdnrediseviction
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
How Does Caching Work End-to-End? Where, What, and When (Cache-Aside vs. Write-Through)
Target Audience: Junior & Senior Software Engineers preparing for System Design Interviews — assumes basic familiarity with the idea of a cache. Difficulty: Medium
This doc builds on the <a href="https://www.interviewp
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
Cache-aside vs write-through: behavior after a write
Run Playgroundclass Store {
constructor() {
this.db = {};
this.cache = {};
}
}
// Cache-aside: write goes straight to the DB; cache entry is invalidated, not refreshed.
function cacheAsideWrite(store, key, value) {
store.db[key] = value;
delete store.cache[key]; // invalidate -- do NOT repopulate here
}
function cacheAsideRead(store, key) {
if (key in store.cache) {
return ['HIT', store.cache[key]];
}
const value = store.db[key];
store.cache[key] = value; // populate on miss
return ['MISS', value];
}
// Write-through: write updates the cache and the DB together, synchronously.
function writeThroughWrite(store, key, value) {
store.db[key] = value;
store.cache[key] = value; // cache updated in the same step as the write
}
function writeThroughRead(store, key) {
if (key in store.cache) {
return ['HIT', store.cache[key]];
}
const value = store.db[key];
store.cache[key] = value;
return ['MISS', value];
}
function printResult(result) {
console.log(`('${result[0]}', '${result[1]}')`);
}
const s1 = new Store();
cacheAsideWrite(s1, 'x', 'v1');
printResult(cacheAsideRead(s1, 'x')); // MISS -- invalidated, not refreshed
printResult(cacheAsideRead(s1, 'x')); // HIT -- warmed by the previous read
const s2 = new Store();
writeThroughWrite(s2, 'x', 'v1');
printResult(writeThroughRead(s2, 'x')); // HIT -- already warm from the write05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 34 of 99 decoded in the System Design track. One more won't hurt.