Notesfoundations🌱 seedling
Cache-Aside
Also: Look-Aside Cache · Lazy Loading · Cache Aside Pattern
The application owns the cache: on a read it checks the cache, falls back to the database on a miss and fills the cache. On a write it updates the database and deletes the cached key.
Why it exists
It works with any key-value store, caches only what's actually read, and keeps working (slowly) if the cache is down.
How it works
- Read:
get(k). On a miss, query the database,set(k, v, ttl), return. - Write: update the database, then
delete(k). The next read refills from the source of truth. - Delete, don't set: deletes are idempotent and order-insensitive. Two racing sets can land out of order and leave the old value.
- Always set a TTL, so missed invalidations heal themselves.
Trade-offs
| Gains | Costs |
|---|---|
| simple, generic, lazy | first read of every key misses |
| cache failure isn't fatal | two systems updated non-atomically leave stale windows |
| caches only hot data | every code path must invalidate correctly |
In practice
- Facebook memcache: cache-aside ("look-aside") with delete-on-write at billions of requests per second, plus leases to stop stale sets and herds.
Pitfalls
- The stale-set race: a slow reader fetches v1, a writer commits v2 and deletes the key, then the reader sets v1. Without a TTL it's stale forever. Leases fix it (see Cache Invalidation).
- Cold start: an empty cache sends every read to the database at once (see Cache Stampede).
Connections
- Part of
- Caching
- Contrasts with
- Write-Through Cache
- Related
- Cache Invalidation
- Mentioned
- Cache Stampede
Recall
These cards are also in your review deck, scheduled with spaced repetition.