Introduction
Modern cloud‑native applications require high performance, low latency, and horizontal scalability. One of the most effective ways to achieve this is by implementing a distributed caching layer using Redis deployed on Kubernetes, integrated seamlessly with .NET 10.
This article provides a complete, production‑ready guide to:
- Deploy Redis on Kubernetes
- Configure high availability and persistence
- Integrate Redis caching in a .NET 10 application
- Implement distributed cache patterns
- Optimize performance and observability
1. Architecture Overview
Key Components
- Kubernetes Cluster – Hosts application and Redis workloads
- Redis Deployment / StatefulSet – Provides in‑memory caching
- .NET 10 API Service – Consumes Redis for distributed caching
- ConfigMaps & Secrets – Manage configuration securely
- Ingress / Service – Expose application endpoints
High‑Level Flow
- Client requests data from .NET API
- API checks Redis cache first
- If cache miss → fetch from database
- Store result in Redis with expiration
- Return response to client
2. Deploy Redis on Kubernetes
2.1 Namespace
apiVersion: v1
kind: Namespace
metadata:
name: redis-system
2.2 Redis StatefulSet (Production Ready)
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
namespace: redis-system
spec:
serviceName: redis
replicas: 3
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:7.2
ports:
- containerPort: 6379
args:
- "--appendonly"
- "yes"
volumeMounts:
- name: redis-data
mountPath: /data
volumeClaimTemplates:
- metadata:
name: redis-data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 5Gi
2.3 Redis Service
apiVersion: v1
kind: Service
metadata:
name: redis
namespace: redis-system
spec:
clusterIP: None
selector:
app: redis
ports:
- port: 6379
targetPort: 6379
3. Install Redis Using Helm (Recommended)
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install redis bitnami/redis \
--set architecture=replication \
--set replica.replicaCount=2 \
--set auth.enabled=true
Benefits of Helm deployment:
- Built‑in replication & failover
- Secure password authentication
- Easy upgrade & rollback
4. .NET 10 Distributed Cache Integration
4.1 Install NuGet Packages
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
dotnet add package StackExchange.Redis
4.2 Configure Redis in Program.cs
using Microsoft.Extensions.Caching.StackExchangeRedis;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = builder.Configuration["Redis:Connection"];
options.InstanceName = "app-cache:";
});
var app = builder.Build();
5. Implement Distributed Caching Service
public interface ICacheService
{
Task<T?> GetAsync<T>(string key);
Task SetAsync<T>(string key, T value, TimeSpan? expiry = null);
Task RemoveAsync(string key);
}
public class RedisCacheService : ICacheService
{
private readonly IDistributedCache _cache;
public RedisCacheService(IDistributedCache cache)
{
_cache = cache;
}
public async Task<T?> GetAsync<T>(string key)
{
var data = await _cache.GetStringAsync(key);
return data is null ? default : JsonSerializer.Deserialize<T>(data);
}
public async Task SetAsync<T>(string key, T value, TimeSpan? expiry = null)
{
var options = new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = expiry ?? TimeSpan.FromMinutes(10)
};
var json = JsonSerializer.Serialize(value);
await _cache.SetStringAsync(key, json, options);
}
public async Task RemoveAsync(string key)
{
await _cache.RemoveAsync(key);
}
}
6. Use Cache in Controller
[ApiController]
[Route("api/products")]
public class ProductsController : ControllerBase
{
private readonly ICacheService _cache;
public ProductsController(ICacheService cache)
{
_cache = cache;
}
[HttpGet("{id}")]
public async Task<IActionResult> Get(int id)
{
var cacheKey = $"product:{id}";
var product = await _cache.GetAsync<ProductDto>(cacheKey);
if (product != null)
return Ok(product);
// Simulate DB call
product = new ProductDto { Id = id, Name = "Demo Product" };
await _cache.SetAsync(cacheKey, product, TimeSpan.FromMinutes(5));
return Ok(product);
}
}
7. Kubernetes Deployment for .NET 10 API
apiVersion: apps/v1
kind: Deployment
metadata:
name: dotnet-api
spec:
replicas: 2
selector:
matchLabels:
app: dotnet-api
template:
metadata:
labels:
app: dotnet-api
spec:
containers:
- name: api
image: yourrepo/dotnet10-api:latest
ports:
- containerPort: 8080
env:
- name: Redis__Connection
value: "redis.redis-system.svc.cluster.local:6379"
8. Production Best Practices
Security
- Enable AUTH password or Redis ACL
- Store secrets in Kubernetes Secrets
- Restrict access via NetworkPolicies
Performance
- Use connection pooling
- Apply TTL strategy per data type
- Avoid caching extremely large objects
High Availability
- Use Redis Sentinel or Redis Cluster
- Configure PodDisruptionBudget
- Enable persistent storage
Observability
- Export metrics with Redis Exporter + Prometheus
- Visualize via Grafana dashboards
- Monitor cache hit ratio & latency
9. Common Caching Patterns
Cache‑Aside (Recommended)
- Read cache
- If miss → read DB
- Save to cache
Write‑Through
- Update cache and DB simultaneously
Write‑Behind
- Write cache first → async persist to DB
Conclusion
Implementing Redis distributed caching on Kubernetes with .NET 10 dramatically improves:
- API response time
- System scalability
- Infrastructure resilience
By combining Helm‑based Redis deployment, .NET distributed cache abstraction, and cloud‑native observability, you achieve a fully production‑ready caching architecture suitable for modern microservices platforms.