Sub topic 7
Write-around caching
This chapter assumes you know cache-aside (reads check the cache, then the database, and fill the cache on a miss), basic TTL behavior, and how a database write differs from a cache write.
What write-around does
Write-around sends writes straight to the database and skips the cache. The cache is filled only when someone reads the data. A key that was written but never read does not take up cache space.
Cache-aside reads already work this way. The difference is in the write path. Write-through updates the cache on every write. Write-around leaves the cache alone, so any older copy of the key stays there until something removes it.
When it fits
Write-around pays off when most written data is not read soon after. Typical cases are:
- Log or event records that are written once and rarely queried.
- Bulk imports and batch jobs that write millions of rows.
- Data where a small set of keys is read often and the rest is cold.
Writing every new record into the cache would push out hot entries and lower the hit ratio. Write-around protects the cache from that churn.
The stale-entry problem
Skipping the cache on writes has a cost. If a key is already cached and you update only the database, the cache keeps serving the old value. Pure write-around is therefore only safe when one of these holds:
- The data is never updated after it is created (append-only data).
- A short TTL bounds how long a stale value can live.
- The write path also invalidates the cached key.
The third option is what most systems do. It looks like the write step of the cache-aside example earlier in this tutorial.
def update_user(user_id, data, db, cache):
db.save_user(user_id, data) # write goes to the database only
cache.pop(f"user:{user_id}", None) # remove any stale copy; do not store the new valueThe function deletes the key and does not store the new value. The next read misses, loads the fresh row from the database, and fills the cache. Compare this with write-through, which would call cache[key] = ... here.
Trade-offs
| Aspect | Write-around |
|---|---|
| Write latency | Same as the database alone |
| Cache pollution | Low, because only read data is cached |
| First read after a write | Always a miss |
| Staleness risk | Present unless you invalidate or use a TTL |
The main weakness is the read-after-write miss. If your workload usually reads a record right after writing it, such as a user profile saved and then displayed, every first read pays the full database cost. In that case write-through is the better fit.
Common errors
- Forgetting to invalidate. Updating the database and leaving a long-lived cached entry produces stale reads that are hard to trace.
- Using write-around for read-after-write data. The hit ratio looks fine overall, but the user-visible reads right after a save are all slow.
- Assuming it is a different mechanism. Write-around is not a separate piece of software. It is a rule about the write path, and you implement it in application code or in the cache layer's configuration.
Exercise
A service stores sensor readings that are written every second and almost never read, except for a dashboard that reads the latest reading of each sensor. Would you use write-around or write-through for the readings, and what must the write path do for the latest-reading keys?
Show answer
Write-around, because most written readings are never read and caching all of them would evict useful entries. For the latest-reading keys, the write path must either invalidate the cached key after each database write or rely on a short TTL. Otherwise the dashboard shows an old value.