skip to content

Caching Stack

The caches ASP.NET Core ships: IMemoryCache, IDistributedCache stores, HybridCache over both, and output and response caching of whole responses. Interviewers probe which layer fits which data.

part ofASP.NET Coreread it →
on this pageshow

explore

questions

26

In ASP.NET Core, what is IDistributedCache, why does it deal in byte arrays, and how do you back it with Redis?

level: juniorimportance: must knowfreq 62%

answer

  1. one copy outside every web process
  2. Task<byte[]?> GetAsync, null on a miss
  3. SetStringAsync is a UTF-8 extension
  4. the caller serializes; no typed API
  5. Microsoft.Extensions.Caching.StackExchangeRedis

basics

~20 s

IDistributedCache is ASP.NET Core's abstraction over a cache that lives outside the web process, so every instance reads the same entry. It stores byte arrays under string keys; you serialize objects yourself and register Redis with AddStackExchangeRedisCache.

solid answer

~40 s

`IDistributedCache` (namespace `Microsoft.Extensions.Caching.Distributed`) has four operations, each sync and async: `GetAsync` returns `Task<byte[]?>` (null on a miss), `SetAsync` takes a `byte[]` and `DistributedCacheEntryOptions`, `RefreshAsync` resets a sliding expiration, and `RemoveAsync` deletes. Values are bytes because they leave the process: the store is Redis, SQL Server or another server, so there is no object reference to hand back. You serialize, usually to JSON with `JsonSerializer.SerializeToUtf8Bytes`, or use the `SetStringAsync` / `GetStringAsync` extensions, which only convert UTF-8 text. For Redis, add the `Microsoft.Extensions.Caching.StackExchangeRedis` package and call `builder.Services.AddStackExchangeRedisCache(o => { o.Configuration = ...; o.InstanceName = "shop:"; })`. All four instances behind the load balancer then read and write one shared copy of each entry, and it survives an app restart.

code

csharp · 22 lines
csharp
using System.Text.Json;
using Microsoft.Extensions.Caching.Distributed;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddStackExchangeRedisCache(o =>
{
    o.Configuration = builder.Configuration.GetConnectionString("redis");
    o.InstanceName = "shop:";
});
var app = builder.Build();

app.MapGet("/baskets/{id:guid}", async (Guid id, IDistributedCache cache, CancellationToken ct) =>
{
    byte[]? bytes = await cache.GetAsync($"basket:{id}", ct);
    return bytes is null
        ? Results.NotFound()
        : Results.Ok(JsonSerializer.Deserialize<BasketSnapshot>(bytes));
});

app.Run();

public sealed record BasketSnapshot(Guid Id, string[] Skus, decimal Total);

go deeper

for a junior

Recall that IDistributedCache stores byte arrays under string keys outside the process, that you serialize values yourself, and that AddStackExchangeRedisCache registers a Redis-backed implementation.

for a middle

Explain the four operations and the string extensions, why bytes are the honest contract for an out-of-process store, and how a shared store fixes per-instance misses behind a load balancer.

for a senior

Treat the serialized shape as a stored contract that outlives a deploy, version keys when it changes, and plan for a store outage turning cache calls into exceptions.

for a principal

Decide when a network hop per read is worth one shared copy, and when a two-tier design that adds a local cache in front pays for its extra staleness.

## What the abstraction is for `IMemoryCache` keeps objects inside one process, so a fashion retailer running four instances of its storefront API behind a load balancer has four separate caches. A shopper whose basket snapshot was cached by instance 1 and whose next request lands on instance 3 gets a miss there. `IDistributedCache` solves exactly that: it is the ASP.NET Core abstraction over a cache that lives **outside** the web processes, so every instance sees the same entry, and the entry survives a deploy or a crash of any one instance. ## The interface The interface is deliberately small: | Member | Returns | Purpose | |---|---|---| | `Get(key)` / `GetAsync(key, token)` | `byte[]?` / `Task<byte[]?>` | the value, or `null` on a miss | | `Set(key, value, options)` / `SetAsync(...)` | `void` / `Task` | store bytes with expiration options | | `Refresh(key)` / `RefreshAsync(...)` | `void` / `Task` | reset a sliding expiration without reading the value | | `Remove(key)` / `RemoveAsync(...)` | `void` / `Task` | delete the entry | The static `DistributedCacheExtensions` class adds `SetAsync(key, value)` without options, and `SetString` / `SetStringAsync` / `GetString` / `GetStringAsync`, which encode and decode text as UTF-8 and nothing more. There is **no** generic `GetAsync<T>`, no `GetOrCreateAsync` and no tagging. ## Why bytes A value in Redis or a SQL Server table is a sequence of bytes on another machine. There is no object reference to return, so the abstraction does not pretend there is one: the caller chooses the serialization format and owns its compatibility. A basket snapshot round-trip looks like this: ```csharp public sealed class BasketSnapshots(IDistributedCache cache) { public async Task<BasketSnapshot?> GetAsync(Guid basketId, CancellationToken ct) { byte[]? bytes = await cache.GetAsync($"basket:{basketId}", ct); return bytes is null ? null : JsonSerializer.Deserialize<BasketSnapshot>(bytes); } public Task SaveAsync(BasketSnapshot snapshot, CancellationToken ct) => cache.SetAsync($"basket:{snapshot.Id}", JsonSerializer.SerializeToUtf8Bytes(snapshot), new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(30) }, ct); } ``` Two consequences follow: - **Every read deserializes a fresh object.** Mutating it cannot corrupt the cached copy, unlike an `IMemoryCache` reference. - **The type's shape is now a stored contract.** Rename a property of `BasketSnapshot` and entries written by the previous deploy deserialize with that property missing. Put a version in the key (`basket:v2:{id}`) when the shape changes incompatibly. ## Backing it with Redis ```csharp builder.Services.AddStackExchangeRedisCache(options => { options.Configuration = builder.Configuration.GetConnectionString("redis"); options.InstanceName = "shop:"; }); ``` - **Package:** `Microsoft.Extensions.Caching.StackExchangeRedis`, which registers `IDistributedCache` as a singleton. - **Connection:** `Configuration` takes a StackExchange.Redis connection string; `ConfigurationOptions` takes a parsed object and wins when both are set; `ConnectionMultiplexerFactory` lets you reuse a multiplexer the app already owns. - **`InstanceName`:** a prefix written in front of every key so several apps can share one Redis. Other stores plug in the same way: `AddDistributedSqlServerCache` for a SQL Server table and `AddDistributedMemoryCache` for an in-process stand-in used in development and tests. Application code depends only on `IDistributedCache`, so switching stores is a `Program.cs` change. ## What it does not do 1. **Coordinate concurrent misses.** Two instances that miss the same key both load and both write. 2. **Hide outages.** A Redis call that fails throws; nothing falls back to a miss for you. 3. **Keep a local copy.** Every read is a network round trip. Those gaps are why ASP.NET Core also offers `HybridCache`, which layers an in-process tier and miss coordination on top of a registered `IDistributedCache`. ## Mistakes interviewers listen for - **Treating it like `IMemoryCache`.** Expecting a stored object back, or mutating a deserialized basket and assuming the cache changed, shows the out-of-process model has not landed. - **Assuming `SetStringAsync` serializes objects.** It only encodes a `string` as UTF-8; turning a `BasketSnapshot` into that string is still the caller's job. - **Omitting expiration options.** The overloads without `DistributedCacheEntryOptions` leave the choice to the store, and on Redis that means no expiry at all. - **Using `AddDistributedMemoryCache` in a farm.** It implements the interface inside each process, so four instances still hold four caches. A strong answer names the interface, says why it is byte-based, shows one serialize-and-store round trip with explicit options, and registers Redis with a prefixed `InstanceName`.

  • Why does IDistributedCache have no GetOrCreateAsync?
    The interface stops at get, set, refresh and remove, and the extensions only add byte and UTF-8 string overloads. A get-or-create needs a serializer and a policy for concurrent misses, which the abstraction leaves to the caller. `HybridCache` is the library that adds `GetOrCreateAsync` on top of a registered `IDistributedCache`.
  • What happens to cached basket snapshots when a deploy renames a property of BasketSnapshot?
    Entries written by the old build still hold the old JSON. `System.Text.Json` ignores unknown properties by default and leaves the renamed one at its default, so the new build silently reads incomplete baskets until they expire. Change the key prefix, for example `basket:v2:`, when the stored shape changes.
  • Is IDistributedCache registered as scoped, transient or singleton?
    All three built-in stores register it as a singleton: `AddStackExchangeRedisCache` and `AddDistributedSqlServerCache` with `Add`, `AddDistributedMemoryCache` with `TryAdd`. The Redis implementation holds one lazily created connection for the whole app, so it is safe to inject anywhere.

  • IDistributedCache returns the same object reference that was stored, like IMemoryCache
  • IDistributedCache has a generic GetAsync<T> that deserializes values for you
  • SetStringAsync serializes any object to JSON before storing it
  • With AddStackExchangeRedisCache each instance keeps its own copy and Redis syncs them
  • A Redis outage makes GetAsync return null so callers fall back automatically
open as a page

In ASP.NET Core 10, what is HybridCache, how do you register it with AddHybridCache, and what does GetOrCreateAsync do on a miss?

level: juniorimportance: must knowfreq 52%

basics

~10 s

HybridCache is a two-tier cache: an in-process IMemoryCache in front of any registered IDistributedCache. AddHybridCache registers it; GetOrCreateAsync checks memory, then the distributed store, then runs your factory once and writes both tiers.

open as a page

In ASP.NET Core's IMemoryCache, how do absolute and sliding expiration differ, and why would you set both on one entry?

level: juniorimportance: must knowfreq 58%

basics

~20 s

Absolute expiration removes an entry at a fixed time no matter how often it is read; sliding expiration removes it after a period with no reads. Sliding alone can keep a hot entry forever, so an absolute cap bounds staleness.

open as a page

In ASP.NET Core, how do you register IMemoryCache and read or write an entry with TryGetValue and Set, and where does that data live?

level: juniorimportance: must knowfreq 64%

basics

~20 s

Call builder.Services.AddMemoryCache(), inject IMemoryCache, read with TryGetValue (a bool plus an out value) and write with Set. Entries are object references in that server process's heap: every instance keeps its own copy, lost on restart.

open as a page

In ASP.NET Core, what does output caching do, and why do AddOutputCache and UseOutputCache on their own cache no responses?

level: juniorimportance: must knowfreq 56%

basics

~20 s

Output caching stores an endpoint's whole response (status, headers and body) on the server and replays it without running the endpoint. AddOutputCache and UseOutputCache only make it available; nothing is cached until a policy applies to the request.

open as a page

In an ASP.NET Core MVC controller, what headers does [ResponseCache(Duration = 300)] write, and does it cache anything on the server by itself?

level: juniorimportance: must knowfreq 50%

basics

~20 s

[ResponseCache(Duration = 300)] writes Cache-Control: public,max-age=300 (Location defaults to Any) and nothing else; it caches nothing on the server. Browsers and shared caches act on that header; a server cache needs UseResponseCaching or output caching.

open as a page

In ASP.NET Core output caching, which requests and responses does the default policy refuse to cache, and how long does it keep the rest?

level: middleimportance: must knowfreq 48%

basics

~10 s

The default policy caches only anonymous GET or HEAD requests whose response is a 200 with no Set-Cookie header, varies by every query-string key, and keeps each entry 60 seconds (OutputCacheOptions.DefaultExpirationTimeSpan).

open as a page

How does ASP.NET Core's response caching middleware (UseResponseCaching) differ from output caching, and why is output caching usually the better server-side cache today?

level: middleimportance: must knowfreq 45%

basics

~20 s

The response caching middleware stores only what the response's Cache-Control headers allow and lets request headers bypass it; output caching (ASP.NET Core 7+) follows server-side policies, ignores client no-cache, supports pluggable stores, tag eviction and request locking.

open as a page

In ASP.NET Core, what does AddDistributedMemoryCache register, and why can't four load-balanced instances share basket snapshots through it?

level: juniorimportance: should knowfreq 38%

basics

~20 s

AddDistributedMemoryCache registers MemoryDistributedCache, an IDistributedCache that keeps byte arrays in a MemoryCache inside the process. Each instance therefore has its own store, so a basket written on one instance is a miss on the others.

open as a page

With ASP.NET Core's IDistributedCache, what do DistributedCacheEntryOptions and RefreshAsync control, and what expiration does an entry written without options get?

level: middleimportance: should knowfreq 46%

basics

~20 s

DistributedCacheEntryOptions sets an absolute deadline, a relative one, or a sliding window; reads and RefreshAsync reset the sliding window. Without options, Redis sets no expiry at all, while the SQL Server store applies a 20-minute sliding default.

open as a page

In ASP.NET Core's HybridCache, how do Expiration and LocalCacheExpiration differ, and why can RemoveAsync leave other instances serving the old value?

level: middleimportance: should knowfreq 38%

basics

~10 s

Expiration is the overall and distributed-tier lifetime; LocalCacheExpiration is how long each process keeps its memory copy, defaulting to Expiration and never longer. RemoveAsync clears only the caller's memory and the shared store.

open as a page

In ASP.NET Core's IMemoryCache, what do RegisterPostEvictionCallback and AddExpirationToken do, and when does an eviction callback actually run?

level: middleimportance: should knowfreq 32%

basics

~20 s

RegisterPostEvictionCallback runs a delegate on the thread pool after an entry leaves the cache, with an EvictionReason. AddExpirationToken ties the entry to an IChangeToken, such as a CancellationChangeToken, whose firing queues its eviction at once.

open as a page

In ASP.NET Core output caching, how do AddBasePolicy and AddPolicy differ, and in what order are base and endpoint policies applied?

level: middleimportance: should knowfreq 40%

basics

~20 s

AddBasePolicy applies a policy to every request the middleware sees; AddPolicy registers a named policy that only endpoints referencing it through CacheOutput("name") or [OutputCache(PolicyName = ...)] use. Base policies run first and endpoint policies after, so the endpoint's settings win.

open as a page

How does ASP.NET Core output caching build its cache key by default, and what do SetVaryByQuery and VaryByValue change about it?

level: middleimportance: should knowfreq 37%

basics

~20 s

The key joins method, scheme, host and path (case-insensitive) plus, by default, every query-string key and value. SetVaryByQuery narrows the query part to named keys; VaryByValue adds a key/value pair computed from HttpContext, such as the UI culture.

open as a page

In ASP.NET Core MVC, how do CacheProfiles work with [ResponseCache(CacheProfileName = ...)], and which value wins when the attribute and profile disagree?

level: middleimportance: should knowfreq 24%

basics

~10 s

CacheProfiles is a named dictionary of CacheProfile settings on MvcOptions; [ResponseCache(CacheProfileName = "x")] uses it as defaults, and any property set on the attribute overrides the profile's. An unknown profile name throws InvalidOperationException.

open as a page

What must a request and its response satisfy for ASP.NET Core's response caching middleware to store and later serve them, and which of those checks can surprise you?

level: middleimportance: should knowfreq 32%

basics

~20 s

The request must be a GET or HEAD without an Authorization header; the response must be a fresh 200 marked Cache-Control: public, without private, no-store, no-cache, Set-Cookie or Vary: *. Request no-cache or no-store directives skip lookup or storage.

open as a page

With AddStackExchangeRedisCache in ASP.NET Core, what does InstanceName do to keys, how is an entry stored, and what happens when Redis is unreachable?

level: seniorimportance: should knowfreq 36%

basics

~20 s

InstanceName is prepended to every key exactly as written, with no separator. Each entry is a Redis hash holding absexp, sldexp and data plus a key TTL. An unreachable Redis does not stop startup, but cache calls then throw.

open as a page

Right after a deploy, thousands of concurrent requests call HybridCache.GetOrCreateAsync for one catalogue key; how many times does the factory run, and what limits that?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Once per key per instance at a time: the first caller starts the load and concurrent callers for that key join it. Across N instances it can still run N times, fewer when the distributed tier holds the value.

open as a page

In ASP.NET Core's HybridCache, what do tags on GetOrCreateAsync and RemoveByTagAsync do, and why does RemoveByTagAsync not delete any cached values?

level: seniorimportance: should knowfreq 30%

basics

~20 s

Tags group entries so one call can invalidate them together. RemoveByTagAsync records an invalidation time for the tag instead of deleting values; later reads treat entries created before it as misses. The reserved wildcard tag, a lone asterisk, invalidates everything.

open as a page

When a cached exchange rate expires and 200 concurrent requests call IMemoryCache.GetOrCreateAsync for it in ASP.NET Core, how often does the factory run, and why?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Possibly up to 200 times. GetOrCreateAsync is an extension that calls TryGetValue, then awaits the factory and commits the entry, with no lock per key; every caller that misses before the first commit runs its own factory.

open as a page

A remittance service caps its exchange-rate IMemoryCache with SizeLimit; what must every entry then carry, and what happens when the limit is reached?

level: seniorimportance: should knowfreq 34%

basics

~10 s

Once MemoryCacheOptions.SizeLimit is set, every entry needs a Size or committing it throws InvalidOperationException. A write that would exceed the limit is not stored, and a background compaction evicts expired, then low-priority, least-recently-used entries.

open as a page

In ASP.NET Core output caching, what does resource locking do when a popular entry expires under load, and when would you call SetLocking(false)?

level: seniorimportance: should knowfreq 27%

basics

~20 s

Locking, on by default, lets only one request per cache key run the endpoint on a miss; concurrent requests for that key wait and are served the response it produced. SetLocking(false) lets every miss run the endpoint independently.

open as a page

On a festival site whose event listings use ASP.NET Core output caching, how do you evict them by tag when an organiser edits an event, and what changes with three instances?

level: seniorimportance: should knowfreq 35%

basics

~10 s

Tag the cached listings (Tag("events") or [OutputCache(Tags = ...)]), inject IOutputCacheStore into the edit endpoint and call EvictByTagAsync("events"). The default store is per process, so with three instances register AddStackExchangeRedisOutputCache to share one store.

open as a page

A bus timetable API behind ASP.NET Core's response caching middleware returns route 12's timetable for route 31; why, and how does VaryByQueryKeys fix it?

level: seniorimportance: should knowfreq 26%

basics

~20 s

The middleware's key is method, scheme, host and path only, so /api/timetable?route=12 and ?route=31 share one entry. VaryByQueryKeys = new[] { "route" } adds that query key to the key; it requires UseResponseCaching, or the filter throws.

open as a page

With AddDistributedSqlServerCache in ASP.NET Core, how do you create the cache table, and how are expired rows hidden from reads and cleaned up?

level: middleimportance: nice to knowfreq 22%

basics

~20 s

Create the table with the dotnet sql-cache tool, then set ConnectionString, SchemaName and TableName. Reads filter on ExpiresAtTime, so expired rows are never returned; a delete of expired rows runs at most every ExpiredItemsDeletionInterval, 30 minutes by default.

open as a page

In ASP.NET Core's HybridCache, how are values serialized, how do you plug in an IHybridCacheSerializer<T>, and when do callers share one cached instance?

level: middleimportance: nice to knowfreq 18%

basics

~10 s

HybridCache handles string and byte[] itself and serializes other types with System.Text.Json unless you register an IHybridCacheSerializer<T> or a serializer factory. Mutable types are deserialized for every caller; sealed types marked [ImmutableObject(true)] are shared.

open as a page