Use cache-aside for read-heavy data: read Redis, load the authoritative store on a miss, then cache a serialized DTO with a finite TTL. Cache keys must include version and tenant scope. Example: var key = $"v2:{tenant.RestaurantId}:{tenant.OutletId}:product:{id}"; var cached = await cache.GetStringAsync(key, ct); if (cached is null) { var dto = await repository.GetAsync(id, tenant, ct); await cache.SetStringAsync(key, JsonSerializer.Serialize(dto), options, ct); } Invalidate or update after a successful database commit. Add TTL jitter and request coalescing to reduce stampedes. Do not cache secrets or authorization decisions carelessly; plan for Redis failure, stale data, serialization changes, memory limits, and metrics for hit rate and latency.
Backbencher Developer · · 0 likes