Caching Strategy
Purpose
Design a multi-layer caching architecture that minimizes latency and server load while maintaining data freshness. Produces a cache hierarchy diagram, key schema, TTL policy table, and invalidation flow for each cacheable resource.
Inputs
- Application architecture (server framework, database, CDN provider)
- Resource inventory (pages, API responses, static assets, database queries)
- Access patterns (read/write ratio, update frequency, personalization requirements)
- Freshness requirements (how stale can each resource be before it hurts the user?)
- Current caching setup (if any) and known pain points
Process
Step 1: Identify Cacheable Resources and Access Patterns
Inventory all resources served by the application:
- Static assets (JS, CSS, images, fonts) — versioned or content-hashed?
- HTML pages — fully static, SSG, SSR, or dynamic?
- API responses — public or user-specific? How often do underlying data change?
- Database query results — read frequency vs write frequency per table/query
- Computed/aggregated data — dashboards, reports, search indexes
For each resource, document the read:write ratio and acceptable staleness window.
Step 2: Design Cache Hierarchy
Define what is cached at each layer, from closest to the user outward:
- Browser cache — immutable versioned assets, prefetched resources
- CDN / Edge cache — public HTML, public API responses, optimized images
- Application cache — session data, computed results, frequently-accessed records
- Database query cache — prepared statement results, materialized views
Document which layer is the primary cache for each resource type.
Step 3: Define Cache Key Schema
Design consistent, collision-free cache keys:
- Include resource type, identifier, and variant (e.g.,
page:blog:slug:v2:locale:en)
- Account for personalization dimensions (user role, locale, feature flags)
- Define key namespacing to support bulk invalidation (e.g.,
page:blog:*)
- Document key generation patterns for each resource type
Step 4: Specify TTL Policies per Resource Type
Assign time-to-live values based on staleness tolerance:
- Immutable assets — long TTL (1 year) with content-hash cache busting
- Semi-static content (blog posts, product pages) — medium TTL (minutes to hours)
- Dynamic/personalized content — short TTL (seconds) or no-cache with revalidation
- API responses — TTL matched to underlying data change frequency
Document TTL as both max-age and s-maxage where CDN behavior differs from browser.
Step 5: Design Invalidation Strategy
Define how stale cache entries are removed or refreshed:
- Event-driven invalidation — cache purge triggered by data mutations (webhook, pub/sub)
- Time-based expiry — TTL-only, no active invalidation
- Versioned keys — new key on data change, old key expires naturally
- Tag-based invalidation — associate cache entries with tags, purge by tag
Map each resource to its invalidation method. Document the mutation-to-purge flow.
Step 6: Plan Stale-While-Revalidate Patterns
Design graceful degradation for cache misses:
- Which resources support
stale-while-revalidate? Define the stale window.
- Which resources require strict freshness (financial data, auth state)?
- Define fallback behavior when the origin is unavailable (serve stale? show error?)
- Document
Cache-Control header construction for each resource class
Step 7: Specify Cache Warming Strategy for Cold Starts
Plan for empty caches after deployments, CDN purges, or scaling events:
- Which resources should be pre-warmed? (High-traffic pages, critical API responses)
- Warming method — build-time generation, deployment hook, background job, on-demand
- Warming priority order — warm the hottest paths first
- Cold start latency budget — acceptable response time before cache is warm
Output Format
# Caching Strategy: [Application Name]
## Cache Hierarchy
[User] → Browser Cache → CDN/Edge → App Cache → DB Query Cache → [Database]
| Layer | Resources Cached | TTL Range | Invalidation |
|-------|-----------------|-----------|-------------|
| Browser | ... | ... | ... |
| CDN/Edge | ... | ... | ... |
| Application | ... | ... | ... |
| DB Query | ... | ... | ... |
## Cache Key Reference
| Resource Type | Key Pattern | Variants | Example |
|--------------|-------------|----------|---------|
| ... | ... | ... | ... |
## TTL Policy Table
| Resource | Browser max-age | CDN s-maxage | stale-while-revalidate | Rationale |
|----------|----------------|-------------|----------------------|-----------|
| ... | ... | ... | ... | ... |
## Invalidation Flows
### [Resource Type]
1. Mutation occurs in [source]
2. [Event/webhook] triggers cache purge
3. [Layer] invalidates entries matching [pattern]
4. Next request triggers revalidation
## Cache Warming Plan
| Resource | Method | Trigger | Priority |
|----------|--------|---------|----------|
| ... | ... | ... | ... |
## Cache-Control Headers
| Route Pattern | Cache-Control Value |
|--------------|-------------------|
| ... | ... |
Quality Checks
Evolution Notes
1---2name: caching-strategy3department: "tuner"4description: "Cache hierarchy design with TTL policies, invalidation flows, and warming strategies"5version: 16triggers:7 - "cache"8 - "caching"9 - "CDN"10 - "stale"11 - "invalidation"12 - "TTL"13 - "edge"14 - "stale-while-revalidate"15---1617# Caching Strategy1819## Purpose2021Design a multi-layer caching architecture that minimizes latency and server load while maintaining data freshness. Produces a cache hierarchy diagram, key schema, TTL policy table, and invalidation flow for each cacheable resource.2223## Inputs2425- Application architecture (server framework, database, CDN provider)26- Resource inventory (pages, API responses, static assets, database queries)27- Access patterns (read/write ratio, update frequency, personalization requirements)28- Freshness requirements (how stale can each resource be before it hurts the user?)29- Current caching setup (if any) and known pain points3031## Process3233### Step 1: Identify Cacheable Resources and Access Patterns3435Inventory all resources served by the application:36- Static assets (JS, CSS, images, fonts) — versioned or content-hashed?37- HTML pages — fully static, SSG, SSR, or dynamic?38- API responses — public or user-specific? How often do underlying data change?39- Database query results — read frequency vs write frequency per table/query40- Computed/aggregated data — dashboards, reports, search indexes4142For each resource, document the read:write ratio and acceptable staleness window.4344### Step 2: Design Cache Hierarchy4546Define what is cached at each layer, from closest to the user outward:47- **Browser cache** — immutable versioned assets, prefetched resources48- **CDN / Edge cache** — public HTML, public API responses, optimized images49- **Application cache** — session data, computed results, frequently-accessed records50- **Database query cache** — prepared statement results, materialized views5152Document which layer is the primary cache for each resource type.5354### Step 3: Define Cache Key Schema5556Design consistent, collision-free cache keys:57- Include resource type, identifier, and variant (e.g., `page:blog:slug:v2:locale:en`)58- Account for personalization dimensions (user role, locale, feature flags)59- Define key namespacing to support bulk invalidation (e.g., `page:blog:*`)60- Document key generation patterns for each resource type6162### Step 4: Specify TTL Policies per Resource Type6364Assign time-to-live values based on staleness tolerance:65- Immutable assets — long TTL (1 year) with content-hash cache busting66- Semi-static content (blog posts, product pages) — medium TTL (minutes to hours)67- Dynamic/personalized content — short TTL (seconds) or no-cache with revalidation68- API responses — TTL matched to underlying data change frequency6970Document TTL as both `max-age` and `s-maxage` where CDN behavior differs from browser.7172### Step 5: Design Invalidation Strategy7374Define how stale cache entries are removed or refreshed:75- **Event-driven invalidation** — cache purge triggered by data mutations (webhook, pub/sub)76- **Time-based expiry** — TTL-only, no active invalidation77- **Versioned keys** — new key on data change, old key expires naturally78- **Tag-based invalidation** — associate cache entries with tags, purge by tag7980Map each resource to its invalidation method. Document the mutation-to-purge flow.8182### Step 6: Plan Stale-While-Revalidate Patterns8384Design graceful degradation for cache misses:85- Which resources support `stale-while-revalidate`? Define the stale window.86- Which resources require strict freshness (financial data, auth state)?87- Define fallback behavior when the origin is unavailable (serve stale? show error?)88- Document `Cache-Control` header construction for each resource class8990### Step 7: Specify Cache Warming Strategy for Cold Starts9192Plan for empty caches after deployments, CDN purges, or scaling events:93- Which resources should be pre-warmed? (High-traffic pages, critical API responses)94- Warming method — build-time generation, deployment hook, background job, on-demand95- Warming priority order — warm the hottest paths first96- Cold start latency budget — acceptable response time before cache is warm9798## Output Format99100```markdown101# Caching Strategy: [Application Name]102103## Cache Hierarchy104105```106[User] → Browser Cache → CDN/Edge → App Cache → DB Query Cache → [Database]107```108109| Layer | Resources Cached | TTL Range | Invalidation |110|-------|-----------------|-----------|-------------|111| Browser | ... | ... | ... |112| CDN/Edge | ... | ... | ... |113| Application | ... | ... | ... |114| DB Query | ... | ... | ... |115116## Cache Key Reference117118| Resource Type | Key Pattern | Variants | Example |119|--------------|-------------|----------|---------|120| ... | ... | ... | ... |121122## TTL Policy Table123124| Resource | Browser max-age | CDN s-maxage | stale-while-revalidate | Rationale |125|----------|----------------|-------------|----------------------|-----------|126| ... | ... | ... | ... | ... |127128## Invalidation Flows129130### [Resource Type]1311. Mutation occurs in [source]1322. [Event/webhook] triggers cache purge1333. [Layer] invalidates entries matching [pattern]1344. Next request triggers revalidation135136## Cache Warming Plan137138| Resource | Method | Trigger | Priority |139|----------|--------|---------|----------|140| ... | ... | ... | ... |141142## Cache-Control Headers143144| Route Pattern | Cache-Control Value |145|--------------|-------------------|146| ... | ... |147```148149## Quality Checks150151- [ ] Every cacheable resource has an assigned cache layer and TTL152- [ ] Cache keys include all variant dimensions (locale, role, version)153- [ ] Invalidation strategy is defined for every resource that can be mutated154- [ ] Stale-while-revalidate windows are specified where appropriate155- [ ] Personalized content is excluded from shared caches (CDN/edge)156- [ ] Cache warming covers the highest-traffic paths157- [ ] Cache-Control headers are specified for all route patterns158- [ ] Cold start scenario is addressed with acceptable latency budgets159160## Evolution Notes161<!-- Observations appended after each use -->