Prompt
I'm building a multi-tenant app and need TTL-based caching for hot data
Latest observation
Oct 28, 2026
OpenAI APIWeb search: off
A good pattern for multi-tenant TTL caching is:
- Partition by tenant
- Use per-item TTL
- Support explicit invalidation
- Prevent cache stampedes
- Track memory/limits per tenant if needed
Recommended cache key design
Use a composite key:
tenant:{tenantId}:resource:{resourceType}:{resourceId}
Example:
tenant:acme:user:123
tenant:globex:plan:premium
This ensures data from one tenant never leaks into another tenant’s cache.
Basic TTL cache options
In-memory cache
Good for:
- single instance apps
- very low latency
- simple deployments
Bad for:
- multiple app servers
- restarts lose cache
- limited memory
Examples:
- Node:
lru-cache - Python:
cachetools.TTLCache - Java: Caffeine
- Go: ristretto / bigcache
Distributed cache
Good for:
- multi-instance deployments
- shared cache across pods/servers
- better consistency
Examples:
- Redis
- Memcached
For multi-tenant apps, Redis is usually the best default.
Redis approach
Store with TTL
Set keys with expiration:
SET tenant:acme:user:123 "{json}" EX 300
This caches for 5 minutes.
Fetch pattern
Pseudo-flow:
1. Build tenant-aware cache key
2. Read from cache
3. If miss:
- load from DB
- store in cache with TTL
- return result
Example in Node.js with Redis
async function getUser(tenantId, userId) {
const key = `tenant:${tenantId}:user:${userId}`;
const cached = await redis.get(key);
if (cached) return JSON.parse(cached);
const user = await db.users.findOne({ tenantId, userId });
if (user) {
await redis.set(key, JSON.stringify(user), { EX: 300 }); // 5 min TTL
}
return user;
}
Important multi-tenant considerations
1. Isolation
Always include tenant ID in:
- cache keys
- invalidation logic
- metrics/quotas if applicable
2. Hot-key protection
If one tenant is much busier, it can dominate cache usage.
Possible mitigations:
- per-tenant key prefixes
- per-tenant TTL tuning
- per-tenant quotas
- local + distributed cache layering
3. Cache stampede prevention
If many requests miss the same key at once, they can all hit the DB.
Fixes:
- request coalescing / single-flight
- short randomized TTL jitter
- stale-while-revalidate pattern
Example TTL jitter:
const ttl = 300 + Math.floor(Math.random() * 30);
4. Invalidation
When data changes, delete the key(s):
await redis.del(`tenant:${tenantId}:user:${userId}`);
If many related keys exist, use:
- versioned keys
- tag-based invalidation
- key namespaces
Example namespace versioning:
tenant:acme:v42:user:123
Bump the version to invalidate a whole group.
Good default architecture
If you want something practical and scalable:
- Redis as shared TTL cache
- Tenant-prefixed keys
- 5–15 minute TTLs for hot read-heavy data
- Explicit invalidation on writes
- Jitter + single-flight for stampede protection
- Optional local in-process cache for ultra-hot reads
Example strategy by data type
- User profile: 5–30 min TTL
- Tenant settings: 1–10 min TTL
- Feature flags: 30 sec–5 min TTL
- Reference data: 10–60 min TTL
- Authorization claims: very short TTL or event-driven invalidation
If you want, I can give you:
- a Redis + Node/Express implementation,
- a Redis + Python/FastAPI implementation, or
- a production-ready cache design for multi-tenant SaaS.