Courses 0%
38
Caching Patterns · Chapter 38 of 42

Cache-Aside (Lazy Loading)

Akhil
Akhil Sharma
15 min

Cache-Aside (Lazy Loading)

The most common caching pattern — your application checks the cache first, falls back to the database on a miss, and populates the cache for next time.

Cache-Aside (Lazy Loading): The Art of Fetching Only What You Need 🎯 Challenge 1: The Overwhelming Library Problem

Imagine this scenario: You're building an e-commerce website with 10 million products. Your database can handle 1,000 queries per second, but during Black Friday, you're getting 100,000 requests per second. Your database is drowning!

Pause and think: How would you handle 100x more traffic without buying 100x more database servers?

The Answer: Cache-Aside (Lazy Loading) acts as your smart assistant that remembers frequently accessed items. Instead of hitting the database every time, you keep popular items in fast memory (cache) and only fetch from the database when absolutely necessary.

Key Insight: Cache-Aside loads data into cache only when it's actually requested - hence "lazy" loading!

🏪 Interactive Exercise: The Grocery Store Shopper

Scenario: You go grocery shopping every week. Your kitchen pantry is small (like a cache), while the grocery store is huge (like a database). What do you do?

Think about the steps:

  1. Check your pantry first (check cache)
  2. If you have the item, use it! (cache hit)
  3. If you don't have it, go to the store (cache miss)
  4. Buy it and bring it home to your pantry (load into cache)
  5. Next time you need it, it's already in your pantry!

Question: Why don't you keep everything from the grocery store in your tiny pantry?

Cache-Aside Flow: The Digital Version

Application does ALL the work - it manages both cache and database:

Real-world parallel: Like keeping your passport in your desk drawer instead of a safe deposit box. The first time after a trip, you put it in the drawer. Next time you need it, it's right there - no trip to the bank needed!

Key terms decoded:

  • Cache Hit = Found in cache (fast!)
  • Cache Miss = Not in cache (slower, need database)
  • Lazy Loading = Only load what's actually requested

🚨 Common Misconception: "I Should Pre-Load Everything... Right?"

You might think: "Why not just load all 10 million products into cache at startup? Then everything will be fast!"

The Reality Check:

  • Your cache memory: 64 GB
  • All products in database: 5 TB
  • Math doesn't work! 🤯

The Cache-Aside wisdom: Only cache what people actually use!

Mental model: The 80/20 rule! Usually 20% of your data gets 80% of the requests. Cache-Aside naturally caches only that hot 20%, saving memory and money!

Challenge question: What percentage of requests become cache hits in a well-tuned Cache-Aside system? (Hint: Think about popular items getting requested repeatedly)

🎮 Decision Game: Database is Down!

Context: Your application uses Cache-Aside pattern. Suddenly, your database crashes completely.

What happens to new requests? A. Everything fails immediately B. Cached data still works, new data fails C. Application automatically switches to backup database D. Cache handles everything automatically

Think about it... Who's responsible for what in Cache-Aside?

Answer: B - Cached data still works, new data fails!

Here's why:

Real-world parallel: Your pantry still has cereal even if the grocery store burns down. But you can't get milk if you're out and the store is closed!

Key insight: In Cache-Aside, the application is responsible for handling failures. This gives you control but requires more code!

🚰 Problem-Solving Exercise: The Stale Data Problem

Scenario: You're building a social media app. At 9:00 AM, User #42 updates their profile picture. The new picture goes to the database. But the cache still has the old picture!

What do you think happens?

  1. Friend sees old picture until cache expires?
  2. Database automatically updates cache?
  3. Application needs to invalidate cache?

Solution: Cache Invalidation!

The application must explicitly remove or update the cached data:

sql

Real-world parallel: When you update your address, you don't just update it with the DMV. You also need to update your phone's saved addresses, tell your favorite restaurants, update your Amazon account, etc. Nobody does this automatically for you!

Famous quote: "There are only two hard things in Computer Science: cache invalidation and naming things." - Phil Karlton

The Cache-Aside challenge: YOU are responsible for keeping cache and database in sync. The pattern doesn't do it automatically!

🔍 Investigation: The Cold Start Problem

Imagine your application just restarted:

  • Cache: Empty (like a cleared pantry)
  • Database: Full (like a stocked grocery store)
  • Users: Expecting fast responses!

What happens during the first few minutes?

This is called Cache Warming:

Mental model: Like a cold car engine. The first few minutes are rough, but once it warms up, it purrs! Cache-Aside naturally warms up with real traffic.

Pro tip: Some systems pre-warm the cache by running popular queries at startup, but Cache-Aside doesn't require this!

🧩 Implementation Challenge: The Code Pattern

Scenario: You're writing the code for Cache-Aside. Here's the pseudocode pattern you'll use thousands of times:

sql

Question: Why does the application handle all this logic instead of the cache doing it automatically?

Answer: Flexibility and control!

Because YOU write the logic, you can:

  • Decide what to cache (maybe skip caching large items)
  • Choose cache TTL (time-to-live) per item type
  • Handle errors your way
  • Implement custom cache keys
  • Add monitoring and metrics
  • Implement sophisticated eviction policies

Real-world parallel: Like choosing to DIY your home organization vs. hiring an organizer. DIY = more work but total control. Cache-Aside = DIY caching!

👋 Interactive Journey: The Update Problem

Scenario: User updates their shopping cart. You need to update the database AND the cache. What's the correct order?

Option A: Update cache first, then database Option B: Update database first, then cache Option C: Update both simultaneously Option D: Update database, then delete from cache

Think about what could go wrong with each option...

The Analysis:

Option A - Update cache, then database:

Option B - Update database, then cache:

Option C - Update both simultaneously:

Option D - Update database, then delete from cache:

sql

The Winner: Option D (Update DB, then invalidate cache)!

This is the most common Cache-Aside update pattern:

javascript

Mental model: When you change your phone number, you update it with your carrier (database), then tell your friends to "forget my old number" (invalidate cache). They'll ask you for the new one next time!

🎪 The Great Comparison: Cache-Aside vs Your Daily Life

Let's solidify your understanding. Match Cache-Aside behaviors to real-world scenarios:

Cache-Aside Behavior → Real-World Equivalent

Check cache first → ? Cache miss → ? Load from database → ? Store in cache → ? Cache invalidation → ? Lazy loading → ?

Think about each one...

Answers revealed:

javascript

The big picture: Cache-Aside is like your brain's working memory - you only remember what you use frequently, forget what you don't use, and look things up when needed!

💡 Final Synthesis Challenge: When Should You Use Cache-Aside?

Complete this decision tree:

"I should use Cache-Aside when..."

Your answer should consider:

  • Read/write patterns
  • Data consistency requirements
  • Control needs
  • Complexity tolerance

Take a moment to formulate your complete answer...

The Complete Picture:

Use Cache-Aside when:

✅ Read-heavy workload (reads >> writes)

  • Social media profiles, product catalogs, user preferences

✅ You need fine-grained control

  • Custom cache keys, selective caching, per-item TTLs

✅ You can tolerate some inconsistency

  • Eventual consistency is acceptable
    • Brief stale data is okay

✅ Your application is the orchestrator

  • You want to handle cache logic explicitly
    • You need custom error handling

✅ Unpredictable access patterns

  • Don't know what will be popular
    • Can't pre-load everything

✅ Cost-conscious scaling

  • Cache only what's actually used
    • Avoid cache memory waste

Avoid Cache-Aside when:

❌ Write-heavy workload

  • Constant updates invalidate cache frequently
    • Cache hit rate becomes too low

❌ Strict consistency required

  • Financial transactions, inventory counts
    • Can't tolerate ANY stale data

❌ You want simplicity

  • Don't want to manage cache logic yourself
    • Prefer automatic cache management

Real-world success stories:

  • Netflix: Caches movie metadata, user preferences
  • Facebook: Caches user profiles, friend lists
  • Amazon: Caches product information, recommendations
  • Reddit: Caches popular posts, user karma

🎯 Quick Recap: Test Your Understanding

Without looking back, can you explain:

  1. Why is it called "lazy" loading?
  2. Who is responsible for loading data into cache - the cache or the application?
  3. What happens during a cache miss?
  4. Why is cache invalidation challenging?
  5. What's the safest way to handle updates?

Mental check: If you can answer these clearly, you've mastered Cache-Aside! If not, revisit the relevant sections above.

📊 The Cache-Aside Cheat Sheet

yaml

🚀 Your Next Learning Adventure

Now that you understand Cache-Aside, you're ready to explore:

Immediate comparisons:

  • Write-Through Cache: What if writes go through cache first?
  • Write-Behind Cache: What if writes are asynchronous?
  • Read-Through Cache: What if cache loads data automatically?

Compare and contrast:

  • Cache-Aside vs Read-Through: Who owns the loading logic?
  • Cache-Aside vs Write-Through: How are writes handled?
  • When to use which pattern?

Advanced Cache-Aside topics:

  • Cache stampede problem (thundering herd)
  • Probabilistic early expiration
  • Cache warming strategies
  • Distributed caching with Cache-Aside (Redis, Memcached)
  • Monitoring cache hit rates and optimizing TTLs

Real-world implementations:

  • Redis with Cache-Aside in Python/Java/Node.js
  • Memcached implementation patterns
  • CDN as a Cache-Aside layer
  • Multi-level caching strategies

Remember: Cache-Aside is the most common caching pattern because it's flexible, resilient, and relatively simple. Master it, and you'll understand the foundation for all other caching strategies! 🎉


Key Takeaways

  1. Cache-aside (lazy loading) loads data into cache only on cache miss — the application manages both cache and database
  2. This is the most common caching pattern — simple to implement and works well for read-heavy workloads
  3. Cache misses result in three round trips — check cache, read from database, write to cache
  4. Stale data is the main risk — use TTL or explicit invalidation when the underlying data changes
Chapter complete!

Course Complete!

You've finished all 42 chapters of

System Design Indermediate

Browse courses
Up next Write Through Cache
Continue