Cache-Aside
The app checks the cache first and loads from the database only on a miss — the default caching strategy.
By Ratish Jain · Updated
Definition
Cache-aside (a.k.a. lazy loading) puts the application in charge of the cache. The cache doesn't know about the database — the app reads from the cache, and on a miss, reads the database and writes the result back into the cache.
The Problem It Solves
You want most reads served from a fast cache without caching data nobody asks for. Cache-aside only ever caches data that has actually been requested, keeping the working set small and hot.
How It Works
- 1App looks up the key in the cache.
- 2Hit → return the cached value.
- 3Miss → read from the database.
- 4Write the value into the cache (with a TTL) for next time.
- 5Return the value to the caller.
Consistency on writes
On an update, invalidate (delete) the cache key rather than trying to update it. Deleting is safer — the next read repopulates from the source of truth and avoids subtle race conditions.
Tradeoffs
- Only caches data that's actually used.
- Cache failures degrade gracefully — reads fall back to the DB.
- Simple to reason about and implement.
- Every cache miss pays cache + DB round trips.
- First request for any key is always slow (cold start).
- Risk of stale data within the TTL window.
Interview Questions
Cache-aside loads lazily on read misses; write-through writes to the cache and DB together on every write, keeping the cache always warm at the cost of write latency. Cache-aside is the common default; write-through suits read-after-write heavy paths.
Use a lock or 'single-flight' so only one request rebuilds the key while others wait, or serve a slightly stale value while refreshing in the background.
- →App checks cache → DB on miss → backfill cache.
- →Only hot, requested data gets cached.
- →On write: delete the key, don't update it.
- →Cold starts are slow; TTL bounds staleness.
Related
Everything on Archprimer is connected — follow the graph.