← Back to Blog Performance

Caching in .NET: L1, L2, Distributed, and HTTP Caching Strategies

Saurav Rai

Founder & Lead Architect

· · 12 min read

Overview

When and how to use IMemoryCache, IDistributedCache, Redis, and HTTP response caching in ASP.NET Core.

The Caching Hierarchy

Modern applications use multiple cache levels. L1 (in-process memory) is fastest but limited and not shared across instances. L2 (distributed Redis) is shared and survives restarts. HTTP caching (CDN/browser) offloads the server entirely.

For most CRUD APIs, Redis distributed caching provides the best balance: sub-millisecond reads, shared state across instances, and persistence across deployments.

  • L1 IMemoryCache: microsecond access, process-local
  • L2 IDistributedCache/Redis: millisecond, shared
  • Response cache: HTTP 304 saves full round-trip
  • CDN caching: edge serves requests near users

Redis Cache Patterns

Cache-aside is the most common pattern: check cache first, fetch from DB on miss, populate cache. For high-read low-write data, read-through caching transparently handles this.

Always set appropriate TTLs. Use sliding expiration for user session data and absolute expiration for reference data.

services.AddStackExchangeRedisCache(options =>
    options.Configuration = configuration.GetConnectionString("Redis"));

// Cache-aside pattern
var user = await _cache.GetAsync<User>($"user:{id}");
if (user is null)
{
    user = await _db.Users.FindAsync(id);
    await _cache.SetAsync($"user:{id}", user, TimeSpan.FromMinutes(10));
}
return user;

Key Takeaways

  • Always invalidate cache on write operations
  • Use cache stampede protection (SemaphoreSlim or locking)
  • Redis TTLs prevent stale data without explicit invalidation
  • Monitor cache hit rates—below 80% suggests misconfiguration
  • IMemoryCache has size limits—set them to prevent OOM

Saurav Rai

Founder & Lead Architect, Omni Stack

7+ years building enterprise .NET and cloud applications for clients across Australia, USA, and the Middle East. Passionate about clean architecture, developer experience, and shipping fast.

Performance · 11 min read

Progressive Web Apps with Blazor WASM: Complete Implementation Guide

Service workers, web app manifest, offline support, push notifications, and install prompts for Blazor PWAs.

Read More →
.NET / Blazor · 12 min read

Blazor Auto Render Mode: Server vs WebAssembly Per Component

.NET 8 per-component interactivity: our production guide to choosing Server vs WASM—with benchmarks.

Read More →
.NET / Blazor · 8 min read

Building Real-Time Dashboards with SignalR and Blazor Server

Step-by-step: live-updating dashboards using SignalR Hub groups, Blazor Server, and efficient diff rendering.

Read More →