Caching Strategies

Cache-Aside, Read-Through, Write-Through, and Write-Behind (Write-Back) synchronization patterns

Difficulty: Beginner · Read time: 8 min · Access: Free Interactive Lesson

How It Works

In-memory caches (Redis, Memcached) serve hot reads in under 1 ms in RAM (Random-Access Memory) instead of 10–100 ms disk-backed SQL queries. How your application keeps Cache and Database (DB) synchronized depends on your read/write workload: Cache-Aside (lazy loading by the app), Read-Through / Write-Through (cache layer handles DB reads/writes synchronously), or Write-Behind / Write-Back (writes buffer in cache and flush asynchronously to the DB in batches).

Why It Matters

Picking the wrong caching pattern causes stale reads after updates, wasted RAM caching cold keys nobody reads, or permanent data loss if a cache server crashes before flushing unpersisted Write-Behind buffers.

Interactive Architecture Scenarios

  • Cache-Aside (Lazy Loading) Hit vs Miss
  • Write-Behind (Async Batched DB Flush)
  • Failure Mode: Read/Write Race Leaving Permanent Stale Cache

Key Terms & Short Forms

  • Cache-Aside (Lazy Loading Application Cache): Pattern where the app checks Redis first; on a miss, it queries the DB and writes the result into Redis with a TTL.
  • TTL (Time-To-Live): Expiration duration (e.g., `TTL = 300s`) after which a cached key is automatically deleted so stale data cannot linger forever.
  • Write-Through (Synchronous Dual-Write Caching): Every write updates both the Cache and the Database synchronously before returning OK—keeping cache fresh at the cost of slower writes.
  • Write-Behind (Write-Back Asynchronous Buffered Caching): Writes update Cache RAM immediately (<1 ms) and flush to the DB asynchronously in coalesced batches for extreme write throughput.
  • Cache Hit Ratio (Percentage of Reads Served from Cache): Ratio `Hits / (Hits + Misses)`; pushing hit ratio from 80% to 98% cuts downstream database read load by 10x.
  • Cache Invalidation (Stale Key Deletion / Update): Deleting (`DEL key`) or updating cached entries when the underlying database row changes so users do not read outdated state.

Related Lessons in Caching & Performance