Video in progress
What is the difference between write-through and write-behind caching?
With write-through, the write goes to the cache and the DB synchronously, and the reply returns after both are written. With write-behind, the write goes only to the cache, and the DB gets it later in batches in the background: fast, but data can be lost if the cache goes down.
📖 Answer
In cache-aside the cache sits "on the side". In these two patterns the cache sits on the write path.
Write-through
client -> cache -> DB (synchronous) -> OK
The cache is always fresh, so reads rarely miss. But every write goes to two places and latency goes up. Data nobody reads also piles up in the cache, so you still need a TTL.
Write-behind (write-back)
client -> cache -> OK
cache -> (every 1 s, in a batch) -> DB
Writes are very fast and the pressure on the DB drops. For example, you can collect thousands of "user online" updates per second, or a view count, in Redis and flush them to the DB with a single UPDATE every minute. The price: if Redis goes down, data that hasn't reached the DB yet is lost, and ordering and constraint errors in the DB show up late.
Redis doesn't do either of these automatically. The logic is written by the application (or by add-ons like Redisson's MapWriter or RedisGears). I only use write-behind for statistics that can survive a loss.
Sign up to mark studied questions and save your progress.