CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 3 of 6 · 3 min read

Writes: through, back and around

What happens to the cache and the database when data is written.

Think of a shop that keeps a price list at the till. When a price changes, the manager can update the list and the master book together, or update the list now and the book at night, or update the master book and throw the old list away so it is rewritten on the next sale. Each choice has a different risk.

Write-through

In write-through, every write goes to the cache and to the database as one operation, and succeeds only when both are done.

Example: a profile update is stored in the cache and the database before the user sees "saved".

Write-back (write-behind)

In write-back, a write goes to the cache and is acknowledged at once. The cache sends it to the database later, possibly in batches.

Example: a counter of video views is updated in the cache and saved to the database once every 10 seconds.

Write-around

In write-around, a write goes straight to the database and skips the cache. The cache is filled only when the item is next read.

Example: a user uploads a new document. It is written to the database. It enters the cache only if someone reads it.

The book "Building Microservices" describes write-behind caching. The write goes to a local cache and is flushed to the downstream source later, usually the canonical source of data. It is useful for bursts of writes, or when the same data is likely to be written many times. It also notes that buffering and batching writes is a useful optimisation, and that if the buffered writes are kept safely, they can be queued and sent when a downstream service returns after an outage.

A worked example. A counter is updated 1,000 times in 10 seconds, and the database call costs 5 ms.

Pattern Database writes Time the writer waits
Write-through 1,000 1,000 × 5 ms = 5 seconds in total
Write-back, flush every 10 s 1 about 0 for the database part
Write-around 1,000 5 seconds, and the cache is cold for the key

Write-back removes 999 of 1,000 database writes in this case. Its price is risk. If the cache machine dies before the flush, up to 10 seconds of updates are lost, and the database never learns of them. The data must be of a kind where that is acceptable, such as view counts, or the buffer must itself be stored safely, for example in a log on disk or a queue.

Write-through never loses a write the user was told about, but each write takes the time of both stores, and items that are written but never read still fill the cache.

Write-through Write-back Write-around
Write speed Slow (both stores) Fast Database speed
Data safety High Risk of loss until flushed High
Cache after a write Fresh Fresh Missing or stale until next read
Database load Every write Fewer writes Every write
Fits Data read soon after being written Counters, bursts, repeated writes Data written once and read rarely

Common mistake

Using write-back for data that must not be lost, such as payments. The cache is volatile memory. A crash before the flush loses the write while the user has already been told it worked.

Check yourself:

  • A page-view counter is updated 600 times a minute. With write-back and a flush every 30 seconds, how many database writes happen per minute?
  • Which write pattern risks losing acknowledged writes, and why?