Why Caching Strategy Matters
A cache speeds reads, but how it stays in sync with the database changes correctness and risk. Three common patterns handle writes differently.
Cache Aside
The application checks the cache first. On a miss it loads from the database, then stores the value in the cache for next time. Writes go straight to the database and the cache entry is deleted or left to expire. This is simple and the cache only holds data that is actually requested.
Write Through
Every write goes to the cache, which then synchronously writes to the database. The cache stays fresh and reads are fast, but each write pays the cost of touching both stores.
Write Back
Writes land in the cache and return immediately. The cache flushes to the database later in batches. This gives very fast writes but risks data loss if the cache fails before flushing.
Key idea
Cache aside is simple and lazy, write through keeps the cache fresh at write cost, and write back is fastest but can lose data that has not yet flushed.