Yashotantra Academy logo

4 · Performance & Fault Tolerance

6. Caching Strategies & Preventing Cache Stampedes

Cache-Aside, Write-Through, Write-Back, Eviction policies, and the Thundering Herd.

10 min read · 3 MCQs

Caching Access Patterns

Cache-Aside (Lazy Loading): The application reads cache first; on miss, reads database and populates cache. Write-Through: The application writes to cache, which synchronously writes to the database before returning. Write-Back (Write-Behind): The application writes to cache, and an asynchronous worker flushes dirty records to the database in batches.

Eviction Policies: LRU, LFU, ARC

When memory limits are reached, eviction algorithms choose what to discard: Least Recently Used (LRU) discards items that haven't been read for the longest time; Least Frequently Used (LFU) discards items with the lowest access count; Adaptive Replacement Cache (ARC) dynamically balances both.

The Thundering Herd & Cache Stampede

When a hot cached key expires, thousands of concurrent requests miss the cache simultaneously and all execute expensive database queries at the exact same millisecond. Mitigations include mutex locking (Singleflight), probabilistic early expiration (XFetch algorithm), or pre-warming background refreshes.

Chapter quiz

3 questions · pass mark 75%
  1. 1. In the Cache-Aside pattern, what happens on a cache miss?

  2. 2. What is a 'Cache Stampede' (Thundering Herd)?

  3. 3. Which caching pattern offers the lowest write latency to the client?