Article: Distributed Cache in .NET: How to Configure and Use Redis
This repository provides a hands-on guide and reference implementation for integrating Redis as a Distributed Cache layer into a .NET Web API.
The project demonstrates how to achieve near-instantaneous response times by moving the workload of fetching heavy datasets (1,000+ records) from a simulated database to an in-memory cache, highlighting the significant performance gap between a Cache Miss and a Cache Hit.
- Generic Cache Repository Pattern: Implementing
ICacheRepository<T>to decouple business logic from the caching provider. - Double Invalidation Strategy: Maintaining data consistency by clearing both individual keys and collection keys on data updates.
- Performance Benchmarking: Comparison of response times between database fetching and Redis retrieval.
- Serialization: Handling complex objects using JSON serialization for Redis storage.
The application first checks the cache. If the data is missing (Cache Miss), it fetches it from the database and stores it in the cache for future requests.
To prevent "stale data" (showing old info after an update), this project implements a strategy where any write operation (POST, PUT, DELETE) automatically removes:
- The specific item key (e.g.,
user:123) - The collection key (e.g., all_users_list)
This ensures that the next GET request is forced to fetch fresh data from the database.
- .NET 9 (Web API)
- Redis (Distributed Cache)
- StackExchange.Redis (Client library)
- Docker (Local environment)
- .NET 9 SDK
- Docker Desktop
Run the following command to start a local Redis instance:
docker run --name my-redis -d -p 6379:6379 redisNavigate to the project directory and execute:
dotnet run --project src/RedisCache.Api/RedisCache.Api.csprojThe API will be available (typically) at: http://localhost:5000 (check your launchSettings.json).
This repository is part of my technical writing and learning notes.
If you found it useful, consider starring the repo and sharing feedback.
- Author: Joao Oliveira
- Blog: https://joaooliveira.net
- Topics: .NET, Redis, Distributed Systems, Caching Patterns
Issues and pull requests are welcome.
If you plan a larger change, please open an issue first so we can align on scope.
Licensed under the MIT License. See the LICENSE file for details.