Scenario: Tenant-Scoped Redis Caching with Key Prefixing¶
Goal¶
Document a sample that combines multitenancy caching behavior with Redis key prefixing to guarantee cache isolation per tenant.
Sample name and location¶
- Solution:
CleanArchitecture.Extensions.Samples.Multitenancy.RedisCaching - Path:
samples/CleanArchitecture.Extensions.Samples.Multitenancy.RedisCaching
Modules used¶
- Multitenancy core
- Multitenancy.Caching
- Multitenancy.Redis (planned adapter)
- Multitenancy.AspNetCore for inbound resolution
Prerequisites¶
- Base Web API solution; provide a Redis connection (local container or test instance) via configuration.
- Numbered step comments and matching README entries for every change.
Steps¶
- Reference
CleanArchitecture.Extensions.Multitenancy,CleanArchitecture.Extensions.Multitenancy.AspNetCore,CleanArchitecture.Extensions.Multitenancy.Caching, and addCleanArchitecture.Extensions.Multitenancy.Rediswhen available; ensure distributed cache services are registered before the multitenancy adapters. - Configure
MultitenancyOptionsto require tenant context for cache-bearing operations and enableTenantScopedCacheBehaviorin MediatR. - Configure
RedisKeyStrategyOptionswith a prefix pattern such as{tenant}:{resource}:{hash}, disable global keys, and set default TTLs per resource; wireTenantRedisConnectionResolverif different endpoints per tenant are needed. - Register Redis distributed cache (StackExchange.Redis) pointing at the dev instance; expose the connection string via configuration so tests can swap to in-memory fakes.
- Update caching helpers to reject keys that lack tenant context; audit background jobs to ensure
TenantContextis restored before cache usage. - Add integration tests to assert keys include tenant prefixing, cross-tenant cache pollution is prevented, and cache hits/misses are counted per tenant.
- Document migration steps for changing prefix formats and how to flush a single tenant's keys safely during deletion without issuing a global flush.
- Note operational guardrails: avoid flush-all commands, monitor keyspace size per tenant, and alert on prefix collisions.
Validation¶
- Cache keys are consistently prefixed with the current tenant ID and optional environment/resource segments.
- Requests without tenant context fail fast when cache access is required.
- Cache isolation holds under concurrent cross-tenant load and during key format migrations.