CACHE-ASIDE

The app owns the caching logic — and the staleness risk

L215 MINCACHING
PREDICT

A misses and reads “Alice” from the DB. B updates the row to “Bob” and deletes the cache key. Then A sets the cache. There is no TTL. What does the cache hold, and for how long?

In cache-aside the application owns the cache. A read checks the cache; on a miss it reads the DB and sets the value. A write updates the DB and deletes the key.

The read is two steps, read the DB then set the cache, and a writer can run between them. The reader holds the old row, the writer updates and deletes, and then the reader sets the old row. The delete already happened. Nothing will remove that entry. A TTL bounds the damage to one TTL. A lease closes it: the miss gets a token, the delete revokes it, and the late set is refused.

▶ SIMULATION
READER A
tmp_A = —
lease: —
pc: get
CACHE
user:1 = —
expires in: —
live lease: none
DATABASE + WRITER B
users[1] = "Alice" v1
last acknowledged write: v1
pc: write
clockt = 0 ms · mode: plain
reader C saw—
stale reads / reads0 / 0
last event—
✓ cache never holds a value older than the last acknowledged write✓ every read after a write is acknowledged returns it
SCHEDULE
01[A]config: no TTL, DEL on write
02[A]A: GET user:1 → MISS
03[A]A: SELECT name FROM users → tmp
04[B]B: UPDATE users SET name = "Bob"
05[B]B: DEL cache user:1 (write acknowledged)
06[A]A: SET cache user:1 ← tmp
07[C]C: GET user:1 @ 100 ms
08[C]C: GET user:1 @ 6000 ms
09[C]report
Start of stale fill scenario. 9 steps.
function read(key) {
  const hit = cache.get(key);
  if (hit) return hit.value;
  const row = db.get(key);        // may be the old row...
  cache.set(key, row);            // ...written after the writer's DEL
  return row.value;
}

function write(key, value) {
  db.put(key, value);
  cache.del(key);                 // runs once; a late SET undoes it
}

Watch the invariant strip. STALE FILL turns the entry invariant red at A’s SET and the read invariant red at C’s first read. Both stay red. SHORT TTL heals the cache at 5050 ms but the stale read is already counted. FIX: LEASED FILL runs the same interleaving and the SET bounces off the revoked lease.

BREAK IT NOT YET

No TTL, delete-on-write. Run every step of A and B in an order that leaves the cache holding the old name after the write is acknowledged.

GOAL: both finish and the cache holds v1 while the DB has v2

FIX IT NOT YET

Make the read path safe against a write that lands between your DB read and your cache fill. The grader pauses readThrough right after db.get, runs a full write, then resumes you. The cache must still match the DB afterwards, and a quiet read must still populate the cache.

Mental model:

a cache fill is a write based on an old read. Any writer can run between the read and the fill, so the fill must be conditional: on a lease the writer can revoke, or on a version that refuses to go backwards. A TTL does not fix the race. It only bounds how long you are wrong.

IN PRODUCTION

Redis or memcached in front of Postgres or MySQL, with a get → miss → select → set read path and update → del on writes. Facebook's “Scaling Memcache at Facebook” paper describes leases for exactly this stale set. Symptom: one record stays wrong until it is edited again.

IN INTERVIEWS

“Walk me through cache-aside. Where can it serve stale data?” Describe the read and write paths, then the stale-fill interleaving, then say why delete beats set on write, what a TTL does and does not bound, and how leases or versioned fills close the window.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: TTL, JITTER & NEGATIVE CACHING →