除了基本的 cache-aside,还有几种 缓存策略 — cache-aside、write-through、write-behind、read-through — 各自有不同的一致性和性能权衡。理解它们(以及逐出策略)可以帮助你设计有效的缓存。
Cache-aside(懒加载 — 最常见)
App checks cache → miss → load from DB → populate cache → return.
✓ Only requested data is cached (efficient); resilient (works if cache is down)
✗ First request is a miss (slower); cache can be stale until TTL/invalidation
→ The default, most common strategy.
Write-through 和 write-behind
WRITE-THROUGH → write to cache AND DB together (synchronously) on every write
✓ Cache always fresh/consistent ✗ Writes are slower (two writes); caches unread data
WRITE-BEHIND (write-back) → write to cache immediately, write to DB ASYNC later
✓ Fast writes ✗ Risk of data loss if cache fails before the DB write; more complex
