Skip to the solution
MediumSystem Design6 min read

How does caching work end-to-end (where, what, when) — cache-aside vs write-through?

331 views

The problem

End-to-end caching: layers, what to cache, when to evict, and write strategies with TTL and stampede guards.

  • caching
  • cache-aside
  • write-through
  • cdn
  • redis
  • eviction

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read · 8 sections

Read the code

Cache-aside vs write-through: behavior after a write
class 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 write

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 42 of 106 in System design

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What is the difference between throttling and rate limiting?

Continue