AI Hints
Home›System Design Tutorial›Caching Basics
System Design · Lesson 57 of 120

Write Strategies

Through, back, around.

⏱ 2 min read▶ Video at 1:07:43🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

Reads are only half the story. When a product's price changes, the database and the cache must both know. There are three common strategies for writes.

Write Strategies
From the video: Write Strategies
  • Prices change
  • Cache and database
  • Three strategies

Three write strategies

Write through updates the cache and the database together, so the cache is always fresh, but every write is slower. Write back updates only the cache, and saves to the database later, which is very fast, but risky if the cache crashes. Write around writes only to the database, and lets the cache fill on the next read.

StrategyHowTrade-off
Write-throughcache + DB togetherfresh, slower writes
Write-backcache now, DB laterfast, can lose data
Write-aroundDB onlycache fills on read

A common recipe

A very common recipe combines two ideas. Read with cache aside. And on a write, update the database, then delete the cached key, rather than updating it. The next read misses, and loads the fresh value. Deleting is simpler and safer than trying to keep the cache exactly in step.

keyvalue
Readscache-aside
Writesupdate the DB
Thendelete the cache key
Next readloads fresh data

Summary

Write through keeps the cache fresh, write back is fast but risky, and write around skips the cache. A common recipe is update the database, then delete the key. Next, what to throw out when the cache is full.

Key takeaways
  • Through: fresh
  • Back: fast, risky
  • Around: lazy
Next up: Eviction and LRU — what to throw out