Caching Strategies
Cache for latency and cost, not as a second source of truth without a plan. Name the consistency model and invalidation path.
Workflow
- Read pattern, freshness needs, and blast radius of stale data.
- Layer: browser, CDN, app memory, Redis, DB.
- Key design and TTL; avoid unbounded key growth.
- Invalidation: TTL-only vs event-driven vs write-through.
- Stampede: singleflight, soft TTL, probabilistic early expire.
- Negative caching for misses.
- Metrics: hit ratio, latency, error on cache path.
Output format
## Cache design: <surface>
**Consistency:** …
**Keys / TTL:** …
**Fill path:** …
**Invalidate:** …
**Stampede control:** …
**Failure mode:** …
Rules
- Security: never cache authorized personal data under shared keys.
- Prefer explicit invalidation for user-visible mutations.
- Document what clients may see stale.
- Measure before multi-layer complexity.
- Do not invent Redis commands incorrectly — describe intent.
Edge cases
- CDN + auth: private vs public.
- Multi-region: replication lag.
- Search indexes: near-cache vs source of truth.