🚀 What I built
A production-ready cache stampede prevention library in Go with 3 strategies,
full Prometheus metrics, and a live Grafana dashboard.
🔥 The problem
When a hot cache key expires, thousands of concurrent requests all miss at the same
time and hammer the database. This is a cache stampede — and it can take down your DB.
🛡️ 3 strategies implemented
Hard Lock + Stale-While-Revalidate
Only one process fetches the new value (Redis SETNX + Lua atomic script)
Everyone else gets served the stale value immediately — zero wait time
2, XFetch (probabilistic early refresh)
Formula: P = exp(-β × timeRemaining / δ)
Starts refreshing in the background before the key expires
No thundering herd because refreshes are spread out probabilistically
Request Coalescing (singleflight)
In-process deduplication: 50 concurrent goroutines → 1 real DB call
~13:1 dedup ratio visible in Grafana
📊 Monitoring stack
Prometheus metrics for every decision point (hit/miss/stale/lock_contention)
Grafana…