graph TD
User[User Request] --> CDN[CDN Cache<br/>e.g., CloudFront/Akamai]
CDN -->|Miss + TTL 1h| Edge[Edge Cache<br/>e.g., Varnish/Envoy]
Edge -->|Miss + TTL 10m| Service[Service Cache<br/>e.g., App Redis]
Service -->|Miss + TTL 1m| DB[Database<br/>e.g., PostgreSQL + Query Cache]
DB --> Service
Service --> Edge
Service -->|Stampede Guard| Guard{Stampede<br/>Protector}
Guard -->|Acquire Lease| Populate[Background Populate]
Cache Hierarchy Design
This hierarchy is optimized for a dynamic API endpoint like GET /api/v1/users/{userId}/profile, serving user profiles with personalization (e.g., JSON response with user data, stats, recent activity). It minimizes DB load while handling high traffic (e.g., 1M+ RPS).
1. Cache Keys (Hierarchical & Granular)
Keys are prefixed by layer and include versioning (:v1) for invalidation. Use consistent hashing for sharding.
Layer
Cache Key Example
Description
CDN
profile:{sha256(host+path+qs)}:v1
URL-based (host + /api/v1/users/123/profile?fields=basic). SHA256 for collisions. User-agnostic for shared content.
Edge
edge:profile:user:{userId}:v1:{qs_hash}
Adds userId + query hash (e.g., fields). Per-user but coarse.