Course map

Topic 083 / Core

Cache-aside and read-through

Chapter 6.1 · Caching Strategies

What and why

In most systems the same records are read far more often than they change — a product page is viewed thousands of times between price updates — yet every read costs a full database round trip. Cache-aside fixes this in application code: check a fast in-memory store first, and only on a miss read the database and fill the cache. Read-through is the same idea with the loading moved into the cache layer itself: the application only ever talks to the cache, and the cache fetches from the database on a miss. The difference is who owns the loading logic — your code (aside) or the cache (through). Builds on: → 82 (cache hierarchy, hit ratio).

Diagram

Cache-aside read path The application asks the cache for a key. On a hit the value returns and the read is done. On a miss the application queries the database, then sets the key in the cache with a TTL. App Cache Database 1 get(key) hit → value — done miss 2 query value 3 set(key, value, TTL)
Write path: update the database first, then delete cache[key] — the next read misses and refills from the database. Read-through: the same flow, but the miss → query → set steps happen inside the cache layer, not in application code.
Text version of this diagram
READ path (cache-aside)                     WRITE path
───────────────────────                     ──────────
app ── get(key) ──► cache                   1. update the database
          │                                 2. delete cache[key]
     hit ─┤── value ──────► done               (next read misses and
          │                                     refills from the database)
    miss ─┘
app ── query ────► database
app ── set(key, value, TTL) ──► cache ──► done

Read-through: same flow, but the "miss → query → set" steps
happen inside the cache layer, not in application code.

How it works

  1. On read, the application asks the cache for the key.
  2. Hit: return the cached value — the database is never touched.
  3. Miss: read the database, write the result into the cache with a TTL (→ 85), return it.
  4. On write, update the database first, then delete the cache entry; the next read misses and refills from the database.
  5. The order in step 4 matters: deleting the cache before the database write leaves a window where a reader refills the cache with the old value — stale data that survives until the TTL expires.
  6. Even with correct ordering a smaller race remains: a reader that fetched the old value from the database just before the write can set the cache after the delete. Cache-aside never guarantees freshness — the TTL (→ 85) is what bounds how long such a stale value can live.

Practical example

(numbers illustrative). A product-detail endpoint reads one row: database read ≈ 25 ms, cache read ≈ 1 ms. At a 90% hit ratio (→ 82), the mean read becomes 0.9 × 1 ms + 0.1 × 26 ms ≈ 3.5 ms — about 7× faster — and the database sees only 10% of the read traffic, freeing it for writes. The same arithmetic shows why hit ratio is the number to watch: at 50% hits the mean is ≈ 13.5 ms and the database still carries half the load.

When to use it

  • Use when: read-heavy data that changes rarely, a bounded staleness window is acceptable, and measurement shows the database read is actually the bottleneck.
  • Avoid when: writes are frequent (constant invalidation erases the benefit), reads must always be fresh (→ 84 write-through), or the hit ratio would stay low — then the cache is pure overhead.
  • Failure scenarios: stale reads after a write (invalidation missing, mis-ordered, or lost to the refill race — steps 5–6); a popular key expiring and a thundering herd hitting the database (→ 88); the cache going down — the system must degrade to database-only reads, slower but correct, so never store data only in the cache.
  • Simpler alternative: none at all. Fix the query and its index first; the database's own buffer pool (→ 82) already caches hot pages. Add cache-aside only for reads that are measured hot after that.

Practice

(pattern kind — implement minimal, spot the misuse; 15–30 min, shared lab: Postgres + Redis).

  1. Implement get_product(id) with cache-aside and a 60 s TTL; verify a hit skips the database (watch the query log).
  2. Measure p99 latency and hit ratio over 100 reads of 10 products with a skewed access pattern, with the cache off and then on.
  3. Spot the misuse: given a handler that deletes the cache key before updating the database, produce the stale read it allows, then fix the ordering and show it's gone.

Done when p99 drops with the cache on and the stale read is first reproduced, then eliminated.

Shared lab setup

Key takeaway

Cache-aside is the default caching pattern: application-managed, database as source of truth, cache as disposable copy — update the database first, then invalidate. Done when you can: implement it with correct write ordering, state its staleness window, and say what happens when the cache is empty, stale, or down. Used next in → 84 (write strategies), → 85 (invalidation), → 87 (Redis); Projects 2 and 5.

References

Microsoft, "Cache-Aside Pattern" (Azure Architecture Center) — see references.md.

Source directory

Search the guide