CACHE-ASIDE
The app owns the caching logic — and the staleness risk
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.
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
}// Lease (memcached-style): a miss hands out a token; DEL revokes it.
function read(key) {
const r = cache.getOrLease(key); // { value } | { lease }
if (!r.lease) return r.value;
const row = db.get(key);
cache.setIfLease(key, row, r.lease); // no-op if a DEL ran since the miss
return row.value;
}
function write(key, value) {
db.put(key, value);
cache.del(key); // also revokes outstanding leases
}
// Alternative: versioned fill. The writer sets { value, version } with
// setIfNewer, and a reader's late fill of an older version is ignored. 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- Caching in front of the databaseDatabases