skip to content

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%

answer

  1. headers decide vs server policies decide
  2. browser refresh bypasses response caching
  3. memory only, no pluggable store
  4. no tag eviction, no request locking
  5. output caching since ASP.NET Core 7

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.

solid answer

~50 s

The response caching middleware is an HTTP cache inside the app: it stores a response only if it is marked `public` (and not `private`, `no-store` or `no-cache`), and it obeys the request too, so a browser refresh sending `Cache-Control: no-cache` or `max-age=0` skips the stored copy. Its store is a private in-memory cache with no extension point, it cannot evict entries except by expiry, it has no request coalescing, and by default its key ignores the query string. Output caching, added in ASP.NET Core 7, is configured by server-side policies: it needs no `public` header, ignores the client's `no-cache`, varies by every query key by default, plugs into `IOutputCacheStore` (Redis included), evicts by tag and coalesces concurrent misses. In ASP.NET Core 10 both are supported and neither is obsolete; the usual split is `[ResponseCache]` or explicit headers for browsers and CDNs, and output caching for the server.

code

csharp · 12 lines
csharp
builder.Services.AddControllers();
builder.Services.AddOutputCache(o =>
    o.AddPolicy("Timetable", b => b.Expire(TimeSpan.FromMinutes(5)).Tag("timetables")));

var app = builder.Build();
app.UseOutputCache();
app.MapControllers();

[HttpGet("{route}")]
[ResponseCache(Duration = 300)]      // headers for phones and the CDN
[OutputCache(PolicyName = "Timetable")] // server copy, evictable by tag
public Task<Timetable?> Get(string route) => store.FindAsync(route);

go deeper

for a junior

Know that two middleware exist: response caching follows HTTP headers, output caching follows server-side policies, and output caching came in ASP.NET Core 7.

for a middle

Walk the table: client influence, public requirement, query-string keying, store, invalidation and locking, and say why a browser refresh defeats response caching.

for a senior

Explain the practical split of [ResponseCache] headers for downstream caches plus output caching for the server, and why running both server caches together muddles staleness.

for a principal

Choose a caching layer per concern (client, CDN, app) and decide who may force freshness: clients via HTTP semantics, or only the server via policy and eviction.

## Two middleware, two philosophies ASP.NET Core ships two middleware that keep whole responses in server memory: - **Response caching** (`AddResponseCaching` / `UseResponseCaching`, namespace `Microsoft.AspNetCore.ResponseCaching`) has existed since the first releases. It behaves like a small HTTP cache placed in front of your endpoints: the response's `Cache-Control` headers decide what may be stored, and the request's headers can ask it not to serve from storage. - **Output caching** (`AddOutputCache` / `UseOutputCache`, added in ASP.NET Core 7) is a server-side cache configured by policies in code. HTTP headers neither enable nor disable it. Neither is obsolete in ASP.NET Core 10, and both live in the shared framework. The interview question is why new code usually reaches for output caching. ## Side by side | Concern | Response caching middleware | Output caching | |---|---|---| | What enables storage | response `Cache-Control: public` with a lifetime | a policy (`CacheOutput`, `[OutputCache]`, base policy) | | Client influence | request `no-cache`, `max-age=0`, `no-store` change the outcome | ignored, except `only-if-cached` | | Query string in key | not by default; only via `VaryByQueryKeys` | every key by default; narrow with `SetVaryByQuery` | | Store | private in-memory cache, not replaceable | `IOutputCacheStore`; in-memory or the Redis package | | Invalidation | expiry only | expiry plus `EvictByTagAsync` | | Concurrent misses | each runs the endpoint | coalesced per key by default | | Adds `Cache-Control` for clients | no; replays what the endpoint wrote | no; replays what the endpoint wrote | | Size defaults | `SizeLimit` 100 MB, `MaximumBodySize` 64 MB | the same | ## Why the header-driven design disappoints on the server 1. **Browsers turn it off.** A reload sends `Cache-Control: max-age=0` or `no-cache`; the middleware honours the request, so every reload reaches the endpoint. The documentation uses exactly this example to introduce output caching. 2. **It must say `public`.** Storing a response requires announcing it as cacheable by every shared cache on the path, CDNs included. A server-only copy of a `private` response is impossible, while output caching keeps one without touching the headers. 3. **No way to purge.** When the bus network publishes a revised timetable, the only lever is waiting for `max-age` to run out. 4. **One instance, one copy.** The store cannot be swapped for a shared one, so a farm of instances holds separate copies. 5. **Stampedes.** An expiring popular entry sends every concurrent request to the endpoint. ## Where response caching still fits - When you want **one set of HTTP semantics** for every layer: the app, a reverse proxy and browsers all honour the same `Cache-Control`, and clients may legitimately ask for fresh data. - In small single-instance apps whose endpoints already emit correct headers. The attribute is another matter. `[ResponseCache]` remains the convenient way for MVC controllers to **write** `Cache-Control` for browsers and CDNs, and it works whether or not the middleware runs. A common modern setup for the timetable API is therefore: `[ResponseCache(Duration = 300)]` so riders' phones and the CDN keep timetables for five minutes, plus an output cache policy so the app itself rebuilds each timetable at most once per interval and can evict it the moment a revision is published. ## Migrating the timetable API Moving an existing app from response caching to output caching is mostly mechanical: 1. Replace `AddResponseCaching()` / `UseResponseCaching()` with `AddOutputCache(...)` / `UseOutputCache()`. 2. Keep `[ResponseCache(Duration = ...)]` where browsers and the CDN should still cache; it no longer depends on any middleware, except for `VaryByQueryKeys`, which throws once the response caching middleware is gone and must be removed. 3. Add `[OutputCache]` or `CacheOutput(...)` policies, deciding explicitly which query keys matter (output caching varies by all of them by default). 4. Add tags and call `EvictByTagAsync` from the publishing path that loads a revised timetable. 5. Re-test anything signed in: output caching refuses authenticated requests by default, while response caching only looked for an `Authorization` header. ## Mixing them Running both middleware on the same endpoint is legal but rarely useful: two server copies with different lifetimes make staleness hard to reason about. Pick output caching for the server and let headers serve the downstream caches.

  • Does the response caching middleware ever answer 304 Not Modified?
    Yes. When it holds a fresh entry and the request's `If-None-Match` matches the stored `ETag`, or `If-Modified-Since` is satisfied, it returns 304 with the stored validator headers. The overview docs say response caching does not revalidate, but the middleware's source does serve 304 for conditional requests.
  • If you replace UseResponseCaching with UseOutputCache, do browsers stop caching?
    Not necessarily. Browsers and CDNs follow the `Cache-Control` header the endpoint writes, and `[ResponseCache]` keeps writing it without the middleware. Output caching neither adds nor removes such headers, so downstream behaviour only changes if you change the headers.

  • The response caching middleware ignores the browser's Cache-Control request header.
  • Response caching can store entries in Redis by registering IDistributedCache.
  • Output caching only stores responses marked Cache-Control: public.
  • The response caching middleware is obsolete in ASP.NET Core 10.
  • Switching to output caching changes what browsers cache automatically.

Assumes ASP.NET Core 10; output caching was added in ASP.NET Core 7, and the response caching middleware remains supported and not obsolete in 10.