Architecture & Data Flow:
- Goal: Serve product details, inventory, and cart data at 50,000 requests/sec with low latency and cache consistency.
1. Multi-Tier Caching Topology:
- L1 Edge Cache (CDN): Cache static product images, CSS, and semi-static catalog HTML with geo-distributed Points of Presence (PoPs).
- L2 Application Cache: Local in-memory cache (Guava/Caffeine) on API pods for ultra-hot read-only metadata (categories, tax rates).
- L3 Distributed Cache: Clustered Redis with read replicas for dynamic entities (product details, user sessions).
2. Cache Invalidation & Consistency Patterns:
- Cache-Aside (Lazy Loading): Read from cache; on miss, query database and populate cache. Ideal for product catalog reads.
- Write-Through / Write-Around: Write to DB first, then invalidate cache key to prevent stale reads.
- Distributed Invalidation: When product inventory updates, publish an event to Kafka; cache invalidator workers evict stale Redis keys globally.
3. Resilience Against Common Failure Modes:
- Cache Stampede / Thundering Herd: Use mutex distributed locks or probabilistic early expiration (XFetch) so only one worker queries the database on cache miss.
- Cache Penetration: Cache null values with short TTLs and implement Bloom Filters at the gateway to reject non-existent IDs instantly.