Menu

1 / 2

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

DEV Community·I am Augustus·6 months ago
#10ZnCo8U
#redis#go#software#coding#cache
Reading 0:00
15s threshold

🚀 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…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.