---
title: "Speed Up Your ASP.NET Core API with Redis Caching"  
description: "Add Redis caching to an ASP.NET Core Web API. Run Redis locally, use IDistributedCache and HybridCache, choose expiry times, build cache keys, invalid"  
author: "Shreyas Joshi"  
published: 2026-11-26  
canonical: https://www.mindstick.com/blog/307176/speed-up-your-asp-net-core-api-with-redis-caching  
category: "Performance"  
tags: ["performance", "asp.net core", ".net", "Redis"]  
reading_time: 5 minutes  

---

# Speed Up Your ASP.NET Core API with Redis Caching

Your product catalogue API runs the same database query thousands of times a day, and the answer barely changes. Every one of those requests waits for SQL Server, uses a connection and adds load to the database.

Caching stores the result once and serves it from memory for the next requests. Redis, a very fast in-memory data store, is the most common choice for caching in ASP.NET Core, especially when you run more than one server.

When caching helps (and when it doesn't)

Good candidates:

- data read far more often than it changes: product lists, categories, settings, price lists, city or state lookups\
- expensive calculations: dashboard summaries, reports, search results\
- slow external API responses: exchange rates, shipping quotes

Poor candidates:

- data that must always be exactly current: account balances, stock at checkout, payment status\
- data unique to each request that's never reused\
- anything that's already fast enough

Caching adds complexity, mainly around stale data. Use it where the speed gain is worth that.

In-memory cache vs Redis

ASP.NET Core has a built-in in-memory cache (IMemoryCache). It's extremely fast, but each server has its own copy. With two or more servers behind a load balancer, they hold different data, and updating one doesn't update the others. It's also lost on every app restart.

Redis is a separate service that all your servers share. Data survives app restarts and deployments, and invalidating a key affects every server at once.

A popular approach in modern .NET is to combine both, which is exactly what HybridCache does, as we'll see.

Step 1: Run Redis

For development, Docker is the quickest:

docker run -d --name redis -p 6379:6379 redis:7

In production, use a managed service such as Azure Managed Redis / Azure Cache for Redis, Amazon ElastiCache, or a properly secured Redis server. Redis can also be run on Windows through WSL or containers; native Windows ports are not officially maintained.

Step 2: Add the packages

dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis\
dotnet add package Microsoft.Extensions.Caching.Hybrid

Step 3: Configure Redis and HybridCache

appsettings.json:

{\
"ConnectionStrings": {\
"Redis": "localhost:6379"\
}\
}

In production, the connection string includes a password and TLS, for example myredis.example.com:6380,password=...,ssl=True,abortConnect=False. Keep it in secrets, not in the repository.

When a Redis cache is registered, HybridCache automatically uses it as its second-level (shared) cache, with an in-memory first level in front of it.

What HybridCache handles for you

- Get-or-create in one call: if the key exists, return it; if not, run the factory, store the result and return it.\
- Stampede protection: if 500 requests ask for the same missing key at the same moment, only one runs the database query; the rest wait for its result. Without this, a popular key expiring can flood your database.\
- Serialisation of objects to and from Redis.\
- Two cache levels: a very fast local memory copy plus the shared Redis copy.\
- Tags: group related entries so you can remove them together.

Step 5: Use it in an endpoint

app.MapGet("/products/{id:int}", async (int id, ProductService svc, CancellationToken ct) =>\
await svc.GetProductAsync(id, ct) is { } p ? Results.Ok(p) : Results.NotFound());

The first request queries the database. Following requests within the expiry window come from cache, usually in a fraction of the time.

Using IDistributedCache directly

IDistributedCache also works with Redis on older .NET versions, but you handle serialisation and stampede protection yourself. HybridCache is the more convenient default in current .NET.

Choosing expiry times

There's no universal answer. Ask: how stale can this data be without causing a problem?

- Site settings, categories, state/city lists — Reasonable expiry: Hours\
- Product details and prices — Reasonable expiry: Minutes, plus invalidation on update\
- Dashboard summaries — Reasonable expiry: 1–15 minutes\
- Exchange rates from an external API — Reasonable expiry: Matches how often the source updates\
- Stock levels shown on listing pages — Reasonable expiry: Seconds to a minute (check real stock at checkout)

Always set an expiry, even if you also invalidate on updates. It's a safety net if an invalidation is missed.

Cache key tips

- Include everything that changes the result: IDs, filters, page numbers, language, and the tenant or user if data differs per user.\
- Use a clear, consistent pattern: product:42, category:kitchen:page:2.\
- Use an app prefix (InstanceName) so several apps can share one Redis safely.\
- Never mix users' data in one key if the data is personal. Caching one customer's account page under a shared key is a serious privacy bug.

Common mistakes

- Forgetting invalidation: prices updated in the admin panel but customers still see old ones for ten minutes.\
- Caching null for too long: if a product doesn't exist yet and you cache "not found" for hours, it stays invisible after it's added. Use short expiries for empty results, or don't cache them.\
- Caching huge objects: large values are slow to serialise and transfer. Cache what the endpoint actually returns, not entire object graphs.\
- No timeouts or fallback: if Redis is down, your API should still work, just slower. HybridCache's local layer and the database fallback help; monitor Redis health anyway.\
- Exposing Redis to the internet without a password and TLS. Unprotected Redis servers are scanned for and abused constantly.

Measure the result

Before and after adding caching, measure response times (p95 and p99) and database load. Track the cache hit rate. If most requests are misses, your keys or expiry times need rethinking.

Final thoughts

Redis caching can take load off your database and cut response times dramatically for read-heavy endpoints. In modern ASP.NET Core, HybridCache backed by Redis gives you a fast local layer, a shared distributed layer and built-in stampede protection with very little code.

Cache data that's read often and changes rarely, choose expiry times based on how much staleness you can tolerate, invalidate on updates, and keep personal data out of shared keys.

---

Original Source: https://www.mindstick.com/blog/307176/speed-up-your-asp-net-core-api-with-redis-caching

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
