# Data Caching

High-performance caching with in-memory LRU, Redis, and tiered strategies.

## In-Memory Cache

Fast LRU cache with TTL support for single-instance deployments.

## Tiered Cache (L1 + L2)

Combine fast local cache with distributed Redis for best of both worlds.

### Latency Comparison

<1ms
L1 Memory Hit
1-5ms
L2 Redis Hit
10-100ms
Database Query

## Cache Invalidation

Multiple strategies for keeping cache data fresh.

## Cache Keys

Structured keys with tenant and parameter support.

## Metrics & Monitoring

## Cache Backends

Backend | Use Case | Latency | Shared
--- | --- | --- | ---
MemoryCache | Single instance, hot data | ~100ns | ❌
RedisCache | Distributed, shared state | ~1-5ms | ✅
TieredCache | Best of both | ~100ns (L1 hit) | ✅

## Best Practices

### ✅ Do

- Include tenant ID in cache keys for multi-tenant apps
- Set appropriate TTLs based on data volatility
- Monitor hit rates and adjust capacity
- Use tag-based invalidation for related data

### ❌ Don't

- Cache sensitive data without encryption
- Skip cache invalidation on writes
- Use very long TTLs without invalidation strategy