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.