Course contentsShow
← System Design
Lesson 1722 of 1,91941. Real-World System: Notification SystemFree lesson

Real-Time Preference Updates

Implement cache-aside pattern to serve preference checks quickly while keeping database as source of truth.

Real-Time Preference Updates

You'll learn: How to implement the cache-aside pattern so preference checks remain blazingly fast while maintaining database consistency when users change notification settings.

The Challenge

When a user updates their preferences (like muting emails or changing quiet hours), your notification system must respect those changes immediately. However, checking the database on every notification delivery would create a massive bottleneck. You need speed without sacrificing accuracy.

Cache-Aside Pattern for Preferences

The cache-aside pattern (also called lazy loading) works like this:

  1. On read: Check cache first. If miss, fetch from database and populate cache.
  2. On write: Update database immediately, then invalidate or update the cache entry.

The Flow

When processing a notification:

if preferences in cache:
    use cached preferences
else:
    fetch from database
    store in cache with TTL
    use preferences

When a user updates preferences:

write to database (source of truth)
invalidate cache entry for that user
OR update cache entry directly

Why This Works

Think of preferences like a restaurant menu board. The kitchen (database) has the master recipe book. The board (cache) shows what's available. When a dish changes, you erase the old board entry immediately—the next customer forces a fresh lookup from the recipes.

The database remains your source of truth. The cache is just a fast copy that gets refreshed when stale.

Invalidation vs. Update

  • Invalidate: Simpler, forces fresh read next time
  • Update: Faster (no cache miss), but requires write-through logic

Most notification systems prefer invalidation for simplicity and consistency.

Key Takeaway: Cache-aside pattern delivers sub-millisecond preference lookups while ensuring updates propagate instantly by invalidating cached entries and keeping the database as the authoritative source.