Course contentsShow
← System Design
Lesson 131 of 1,9194. Caching StrategiesFree lesson

Cache-Aside (Lazy Loading) Pattern

Application loads data from cache if present, else fetches from DB, stores in cache, and returns it.

Cache-Aside (Lazy Loading) Pattern

What You'll Learn

You'll learn how the cache-aside pattern lets your application intelligently decide when to use cache versus hitting the database directly.

The Core Idea

In the cache-aside pattern (also called lazy loading), your application code takes full responsibility for managing the cache. Here's the flow:

  1. Check the cache first: When your app needs data, it looks in the cache
  2. Cache hit: If found, return the data immediately
  3. Cache miss: If not found, fetch from the database
  4. Populate cache: Store the fetched data in cache for next time
  5. Return the data: Give it back to the user

Think of it like your desk drawer. When you need a pen, you check your drawer first (cache). If it's there, great! If not, you walk to the supply closet (database), grab a pen, put one in your drawer for later (populate cache), and use it. Next time, it's right in your drawer.

Why "Lazy" Loading?

It's called "lazy" because the cache is only populated on-demand—when data is actually requested. The cache doesn't preemptively load everything; it loads data "lazily" as needed.

The Key Characteristics

  • Application controls everything: Your code explicitly manages cache reads and writes
  • Cache is optional: The database is the source of truth; cache failures don't break your system
  • Popular data naturally cached: Frequently requested data stays in cache, while rarely-used data doesn't waste cache space

Key Takeaway: Cache-aside makes your application responsible for checking cache first, fetching from the database on misses, and populating the cache—ensuring only requested data gets cached while keeping the database as the authoritative source.