Skip to content

Lesson 5 of 9 · 26m

Read-Through, Write-Through, Write-Behind, and Cache-Aside

Compare application caching patterns by consistency behavior, failure modes, and operational complexity.

The names sound like architecture astronautics, but each cache pattern is just a decision about who reads, who writes, and what happens when either side fails. Most production systems use cache-aside because it is simple. Most production cache incidents happen because nobody wrote down its consistency rules.

Cache-aside: the default pattern

The application checks the cache, falls back to the database, then populates the cache:

def get_product(product_id):
    key = f"catalog:v4:product:{product_id}"
    cached = redis.get(key)
    if cached is not None:
        return decode(cached)

    product = db.fetch_product(product_id)
    if product is not None:
        redis.set(key, encode(product), ex=300)
    return product

Cache-aside is easy to reason about and resilient to cache failure. Its weakness is staleness: after the database changes, the cache still holds the old value until invalidated or expired.

Writes must have a plan

Two common write strategies are write-through and write-invalidate:

write-through       update cache and database together
write-invalidate    update database, delete cache key, next read repopulates
write-behind        update cache immediately, persist asynchronously

Write-through sounds clean but turns every write into a distributed transaction problem unless carefully scoped. Write-invalidate is usually simpler: commit to the database, then delete the key. Write-behind is fast and dangerous; if the cache or worker fails before persistence, you lose acknowledged writes or must build serious recovery machinery.

The delete-after-commit ordering trap

Deleting before database commit invites a race:

1. request A deletes cache key
2. request B misses cache and reads old database row
3. request B stores stale value
4. commit finishes after that

The safer order is commit first, invalidate second. Even then, races exist. A short TTL is the backstop. For high-contention rows, versioning the value can help because a stale write cannot overwrite a newer version.

Read-through caches centralize fallback logic

In read-through, the cache abstraction itself knows how to load missing data. This reduces duplicated code and makes request coalescing easier, but it couples cache infrastructure to domain logic. It is useful when many callers need the same object contract.

Failure behavior is part of the pattern

Decide whether cache failure means fail-open to the database or fail-closed to protect it. A product catalog can usually degrade to database reads. A hot authentication path may need stricter behavior because a cache outage can become a database outage if every request falls through simultaneously.

cache error -> log -> query origin with timeout -> optionally skip write-back

Do not silently treat cache errors as cache misses at unlimited concurrency. That is how a Redis incident becomes a Postgres incident.

Field notes

  • Cache-aside does not mean write whatever whenever; invalidation and TTL are part of the design.
  • Write-behind needs durable queues, replay logic, and explicit user-visible semantics. Most teams should not start there.
  • Deleting a key is often safer than overwriting it when serialization compatibility is uncertain.
  • Add jitter to TTLs for keys written in batches, or they will expire in synchronized waves.
  • Test the cache-down path. Production will test it for you otherwise.

Was this lesson helpful?

Sign in to rate