100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is the Cache-Aside (Lazy-Loading) Caching Pattern?

Learn how the cache-aside (lazy-loading) caching pattern works, its read/write flow, and trade-offs for database performance.

mediumQ139 of 228 in Database Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

Cache-aside is a caching pattern where the application code itself checks the cache first, and on a miss reads from the database, populates the cache with that result, then returns it โ€” the cache is never automatically synchronized by the database.

On a read, the application queries the cache; if the key exists (a hit) it returns immediately without touching the database. On a miss, the application reads the database, writes the fetched value into the cache with a TTL, and returns it to the caller, so subsequent reads for that key become hits. Writes typically go straight to the database and either invalidate or update the corresponding cache key, since the cache never owns the source of truth. This keeps the cache simple and resilient โ€” a cache outage just means every request falls back to the database โ€” but it means the first request after a miss always pays full database latency, and the application owns all the population logic.

  • Only requested data ever gets cached, avoiding wasted memory
  • Cache failures degrade gracefully to direct database reads
  • Simple to reason about โ€” application fully controls population
  • Works with any cache and any database with no special coupling

AI Mentor Explanation

A commentator keeps a personal notebook of player statistics next to the broadcast desk. When asked about a batter mid-over, they first flip to the notebook page; if the stat is already jotted down, they read it instantly. If it is missing, they radio the stats room, get the number, write it into the notebook for next time, and only then read it out. The notebook is the cache-aside cache: the commentator's own lookup logic decides when to consult it and when to fetch fresh and record the result.

Step-by-Step Explanation

  1. Step 1

    Check the cache first

    On a read request, the application looks up the key in the cache before touching the database.

  2. Step 2

    Return on a hit

    If the key is present, the cached value is returned immediately with no database call.

  3. Step 3

    Read-through on a miss

    On a miss, the application reads the value from the database.

  4. Step 4

    Populate and return

    The application writes the fetched value into the cache with a TTL, then returns it to the caller.

What Interviewer Expects

  • Clear description of the check-cache, then read-database, then populate flow
  • Understanding that the application owns population logic, not the database
  • Awareness of the cold-start / first-request latency cost on a miss
  • Mention of write handling โ€” invalidate or update the cache alongside the database write

Common Mistakes

  • Assuming the cache is automatically kept in sync by the database
  • Forgetting to set a TTL, leading to stale data lingering indefinitely
  • Not handling the thundering-herd problem when a popular key expires
  • Confusing cache-aside with write-through, which populates the cache on every write instead

Best Answer (HR Friendly)

โ€œCache-aside means the application checks the cache first, and only if the data is not there does it go to the database, then saves that result in the cache for next time. It is simple and resilient because if the cache goes down, requests just fall back to the database, but the very first request for any piece of data is always a bit slower.โ€

Code Example

Application-level cache-aside pseudocode with the underlying SQL
-- Application logic (pseudocode):
-- 1. value = cache.get("user:101")
-- 2. if value exists: return value
-- 3. else: query database, store in cache, return value

-- Step 2 fallback: the actual database query on a cache miss
SELECT user_id, name, email
FROM Users
WHERE user_id = 101;

-- Application then runs:
-- cache.set("user:101", result, ttl=300)

Follow-up Questions

  • How does cache-aside differ from read-through caching?
  • What is the thundering-herd problem and how do you mitigate it?
  • How would you invalidate a cache-aside entry after an UPDATE statement?
  • What TTL would you choose for rarely-changing versus frequently-changing data?

MCQ Practice

1. In the cache-aside pattern, who is responsible for populating the cache on a miss?

Cache-aside places population logic in the application: it reads from the database on a miss and writes the result into the cache.

2. What happens to a cache-aside cache if the cache server goes down?

Because the application always checks the cache and falls back to the database, a cache outage just increases latency rather than causing failures.

3. What is a known drawback of cache-aside on a cold cache?

Since entries are only populated after a miss, the first read for any given key must wait on the full database round trip.

Flash Cards

What is cache-aside? โ€” A pattern where the application checks the cache first, and on a miss reads the database and populates the cache itself.

Who populates a cache-aside cache? โ€” The application code, not the database or cache server automatically.

What happens on a cache-aside cache outage? โ€” Requests gracefully fall back to reading directly from the database.

Main drawback of cache-aside? โ€” The first request after a miss always pays full database read latency.

1 / 4

Continue Learning