---
title: 'Cache Invalidation Is a Consistency Protocol, Not a Delete Call'
excerpt: >-
Cache invalidation is difficult because writes, readers, and cache failures
race; choose an explicit freshness contract and design invalidation around it.
tags:
- cache-invalidation
- caching
- consistency
- distributed-systems
- dotnet
seoTitle: 'Cache Invalidation: Understanding Consistency Beyond Delete Calls'
seoDescription: 'Master cache invalidation as a consistency protocol with explicit freshness contracts and practical .NET examples for reliable distributed caching.'
category: system-design
---
Deleting a cache entry after a database write sounds deceptively simple. In reality, concurrent readers, asynchronous cache invalidation, and failures — partially or fully — create subtle races. A reader may already be loading the old value when a writer deletes it. The invalidation may fail or get delayed, allowing multiple application instances to serve stale data concurrently.
The hard problem is not calling Remove; it is defining which versions readers are allowed to observe, for how long, and how to enforce that in a distributed system.
Cache invalidation is fundamentally a consistency protocol between the system of record (usually a database) and derived copies (the cache). It requires explicit thinking about correctness guarantees and system behavior under failures—not just cache API calls.
Key points
- State and document a freshness contract: how stale may data be, and which operations require read-your-writes or freshness guarantees?
- Treat expiration (TTL) as a freshness boundary, not a freshness guarantee during the lifetime of the cache entry.
- Delete-after-write is a common technique but concurrent cache fills can undo invalidation with stale data.
- Reliable multi-instance invalidation requires persisting intent, typically with a transactional outbox, to close failure gaps.
- Versioned keys or stronger synchronization mechanisms help manage stale repopulation but add complexity.
- Cache invalidation is a coordination problem across readers, writers, and caches, not a single API call.
What cache invalidation actually coordinates
In the ubiquitous cache-aside pattern, a typical flow looks like this:
- Reader path: Check cache → on miss, read database → populate cache → return data.
- Writer path: Write to database → evict or refresh the cache for affected keys.
These paths are separate; coordination happens only if explicitly implemented. The subtlety arises because readers and writers operate concurrently, and the cache is eventually consistent, not transactional:
Cache-aside read and write flow
flowchart LR
R[Read Request] --> C{"Cache hit?"}
C -- yes --> RC[Return Cached Value]
C -- no --> DB[Read from Database]
DB --> CF[Cache Fill]
CF --> RC
W[Write Request] --> DBW[Write to Database]
DBW --> OUT[Write Invalidation Outbox]
OUT --> EVICT[Evict/Update Cache]
EVICT --> DONE[Done]Key notes:
- The outbox represents a durable invalidation signal, stored in the authoritative data store transactionally.
- Cache eviction happens asynchronously after database commit.
- Readers may see stale data during the window between commit and eviction.
- Cache fills concurrent with eviction may repopulate stale data.
The core invalidation races:
| Scenario | Problem | Why cache delete alone is not enough |
|---|---|---|
| Stale content reload | Reader reading DB version N, writer commits version N+1 and evicts, then reader writes version N to cache after eviction | No atomic coordination between writes, reads, and cache eviction |
| Failed eviction | Writer commits update but crashes before evicting cache. | Cache remains stale, serving outdated data indefinitely |
Neither race disappears just by choosing a faster cache. It requires explicit guarantees and implementation.
Choose a freshness contract first
“Fresh” is too vague on its own. Define a clear freshness contract based on your application's tolerances and use cases before selecting an invalidation technique:
| Freshness Contract | Description | Applicability | Implementation hints |
|---|---|---|---|
| Bounded staleness | Readers may see data stale by up to a known interval (e.g. 5 min). | Systems where some lag is acceptable, e.g. product catalogs, social feeds. | Use TTL expiration with clear understanding of expiration vs write timing; add invalidation for critical updates. |
| Eventual freshness | Changes propagate asynchronously, old values tolerated temporarily. | High-scale, multi-region systems where synchronous coordination is costly. | Durable events and retry mechanisms, outbox pattern, idempotent event handlers. |
| Read-your-writes | After a write, that user’s reads must reflect it immediately. | User profiles, shopping carts, single-tenant writes. | Bypass cache on reads after writes, or use version-based cache keys. |
| Strong consistency | All reads must reflect the latest committed write (linearizability). | Financial systems, inventory allocation, booking systems. | Read directly from authoritative store, or use advanced protocols like distributed transactions or linearizable caches. |
A contract must specify scope:
- Does it apply across tenants?
- Multi-region replicas?
- All side caches for the same data (detail, list, aggregates)?
- Does cache key composition fully capture request parameters like authorization scope or locale?
Commonly overlooked cache key errors cause stale reads even if invalidation works perfectly.
Common invalidation strategies, and trade-offs
| Strategy | Pros | Cons | Use cases |
|---|---|---|---|
| TTL/Expiration | Simple, bounds stale window | Does not guarantee freshness, can serve stale data until expiry | Non-critical stale reads, safety net fallback |
| Delete after write | Easy to reason about and implement | Non-atomic. Race conditions with concurrent readers and evictions | Where short stale window is acceptable |
| Refresh / Write-through | Avoids next miss, keeps cache fresh immediately | Requires coordination, prone to out-of-order updates | Scenarios needing fresher cache on update |
| Versioned keys | Clear version boundaries, old versions unreachable | Version coordination complexity, cache entry buildup | Multi-key invalidation, read-your-writes paths |
| Durable events + outbox | Survives process crashes, distributed invalidation | Complexity, requires monitoring and idempotency | Multi-instance distributed cache invalidation |
TTL and expiration details
A TTL limits how long an entry remains in cache; it does not guarantee freshness for that duration. For example, a 5-minute TTL means data can be stale for up to 5 minutes, plus any delay in cache refills.
Sliding expiration extends lifetime on reads but can prolong staleness indefinitely if the data is not invalidated or refreshed. Combine absolute and sliding expirations carefully.
Delete after a successful write
The pattern:
- Commit data change to the database.
- Remove affected cache key(s).
Simple to implement but has key limitations:
- No atomicity: a failure between commit and eviction leaves cache stale.
- Concurrent readers may write old values back after eviction (stale repopulation).
This approach is common and often sufficient when slight staleness is acceptable, but beware of edge cases in high concurrency or multi-instance systems.
Refresh or write-through cache
Write-through updates cache as part of the write path, not just evicting:
- After database commit, update cache with new value.
- Can eliminate next cache miss.
Downsides:
- Refresh can fail independently of commit.
- Writes may apply out-of-order across concurrent writers.
- Requires versioned or timestamped cache entries to reject stale updates.
In practice, write-through cache implementations rarely guarantee strong consistency without additional coordination.
Versioned cache keys
Versioned keys embed a record version into cache keys, for example:
product:42:v17user:1001:profile:v12
Readers must first obtain the current version from a durable source, then use the versioned key. Writers advance the version atomically.
Benefits:
- Old keys become unreachable once the version moves forward.
- Stale repopulation is prevented because readers never overwrite newer versions.
Drawbacks:
- Ensuring the current version is fresh can be challenging.
- Key explosion and cache storage growth if the versions accumulate.
- More complex invalidation logic.
Use this approach for strong read-your-writes and multi-key cache invalidation scenarios.
Durable events and the outbox pattern
In multi-instance or distributed setups, asynchronous delivery is essential for invalidation.
The transactional outbox pattern avoids losing invalidation events:
- Write data and invalidation event(s) in same database transaction.
- Poll or listen for outbox events, publish to subscribers responsible for cache eviction or update.
- Event consumers perform idempotent invalidations.
Advantages:
- Guarantees delivery across crashes.
- Supports multi-instance, distributed caches.
- Avoids race where database update commits but invalidation is lost.
This is the most reliable general-purpose invalidation technique but adds complexity and operational overhead.
Failure modes to design for
Caching correctness breaks on predictable failure patterns:
| Failure Mode | Description | Mitigation |
|---|---|---|
| Invalidation failure | Eviction call fails or is skipped | Retry asynchronously, alert on failures, durable outbox pattern |
| Stale repopulation | Cache miss triggers load of old data post-eviction | Version checks on cache insert, serialize writers, accept bounded staleness |
| Incomplete key coverage | Some stale caches not invalidated (lists, indexes) | Track dependencies, explicit invalidation chains or metadata-driven invalidation |
| Negative caching | “Not found” cached hides newly created data | Short TTLs for negative cache entries, invalidate on creation |
| Cache stampedes | Many requests simultaneously miss and hit DB | Request coalescing, locking, background refreshes with stale-while-revalidate |
| Out-of-order delivery | Events arrive late or out of order | Include monotonically increasing versions with events to discard older updates |
Practical design checklist
- Write down the allowed stale-read interval and identify requests needing stronger consistency (e.g. read-your-writes).
- Design comprehensive cache keys capturing all parameters changing the output (tenant, user scope, query params).
- Start with cache-aside plus an absolute TTL where bounded staleness is tolerable.
- Add an outbox and idempotent invalidation subscribers when invalidation must survive process restarts or multi-instance deployment.
- Incorporate versioned keys or bypass cache for requests that cannot tolerate stale repopulation.
- Instrument and measure invalidation latency, cache hit ratios, stale data incidents, and failure rates.
- Test concurrent reads and writes, including failure and retry scenarios.
Observability: what to monitor, log, and trace
Effective cache invalidation requires monitoring and alerting:
What to log:
- Cache hits and misses with keys and request context.
- Invalidation events: enqueue, delivery, retry attempts, failures.
- Eviction/removal calls with success or error status.
- Version bumps with previous and new versions.
- Errors in cache write, read, or invalidation paths.
Structured log example in C# with Serilog:
logger.Information("Cache invalidated {CacheKey} for reason {Reason}",
cacheKey, reason);
Key metrics (Use RED and USE patterns where applicable):
| Metric | Description | Type |
|---|---|---|
| Cache hit rate (%) | % of reads served from cache | Ratio |
| Cache miss count | Count of cache misses | Counter |
| Cache eviction count | Number of evictions done | Counter |
| Cache write failures | Failed attempts to write/evict | Counter |
| Invalidation event lag | Time between DB commit and eviction | Histogram/Timer |
| Outbox event publish failures | Number of failed event deliveries | Counter |
Distributed Tracing (OpenTelemetry example in .NET)
Instrument calls to cache and database operations, including:
- Span for database commit.
- Span for cache removal or update.
- Span for outbox event production and consumption.
using var activity = tracer.StartActivity("UpdateProduct");
// ... DB update
using (var cacheSpan = tracer.StartActivity("CacheEvict"))
{
await cache.RemoveAsync(key, cancellationToken);
}
Trace context propagation across distributed services helps correlate cache misses, evictions, and stale reads in production.
SLA and SLO considerations for caching
| SLI | SLO Target | Rationale |
|---|---|---|
| Cache hit rate | ≥ 85% overall | High hit rate reduces DB load |
| Invalidation delivery time | ≤ 500ms median | Limits stale reads to acceptable window |
| Cache miss latency | ≤ 50ms median | Fast enough for user-facing reads |
| Eviction success rate | ≥ 99.9% | High reliability for consistency |
| Stale data incidents | ≤ 1 per 100,000 reads | Very rare stale data returned |
Minimal example reminder: eviction only
using Microsoft.Extensions.Caching.Memory;
using var cache = new MemoryCache(new MemoryCacheOptions());
const string key = "product:42";
cache.Set(key, new Product(42, "Keyboard"), TimeSpan.FromMinutes(5));
var found = cache.TryGetValue<Product>(key, out var cached);
// After the authoritative product update succeeds:
cache.Remove(key);
record Product(int Id, string Name);
Note eviction here is fire-and-forget and can be undone by concurrent loads.
Realistic ASP.NET Core cache-aside example revisited
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Distributed;
public sealed record ProductSnapshot(int Id, string Name, decimal Price);
public sealed class Product
{
public int Id { get; set; }
public string Name { get; set; } = "";
public decimal Price { get; set; }
}
public sealed class AppDbContext(DbContextOptions<AppDbContext> options)
: DbContext(options)
{
public DbSet<Product> Products => Set<Product>();
}
public sealed class ProductService
{
private readonly AppDbContext db;
private readonly IDistributedCache cache;
private static string Key(int id) => $"product:v1:{id}";
public ProductService(AppDbContext db, IDistributedCache cache)
{
this.db = db;
this.cache = cache;
}
public async Task<ProductSnapshot?> GetAsync(int id, CancellationToken cancellationToken)
{
var json = await cache.GetStringAsync(Key(id), cancellationToken);
if (json is not null)
{
var cached = JsonSerializer.Deserialize<ProductSnapshot>(json);
if (cached is not null) return cached;
}
var product = await db.Products.AsNoTracking()
.Where(p => p.Id == id)
.Select(p => new ProductSnapshot(p.Id, p.Name, p.Price))
.SingleOrDefaultAsync(cancellationToken);
if (product is not null)
{
await cache.SetStringAsync(Key(id), JsonSerializer.Serialize(product),
new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5)
}, cancellationToken);
}
return product;
}
public async Task<bool> UpdateAsync(int id, string name, decimal price, CancellationToken cancellationToken)
{
var product = await db.Products.SingleOrDefaultAsync(p => p.Id == id, cancellationToken);
if (product is null) return false;
product.Name = name;
product.Price = price;
await db.SaveChangesAsync(cancellationToken);
// Post-commit eviction, potential race exists
await cache.RemoveAsync(Key(id), cancellationToken);
return true;
}
}
This example shows a common but imperfect approach, which benefits from instrumentation and operational monitoring to catch failures and stale reads.
Conclusion: design cache invalidation as a protocol, not just a call
Cache invalidation is a distributed consistency problem involving:
- Reliable delivery of invalidation signals despite process crashes and restarts
- Coordination of concurrent readers and writers to avoid stale repopulation
- Explicit, documented consistency contracts for client expectations
- A choice of expiration, versioning, or event-driven approaches based on application needs
There is no silver bullet. Success requires marrying a clear contract to an appropriately complex but understandable implementation, plus solid observability and testing across failure modes.
For detailed references on caching in ASP.NET Core, see: