Write Path Patterns: Write Through, Write Behind, and Cache Aside with Delete on Write
Write Through
Write through updates both cache and origin synchronously: writes block until both succeed. Provides strong consistency (reads immediately see writes) at the cost of higher write latency. Database 5ms plus cache 1ms equals 6ms total unless parallelized. Use when read your writes consistency is critical: user facing counters (likes, votes), inventory systems, session data.
Write Behind
Write behind updates cache immediately and queues origin writes asynchronously. Offers low write latency (<0.5ms) but risks data loss on crashes and creates consistency windows where cache and origin diverge. Rare at scale without strict durability mechanisms. Appears in specialized scenarios: write heavy logging where losing a few entries is acceptable for throughput gains, or with write ahead logs and periodic checkpoints.
Cache Aside with Delete on Write
The dominant pattern: commit to origin first (ensuring durability), then delete affected cache keys, leaving reads to repopulate lazily. Separates write and cache concerns for fault tolerance. Critical ordering: commit before invalidate. Reversing allows stale data to be cached indefinitely when next read fetches old origin data. Race condition exists: if read happens between commit and invalidation, it might repopulate stale data. Mitigate with lease tokens: cache issues short lived tokens on misses, rejects sets if invalidation occurred since the lease was issued.
Choosing the Right Pattern
Default to cache aside with TTL safety net for 95% of use cases. Reserve write through for specific correctness requirements where write latency budget allows synchronous cache updates. Cache aside adds 0.5-2ms for invalidation messages vs baseline.