DEV Community

I am Augustus
I am Augustus

Posted on

Cache Stampede Prevention in Go β€” 1k req/s load test + full monitoring stack

πŸš€ 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

  1. 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

  1. 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 dashboard with 9 panels auto-provisioned via docker-compose

Circuit Breaker state transitions (Closed β†’ Open β†’ Half-Open) tracked as gauges

Load test drives 1 000 req/s of sustained traffic


🧱 Tech stack

Go 1.2xΒ· Redis 7 Β· Prometheus Β· Grafana Β· Docker Compose

Please give me a star for my repository if it is useful for you. Thanks


πŸ”— GitHub: [https://github.com/0x48core/go-latency/tree/main/cache-stampede]

Top comments (0)