Archprimer
PatternBeginner 3 min read

Cache-Aside

The app checks the cache first and loads from the database only on a miss — the default caching strategy.

CachingRead-HeavyLazy Loading

By Ratish Jain · Updated

Diagram with 3 components: Application, Cache, Database. Connections: Application → Cache (1 · read); Application → Database (2 · on miss); Database → Cache (3 · backfill).
ServiceCacheDatabase
On a miss, the app loads from the DB and backfills the cache.

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

  1. 1App looks up the key in the cache.
  2. 2Hit → return the cached value.
  3. 3Miss → read from the database.
  4. 4Write the value into the cache (with a TTL) for next time.
  5. 5Return the value to the caller.

Consistency on writes

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

Pros
  • Only caches data that's actually used.
  • Cache failures degrade gracefully — reads fall back to the DB.
  • Simple to reason about and implement.
Cons
  • 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 vs write-through?

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.

How do you prevent a thundering herd on a hot-key miss?

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.

60-Second Revision
  • →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.