What are the cache-aside, write-through and write-back caching strategies?
MediumConceptCaching
Answer
These strategies define when data enters the cache and when writes reach the database, trading freshness, write latency and durability.
- Cache-aside (lazy loading): the application checks the cache and, on a miss, reads the database and fills the cache. On a write, it updates the database and deletes the cache entry. This is the most common pattern; only requested data is cached, but each miss is slower.
- Write-through: every write goes to the cache and the database synchronously, so the cache stays fresh at the cost of slower writes and caching data that may never be read.
- Write-back (write-behind): writes go to the cache and are flushed to the database asynchronously; writes are fast, but data is lost if the cache fails before flushing.
- Write-around: writes bypass the cache entirely, suiting data that is rarely re-read.