Problem Statement
Compare cache-aside vs write-through for a product catalog. When do you choose each?
Explanation
Cache-aside is simple and cheap for read-heavy workloads with tolerant staleness. On miss, fetch from the database and set the cache with TTL; invalidate on writes. It avoids write amplification but risks cold-start misses. Write-through updates cache and DB synchronously on writes. Reads are more likely warm and consistent with the last write, but write latency is higher. Use write-through when freshness of just-written items matters and write rates are moderate.
Code Solution
SolutionRead Only
cache-aside: get→miss→db→set; write-through: set cache + persist
