Phase 25 of 30 · Topic 25.3

High-Throughput Output Caching (`AddOutputCache`) in .NET 8/9

1Concept

Output Caching caches raw HTTP response payloads and headers at the server level, serving identical subsequent requests directly from RAM without activating route handlers or querying databases.

2Architecture Diagram

Client HTTP GET /products
       │
[ Output Cache Middleware ]
  ├── Cache Hit?  ──> Returns cached HTTP byte payload immediately (0.1ms latency!)
  └── Cache Miss? ──> Executes handler, caches response, and returns to client

3Code Example

C# 13 & .NET 9
using System;

public class OutputCacheConcept
{
    public static void Main()
    {
        Console.WriteLine("Output Caching configuration:");
        Console.WriteLine("builder.Services.AddOutputCache(options => {");
        Console.WriteLine("    options.AddBasePolicy(b => b.Expire(TimeSpan.FromSeconds(60)));");
        Console.WriteLine("});");
    }
}

4Expected Output

Output Caching configuration:
builder.Services.AddOutputCache(options => {
    options.AddBasePolicy(b => b.Expire(TimeSpan.FromSeconds(60)));
});

5Key Takeaways

  • Supports cache tag eviction (`EvictByTagAsync("products")`) for targeted invalidation.
  • Locked against Cache Stampedes (multiple concurrent requests wait for a single backend fetch).
  • Massive RPS multiplier for public read endpoints.