System designInterview preparation
Advanced·Concept·2 min read

How does cache-aside work?

Practice this question

Definition:

Cache-aside means the application manages when values enter and leave the cache. It first looks in the cache, loads missing data from the source of truth, then stores that result for later readers. This reduces repeated database work but creates a separate copy whose freshness must be controlled.

Key points:

  • Read the cache first; on a miss, read the database and cache the result.

  • Expire or invalidate entries when data changes.

  • Use tenant-aware keys and avoid many requests rebuilding the same expired entry at once.

  • Use a TTL as a bound on staleness and consider single-flight loading or expiry jitter to reduce a cache stampede.

Cache-aside read flow:

  1. Build a key that identifies the resource and any user/tenant context.
  2. Read the cache; return the value if it is usable.
  3. On a miss, load the authoritative value from the database.
  4. Store it with a deliberate TTL, then return it.
  5. On writes, invalidate or update affected keys and account for concurrent readers.

Interview point:

  • Explain the write path and invalidation race, not only the successful cache-hit path.
Adapted from 07_SYSTEM_DESIGN_ARCHITECTURE.md · “How does cache-aside work?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.