← All blogs

FullStack Engineering CourseLesson 6 of 20 20 min

Caching in .NET: Patterns, Redis and Invalidation

Why cache, and what to cache

A cache keeps a copy of data in a faster place, so the next read does not have to go back to the slow source. Reading from memory takes well under a microsecond, a Redis call inside the same Azure region usually takes around a millisecond, and a database query often takes several milliseconds or more (these are rough orders of magnitude, not promises). Caching the right data cuts response times and takes load off the database.

Good candidates for caching:

  • Data that is read much more often than it changes: payer details, procedure codes, configuration, feature flags.
  • Results that are expensive to compute: dashboards, aggregates, search results.
  • Responses from slow or rate-limited external services, where a short-lived copy is acceptable.

Poor candidates:

  • Data that must always be exactly current, such as an account balance used to approve a payment.
  • Data that changes on almost every read, where the cache would mostly hold stale copies.
  • Sensitive data (patient records, tokens) in a shared cache, unless it is encrypted, access-controlled and has a clear expiry.

Every cache trades freshness for speed. Before caching anything, decide how stale it may be. "Payer names may be 10 minutes old" is a business decision, not a technical one.

Where a cache can live

LevelExampleSpeedShared across instancesNotes
Browser or CDNCache-Control headers, Azure Front DoorFastest for the userYes (CDN)Only for responses that are safe to share or are private to one user
Output cacheASP.NET Core output cachingVery fastPer instance (or Redis-backed)Caches whole HTTP responses on the server
In-process (L1)IMemoryCacheFastest on the serverNo: each instance has its own copyLost on restart; instances can disagree
Distributed (L2)Redis via IDistributedCacheAbout a network round tripYesSurvives restarts; one copy for all instances
Both (L1 + L2)HybridCache (.NET 9+)Fast, with a shared backing storeYesMemory first, then Redis, then the source

Most services use a combination: a small in-memory cache for very hot data, Redis for data that all instances should share, and HTTP caching for public responses.

Caching patterns

Cache-aside (also called lazy loading) is the most common pattern. The application owns the logic:

  1. Look in the cache.
  2. If the value is there (a cache hit), return it.
  3. If not (a cache miss), read it from the database, store it in the cache with an expiry, and return it.
  4. When the data changes, update the database, then remove the cache entry so the next read loads the new value.
C#
public sealed class PayerService(IDistributedCache cache, IPayerRepository repository)
{
    private static readonly DistributedCacheEntryOptions Options =
        new() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10) };

    public async Task<Payer?> GetAsync(string code, CancellationToken ct)
    {
        string key = $"payer:{code}";
        string? json = await cache.GetStringAsync(key, ct);
        if (json is not null)
            return JsonSerializer.Deserialize<Payer>(json);            // hit

        Payer? payer = await repository.GetAsync(code, ct);            // miss: go to the database
        if (payer is not null)
            await cache.SetStringAsync(key, JsonSerializer.Serialize(payer), Options, ct);
        return payer;
    }

    public async Task UpdateAsync(Payer payer, CancellationToken ct)
    {
        await repository.UpdateAsync(payer, ct);                       // 1. the database first
        await cache.RemoveAsync($"payer:{payer.Code}", ct);            // 2. then drop the stale copy
    }
}

// Program.cs
builder.Services.AddStackExchangeRedisCache(o =>
{
    o.Configuration = builder.Configuration.GetConnectionString("Redis");
    o.InstanceName = "claims-api:";                                    // prefix for every key
});

Other patterns:

PatternHow it worksGood forWatch out for
Cache-asideThe app reads the cache, loads on a miss, and removes entries on writesMost read-heavy dataThe first read after a change is slow; stale data until removal or expiry
Read-throughThe cache library loads missing values itself, through a loader you registerKeeping loading logic in one place (HybridCache works this way)Same staleness rules as cache-aside
Write-throughEvery write goes to the cache and the database togetherData that is read right after it is writtenWrites get slower; the cache fills with data nobody reads
Write-behind (write-back)Writes go to the cache first and reach the database later, in batchesVery high write rates, such as counters and telemetryData loss if the cache fails before the database write
Refresh-aheadPopular entries are reloaded in the background just before they expireHot data that must never be slowExtra load for entries nobody will read again

On a write, remove the cache entry rather than updating it. If two requests update the same record at the same time, they may update the cache in a different order from the database, and the cache keeps the wrong value. A removal is always safe: the next read loads whatever the database holds.

In-memory caching with IMemoryCache

IMemoryCache keeps objects inside the process. It is the fastest option, with no serialisation and no network call, but each instance has its own copy, and everything is lost on restart. Always give it a size limit, or a growing cache becomes a memory leak (lesson 2):

C#
builder.Services.AddMemoryCache(o => o.SizeLimit = 10_000);   // total "size units"; you decide what one unit means

public sealed class ProcedureCodeLookup(IMemoryCache cache, IProcedureRepository repository)
{
    public Task<ProcedureCode?> GetAsync(string code, CancellationToken ct) =>
        cache.GetOrCreateAsync($"proc:{code}", entry =>
        {
            entry.Size = 1;                                                // required when SizeLimit is set
            entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1);
            entry.SlidingExpiration = TimeSpan.FromMinutes(10);            // drop it sooner if nobody reads it
            return repository.GetAsync(code, ct);
        });
}

Two kinds of expiry:

  • Absolute expiry: the entry is removed at a fixed time after it was added, however often it is read. This limits how stale it can get.
  • Sliding expiry: the entry is removed if nobody reads it for a period. This keeps hot data and drops cold data. Always combine it with an absolute expiry, or a popular entry is never refreshed.

When memory runs low or the size limit is reached, the cache evicts (removes) entries, starting with low-priority and expired ones. Do not use IMemoryCache for data that all instances must agree on, such as "this claim is locked": instance A may have removed an entry that instance B still serves.

HybridCache: memory and Redis together

HybridCache (package Microsoft.Extensions.Caching.Hybrid, .NET 9+) combines both levels behind one API. It checks local memory first, then Redis, and only then calls your loader. It also solves two problems you would otherwise code by hand: it prevents a stampede (see below) by running only one loader per key at a time, and it supports tags, so you can remove a group of entries together.

C#
builder.Services.AddStackExchangeRedisCache(o => o.Configuration = redisConnection);   // becomes the L2 store
builder.Services.AddHybridCache(o => o.DefaultEntryOptions = new HybridCacheEntryOptions
{
    Expiration = TimeSpan.FromMinutes(10),          // in Redis
    LocalCacheExpiration = TimeSpan.FromMinutes(1)  // in each instance's memory: short, so instances agree quickly
});

public sealed class PayerService(HybridCache cache, IPayerRepository repository)
{
    public ValueTask<Payer?> GetAsync(string code, CancellationToken ct) =>
        cache.GetOrCreateAsync(
            $"payer:{code}",
            async cancel => await repository.GetAsync(code, cancel),   // runs once per key, even under load
            tags: ["payers"],
            cancellationToken: ct);

    public async Task UpdateAsync(Payer payer, CancellationToken ct)
    {
        await repository.UpdateAsync(payer, ct);
        await cache.RemoveAsync($"payer:{payer.Code}", ct);
    }

    public ValueTask RefreshAllAsync(CancellationToken ct) => cache.RemoveByTagAsync("payers", ct);
}

Keep the local expiry short. Removing an entry clears it in Redis and in the current instance, but other instances may serve their local copy until it expires. For new code on .NET 9 or later, HybridCache is usually the best default.

Invalidation: keeping the cache correct

Removing stale data at the right time is the hard part of caching. The main techniques:

  • Time to live (TTL): every entry expires after a set time. This is the safety net. Always set one, even when you also remove entries on writes, so a missed removal heals itself.
  • Remove on write: after the database write succeeds, remove the entry (cache-aside). Do it after the commit, never before, or a read in between can put the old value back.
  • Events: when the data changes, publish an event (Redis pub/sub or Service Bus) that every instance listens to, and each removes its local copy. This keeps in-memory caches across instances in step.
  • Versioned keys: put a version in the key, such as payers:v42:{code}. To invalidate everything at once, increase the version; old entries are never read again and simply expire.
  • Tags: group related entries under a tag and remove them together (HybridCache and output caching support this).

Even with care, a reader can briefly see old data. Request A misses the cache and reads the old row; request B updates the row and removes the entry; request A then stores the old row in the cache. The TTL limits how long this lasts. Where it matters, remove the entry a second time a few seconds after the write (a delayed double delete), or keep the TTL short.

Cache stampede and other failure modes

A cache stampede (also called a thundering herd or dogpile) happens when a popular entry expires and many requests miss at the same moment. They all query the database at once for the same value, and the database can fall over just when the cache was supposed to protect it. Ways to prevent it:

  • Single flight: let only one request per key load the value while the others wait for its result. HybridCache does this for you.
  • Refresh-ahead: reload hot entries in the background shortly before they expire, so they never expire under load.
  • Serve stale while refreshing: keep returning the old value for a short grace period while one request loads the new one.
  • Add jitter to expiry times (a small random extra time), so entries created together do not all expire together.

Without HybridCache, you can coalesce loads inside one instance by sharing one task per key (the same Lazy<T> idea as the ConcurrentDictionary trap in lesson 1):

C#
private readonly ConcurrentDictionary<string, Lazy<Task<Payer?>>> _loading = new();

private async Task<Payer?> LoadOnceAsync(string code, CancellationToken ct)
{
    var lazy = _loading.GetOrAdd(code, c => new Lazy<Task<Payer?>>(() => LoadAndCacheAsync(c, ct)));
    try
    {
        return await lazy.Value;                         // every caller for this key awaits the same task
    }
    finally
    {
        _loading.TryRemove(new KeyValuePair<string, Lazy<Task<Payer?>>>(code, lazy));
    }
}

// Jitter: spread expiry times over an extra 0-60 seconds.
var ttl = TimeSpan.FromMinutes(10) + TimeSpan.FromSeconds(Random.Shared.Next(0, 60));

This coalesces loads within one instance only. With 10 instances, up to 10 loads can still run at once, which is usually acceptable. If not, take a short distributed lock (lesson 5) around the load.

Other failure modes to plan for:

ProblemWhat happensFix
Cache avalancheMany entries expire at the same time, or the cache restarts empty, and the database takes all the loadJitter on expiry times; warm the cache at start-up; rate-limit loads
Cache penetrationRequests for keys that do not exist always miss and always hit the database (sometimes on purpose, by an attacker)Cache "not found" results for a short time; validate IDs before looking them up
Hot keyOne key gets so much traffic that one Redis node becomes the bottleneckAdd a short in-memory (L1) cache in front; split the value across several keys
Cache downRedis is unreachableTreat the cache as optional: catch errors, fall back to the database, and use short timeouts so a slow cache does not slow every request

Redis in practice

  • Key names: use a clear prefix and structure, such as claims-api:payer:{code}. It keeps services apart and makes keys easy to find and delete.
  • Serialisation: System.Text.Json is fine for most data; MessagePack or Protobuf produce smaller values when size matters. Cache DTOs, not EF Core entities.
  • Value size: keep values small (ideally a few KB). Large values slow down Redis for everyone, because it handles one command at a time.
  • Memory limit: configure maxmemory and an eviction policy such as allkeys-lru (remove the least recently used keys first), so Redis evicts old entries instead of rejecting writes when full.
  • Connections: create one ConnectionMultiplexer for the whole app (a singleton) and reuse it. It is thread-safe and designed to be shared.
  • In Azure: use Azure Managed Redis or Azure Cache for Redis, with private endpoints and Microsoft Entra ID authentication instead of access keys.

HTTP and output caching

The cheapest request is one that never reaches your server. HTTP caching headers tell browsers, proxies and CDNs what they may keep:

HeaderMeaning
Cache-Control: public, max-age=300Anyone (browser, CDN) may cache this for 5 minutes
Cache-Control: private, max-age=60Only the user's own browser may cache it, for 1 minute
Cache-Control: no-cacheCaches may store it, but must check with the server (using ETag) before every use
Cache-Control: no-storeNever store it anywhere. Use this for patient data and other sensitive responses
ETag / If-None-MatchLets a cache check cheaply whether its copy is still current (304 Not Modified, lesson 4)

Output caching (.NET 7+) stores whole responses on the server, so repeated requests skip the endpoint entirely. Tags let you evict them when the data changes:

C#
builder.Services.AddOutputCache(o =>
    o.AddPolicy("payers", b => b.Expire(TimeSpan.FromMinutes(5)).Tag("payers")));

app.UseOutputCache();
app.MapGet("/api/v1/payers", ListPayers).CacheOutput("payers");

// After a payer changes:
await outputCacheStore.EvictByTagAsync("payers", ct);

By default, output caching only caches anonymous GET and HEAD requests that return 200, and it does not cache responses that set cookies or require authentication. Keep it that way for any data that depends on who is asking, or one user may receive another user's data.

Check your understanding

QuestionShort answer
What is cache-aside?The app reads the cache, loads from the database on a miss and stores the result, and removes the entry after a write.
Why remove a cache entry on write instead of updating it?Concurrent updates can reach the cache in the wrong order; a removal is always safe because the next read loads the current value.
IMemoryCache or Redis?IMemoryCache is fastest but per instance and lost on restart; Redis is shared by all instances and survives restarts. HybridCache combines both.
Absolute or sliding expiry?Absolute limits staleness; sliding drops entries nobody reads. Use sliding only together with an absolute limit.
Why always set a TTL?It is the safety net: if a removal is missed, the stale entry still disappears on its own.
What is a cache stampede?Many requests miss the same expired key at once and all hit the database. Prevent it with single-flight loading, refresh-ahead or serving stale data briefly.
What is jitter for?A small random extra expiry time, so entries created together do not all expire together.
What is cache penetration?Repeated requests for keys that do not exist; fix it by caching "not found" briefly and validating input.
What should happen when Redis is down?Fall back to the database with short timeouts; the cache must be optional.
Write-through or write-behind?Write-through writes both together (safe, slower writes); write-behind writes the cache first and the database later (fast, but can lose data).
Which Cache-Control value for patient data?no-store: never store it in any cache.

Summary

  • Decide how stale each kind of data may be before you cache it.
  • Use cache-aside by default: read, load on a miss, and remove on write after the database commit.
  • Always set a TTL, add jitter, and cap in-memory caches with a size limit.
  • Prefer HybridCache on .NET 9+: memory plus Redis, one loader per key, and tag-based removal.
  • Protect the database from stampedes, avalanches and penetration, and treat the cache as optional when it fails.
  • Use one shared ConnectionMultiplexer, clear key prefixes, small values and an eviction policy in Redis.
  • Use HTTP caching and output caching for shared responses, and no-store for sensitive ones.

The next lesson covers resilience: what to do when the database, Redis or another service is slow or down. It covers timeouts, retries with backoff, circuit breakers, bulkheads, health checks and graceful degradation.

Found this useful? Share it: LinkedIn X

Comments