Course contentsShow
← Cloud and Infrastructure
Lesson 2697 of 2,98958. Caching StrategiesFree lesson

Cache-Aside (Lazy Loading) Pattern

Load data into cache only when requested, checking cache first before querying the data source.

Cache-Aside (Lazy Loading) Pattern

What you'll learn: How to implement a caching strategy that loads data into cache on-demand, only when applications request it.

What Is Cache-Aside?

The Cache-Aside pattern (also called Lazy Loading) is a caching strategy where your application code is responsible for both reading from the cache and loading data into it. When the application needs data, it first checks the cache. If the data isn't there (a "cache miss"), the application retrieves it from the underlying data source, stores it in the cache for future requests, and returns it to the caller.

Think of it like keeping frequently-used tools on your workbench. You don't pre-load every tool from storage—you only bring out a wrench when you need it, then leave it on the bench for next time.

How It Works

  1. Check cache first: Application queries the cache with a key
  2. Cache hit: If data exists, return it immediately
  3. Cache miss: If data doesn't exist:
    • Query the database or primary data source
    • Write the retrieved data to cache
    • Return the data to the application

When to Use It

Cache-Aside works well for read-heavy workloads where:

  • Not all data needs to be cached
  • Data access patterns are unpredictable
  • You want fine-grained control over what gets cached
  • Cache misses are acceptable (first request always misses)

Trade-offs

Advantages: Memory-efficient (only caches requested data), simple to implement, works with any cache backend like Redis or Memcached.

Disadvantages: First access is always slow (cache miss), potential cache stampede on popular items, stale data if not invalidated properly.

Key Takeaway: Cache-Aside loads data lazily on-demand, giving you control and efficiency at the cost of first-request latency and requiring application-managed cache population logic.