~/work/cache-stampede
A hot key expired. What did Postgres have to do?
Cache Stampede Lab compares three ways to refill one hot key.Bhupesh built and measured this in a local lab with synthetic traffic, not customer traffic.
Evidence
| Mode | DB reads per refill (both runs) | Errors per 24,000 requests | p99 / peak-second p99 (ms) |
|---|---|---|---|
| Naive | 96 | 308 / 309 | 1.141 / 428.623 1.319 / 379.25 |
| Per-process singleflight | 3 | 0 / 0 | 87.976 / 196.371 87.23 / 195.468 |
| Shared Redis lease | 1 | 0 / 0 | 105.228 / 212.995 138.687 / 268.599 |
- Naive96
- Per-process singleflight3
- Shared Redis lease1
Why naive p99 looks good
Most requests hit cache, rejected requests fail fast, and fewer than 1% execute the slow read. The full-run p99 was only 1.14 / 1.32 ms. The highest one-second p99 reached 428.6 / 379.3 ms, and the runs returned 308 and 309 errors. Those numbers show what happened during the refill. Coordination avoided the rejections here, but it didn’t make the underlying query faster.
Where the answer changes
| Scenario | DB reads | Errors | What happened |
|---|---|---|---|
| 2,000 distinct keys Naive / singleflight / lease | 2,000 each | 7,074 7,078 7,034 | About 7,050 errors in each mode. Per-key coordination doesn’t fix total-capacity overload. |
| Cold 20-key cache (lease) | 20 | 0 | The sentinel lease was preserved and all 20 keys refilled. |
| 75 ms lease / 200 ms query | 94 | 16,093 | Owners repeatedly lost the right to publish: 15,772 HTTP 504s and 321 client timeouts. |
| Owner killed during an 800 ms query | Partial metrics | 98 | A surviving replica refilled the key. The proxy returned 98 HTTP 502s. |
| Redis outage | 0 | 15,915 | The lab fails closed in all modes, protecting PostgreSQL by sacrificing availability. The lease run returned HTTP 503s. |
There is no lease renewal. Lease expiry or Redis failover can allow overlapping refreshes. Redis failover was not experimentally validated.
The singleflight trade-off
Per-process singleflight needs no cross-replica coordination. It cost one read per replica (three per refill) and returned zero errors in both runs. Singleflight removes the need for a shared lock, not for Redis as a cache.
Measurement method
- Window
Two 12 s trials per mode. The warmed hot key was deleted around 4 s.
- Traffic
An open-loop generator in the Compose network offered, dispatched, and completed 24,000 requests per trial with zero generator drops. Completions and latency percentiles include errors.
- Revisions
Ran code at 1c45635 (recorded in environment.json). Results were committed in 4c4bcf1.
- Environment
Docker with 2 CPUs and about 4 GB RAM on an arm64 host, using Docker Compose, Nginx, Redis 7, and Postgres 17.
- Refill settings
2 s lease TTL, 16 DB connections per replica, and a simulated 200 ms delay that holds a DB connection.
Sources & follow-up
- Sources
- Related conceptBhupesh’s write-up of OpenAI’s published approach
The measurements on this page come from his separate local lab.