Skip to content

Cache-aside vs write-through — pick the consistency model

A practical comparison of cache-aside, write-through, and write-behind — what each guarantees, where each breaks, and how to choose.

The choice between cache-aside and write-through looks small in a design doc and becomes a year-long argument in production. They have different failure modes, different consistency guarantees, and different operational footprints. This article is the side-by-side we hand engineering teams: how each works, what each guarantees, and which one to pick for a given workload.

How to verify

Inspect which pattern the application is using today. A grep of the data-access layer usually tells the story.

# Cache-aside signature: code calls cache.get, falls through to db, then cache.set
grep -rn 'cache.get\|redis.get' src/ | head
grep -rn 'cache.set\|redis.set' src/ | head -20

# Write-through signature: every db write is paired with a cache write
grep -rn 'db.write\|db.save\|model.save' src/ | head
# and the surrounding code should always write to cache too

If you see writes that touch the database but no matching cache update, the code is cache-aside. If every model save is wrapped with a cache.set of the same key, it is write-through (or trying to be).

What’s happening

Cache-aside (also “lazy loading”): the cache is a side store. Reads check cache first, fall through to origin on miss, populate cache. Writes go to origin and explicitly invalidate (or update) the cache. The cache and the database are independent; the application is responsible for keeping them in sync.

Write-through: every write goes to the cache, which writes through to the database synchronously. Reads come from the cache; if the cache has it, you get the freshest value; if not, the cache loads from the database on demand. The cache appears as the front door for both reads and writes.

Write-behind (also “write-back”): writes go to the cache and the cache acknowledges immediately; a background process flushes writes to the database asynchronously. The fastest pattern, with the largest data-loss window if the cache fails before the flush.

The consistency stories:

  • Cache-aside: eventual. If the cache write fails after the database write succeeds, the cache has stale data until TTL or next invalidation. Read-your-writes is not guaranteed.
  • Write-through: strong (assuming synchronous cache+db). The cache is the read path and is always populated with the latest write.
  • Write-behind: weak. The database lags the cache. Reads from outside the cache (analytics, BI tools) see stale data; if the cache is lost, in-flight writes are gone.

The decision

  1. Pick cache-aside when: writes are uncommon, the data has natural TTL semantics (catalog data, user profile lookups), and the application can tolerate stale reads for a window after a write. This is the most flexible pattern and the most common in production.

  2. Pick write-through when: reads and writes are interleaved, read-your-writes is required, and the cache is sized to hold the active working set. Session stores often fit this.

  3. Pick write-behind when: write throughput is the bottleneck, the application can tolerate the data-loss window of a cache crash, and there is no external reader of the database. Counter aggregation, telemetry buffering.

  4. Cache-aside implementation. Read-through, write-around, explicit invalidation.

    def get_user(uid):
        v = redis.get(f"user:{uid}")
        if v is not None: return json.loads(v)
        u = db.users.find_one({'_id': uid})
        if u: redis.set(f"user:{uid}", json.dumps(u), ex=600)
        return u
    
    def update_user(uid, fields):
        db.users.update_one({'_id': uid}, {'$set': fields})
        redis.delete(f"user:{uid}")   # invalidate; next read repopulates
  5. Write-through implementation. Cache library or middleware wraps both read and write.

    def get_user(uid):
        return cache.get_or_load(f"user:{uid}", lambda: db.find(uid), ttl=600)
    
    def update_user(uid, fields):
        u = db.users.update_one({'_id': uid}, {'$set': fields})
        cache.set(f"user:{uid}", u, ttl=600)
        return u

Operational notes

  • Cache-aside’s invalidation race: between “write to db” and “delete from cache” another reader can populate the cache with the pre-write value. The fix is to delete before AND after the write, or to use a versioned key.
  • Write-through is only as good as the cache’s durability. If you treat the cache like in-memory state and lose it, you lose writes.
  • Write-behind requires that the database can survive the burst the cache will produce when it flushes; size for that.
  • All three patterns can use the same cache infrastructure (Redis, memcached); the difference is in the application code, not the store.
  • Mixed patterns (cache-aside on most data, write-through on session) are common and fine — be explicit about which data follows which model.

In the engagements we run, the pattern choice is made per data class, not once for the whole application. Stack Harbor’s managed operations practice is to document the chosen pattern per data class, write the assertions that check it (no orphan cache entries, no stale-after-write windows beyond the budget), and treat the cache layer as a first-class part of the data model.