Caching strategies and cache invalidation
In short: A cache keeps frequently used data in fast storage. The hard part is keeping it current: there are strategies such as cache-aside, write-through and write-back, and data must be invalidated.
In more detail: Caches speed up access and relieve databases but cost memory and create the risk of stale data. A well-known saying is that cache invalidation is one of the hardest problems in computer science.
In Depth
- Cache-aside: The application checks the cache, loads from the database if missing and fills the cache.
- Write-through: Writes go to cache and database. Consistent, slower.
- Write-back: First into the cache, later into the database. Fast, risk on failure.
- Invalidation: TTL (expiry time), explicit deletion on write, version keys.
- Eviction: LRU (least recently used), LFU (least frequently used).
- Cache stampede: Many requests reload at once when an entry expires. Remedies: lock, early refresh.
- Hit rate: Most important metric.
See also: Memory hierarchy and cache lines, Bloom filters and probabilistic data structures