keepcalculator

06 Numbers and Powers

Cache Invalidation: Three Write Patterns for Freshness

Card No. 06·79492 · Updated · Standard formula, shown below

Cache invalidation is the process of keeping cached data fresh when the underlying source changes. Three common write patterns—write-through, write-around, and write-back—each handle this differently, trading off between data freshness, read performance, and the risk of serving stale data after a failure. This article explains how each pattern works, where it excels, and what happens when things go wrong.

Background: Why Cache Invalidation Is Hard

Caches exist to make reads faster, but every write to the source of truth creates a moment where the cache holds an outdated copy. The classic problem is deciding when and how to update or remove that stale entry. Invalidation is hard because it requires coordination between the cache and the database, and because failures (network partitions, crashes, timeouts) can leave the cache permanently out of sync.

There are two fundamental approaches: proactive (update the cache as part of the write) and reactive (invalidate the cache and let the next read repopulate it). The three patterns below are the standard ways to implement these.

Write-Through: Update the Cache on Every Write

In write-through, the application writes to the cache first, and the cache synchronously writes to the database. The write is not considered complete until both the cache and the database have acknowledged it. This guarantees that the cache always contains the latest data—as long as the write succeeds.

Pros:

  • Cache is always fresh (no stale reads after a successful write).
  • Simple to reason about: read path never needs to check the database for freshness.

Cons:

  • Every write pays the latency of both the cache and the database, so write throughput is limited by the slower of the two.
  • If the cache write fails but the database write succeeds (or vice versa), you can end up with an inconsistent state.

Failure mode: if the database goes down, the cache write may still succeed, but the next read will return data that was never persisted. If the cache goes down, writes fail entirely unless you add a fallback that bypasses the cache.

Write-Around: Write to the Database, Invalidate the Cache

Write-around skips the cache entirely on writes. The application writes directly to the database, and then invalidates (deletes) the corresponding cache entry. The next read misses the cache and repopulates it from the database.

Pros:

  • Writes are fast (no cache write overhead).
  • Cache is never polluted with data that is rarely read.

Cons:

  • Immediately after a write, the next read is a cache miss, so it is slower.
  • If the invalidation fails (e.g., the cache delete times out), the cache will serve stale data indefinitely.

Failure mode: the classic “stale cache” problem. If the invalidation message is lost, the cache keeps serving old data. Many systems use a short TTL (time-to-live) as a safety net, so even if invalidation fails, the entry eventually expires.

Write-Back: Write to Cache, Flush Later

Write-back (also called write-behind) writes to the cache immediately and acknowledges the write, then asynchronously flushes the change to the database at a later time. This gives the lowest write latency and the highest write throughput, because writes are batched and done in the background.

Pros:

  • Very fast writes—the application only waits for the cache.
  • Batching reduces load on the database.

Cons:

  • Data loss risk: if the cache crashes before the flush, the write is lost.
  • Reads may return data that is not yet in the database, which can cause issues if other services read from the database directly.

Failure mode: the worst-case scenario is a cache node failure before the flush. You lose acknowledged writes. To mitigate, some systems use a persistent write-ahead log or replicate the cache, but that adds complexity.

How to Choose Between the Patterns

The right pattern depends on your workload’s read/write ratio and tolerance for staleness or data loss.

  • Use write-through when reads must never see stale data and write volume is low enough that the extra latency is acceptable (e.g., user profiles, configuration data).
  • Use write-around when writes are frequent but reads are infrequent, or when you can tolerate a one-time cache miss after each write (e.g., blog post views, news feeds).
  • Use write-back when write throughput is critical and you can accept a small risk of data loss (e.g., analytics counters, chat message history where a lost message is tolerable).

Many production systems combine patterns. For example, a write-around with a short TTL is a common default because it bounds staleness without requiring synchronous cache updates.

Common Problems and Misconceptions

Misconception: “Invalidation means deleting the cache entry.” Invalidation can be a delete, an update, or a version check. Deleting is simplest but causes a cache miss; updating avoids the miss but requires the write path to know the new value.

Problem: Thundering herd. When an entry expires, many concurrent reads all miss and hit the database. Solutions include locking (only one request repopulates) or probabilistic early expiration.

Problem: Cache stampede with write-around. If a popular entry is invalidated, the next read repopulates it, but if that read fails, the entry stays empty. A background refresh can help.

Problem: Ordering issues. In write-back, if two writes happen to the same key, the flush order matters. If the cache flushes them out of order, the database ends up with an older value. Using a per-key queue or version numbers solves this.

Failure Handling and Consistency Guarantees

Each pattern has a different consistency guarantee under failure:

  • Write-through gives strong consistency if both writes succeed, but a partial failure (cache ok, DB fail) leaves the cache ahead of the database. A common fix is to treat the cache as a “write-through” only if the database write succeeds first, and roll back the cache on failure.
  • Write-around gives eventual consistency—after a successful invalidation, the next read is fresh. But if invalidation fails, staleness can last until TTL expires.
  • Write-back gives eventual consistency with a risk window. The longer the flush delay, the larger the window where a read might see data that is not yet durable.

In distributed systems, you also need to consider that the cache and database may be on different machines, so network partitions can cause writes to succeed on one side and fail on the other. No pattern can fully prevent that without a distributed transaction, which is why many systems accept a small amount of staleness.

Alternatives and Advanced Techniques

Beyond the three basic patterns, there are other strategies:

  • Time-to-live (TTL): Even with write-through, a TTL protects against bugs where invalidation is missed.
  • Version numbers or timestamps: Store a version with each cache entry; on read, compare with the database version. This avoids invalidation entirely but adds a lookup.
  • Change Data Capture (CDC): Instead of invalidating from the application, a process watches the database’s transaction log and updates the cache automatically. This is robust but requires infrastructure.
  • Read-through with cache-aside: The application checks the cache; on a miss, it loads from the database and populates the cache. This is a common alternative to write-around, but it still needs an invalidation strategy for writes.

For most systems, the simplest reliable approach is a combination: write-around for writes, a short TTL (e.g., 60 seconds) as a safety net, and a background job that periodically refreshes hot entries. That gives you freshness within the TTL window and avoids the complexity of synchronous cache updates.

Frequently Asked Questions

Q: What is the difference between cache invalidation and cache eviction?
Invalidation is the deliberate removal or update of a cache entry because the underlying data changed. Eviction is the automatic removal of entries to free space (e.g., LRU policy). They are separate concerns but often interact: an invalidated entry may be evicted sooner, and an evicted entry may cause a cache miss.

Q: Can I use write-through and write-back together?
Yes. A common hybrid is write-through for critical data and write-back for less critical data, or write-through for the primary cache and write-back for a secondary cache tier. The key is to keep the consistency requirements of each data type in mind.

Q: How do I handle cache invalidation in a microservices architecture?
If multiple services write to the same data, you need a shared invalidation mechanism. Options include publishing an event to a message bus (each service subscribes and invalidates its own cache) or using a distributed cache that supports atomic invalidation. The challenge is ensuring the event is delivered exactly once, which is hard; a TTL is a pragmatic fallback.

Q: What happens if the cache and database are inconsistent after a failure?
It depends on the pattern. For write-through, you may need to reconcile by re-reading from the database and updating the cache. For write-around, a TTL will eventually fix it. For write-back, you may lose data if the cache crashed before flush. There is no universal fix; you must design for the failure mode you can tolerate.

Q: Is “cache invalidation” really one of the two hard things in computer science?
The quote is often attributed to Phil Karlton, but its origin is unverified. The idea is that naming things and cache invalidation are notoriously error-prone because they require global reasoning about state. The three patterns here are just the starting point; real systems often need TTLs, versioning, and distributed coordination to get it right.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →