Cache Policies & Eviction
### Cache Invalidation: The Hard Problem
As the famous saying goes, "There are only two hard things in Computer Science: cache invalidation and naming things." Keeping the cache synchronized with the database is challenging.
### Caching Strategies
1. **Cache Aside (Lazy Loading):** Application queries the cache. If a miss, it queries the database, writes to the cache, and returns data. 2. **Write-Through:** Application writes data to the cache AND the database simultaneously. Slower writes, but guarantees consistency. 3. **Write-Behind (Write-Back):** Application writes only to the cache. The cache asynchronously updates the database. Fast, but risks data loss on crash.
### Eviction Policies
When the cache is full, it must evict old data. The most common policy is **LRU (Least Recently Used)**, which drops data that hasn't been accessed in the longest time.


