Skip to content

~/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.

Lab case study

Evidence

Two runs per mode. Errors are run 1 / run 2; latency shows one pair per run, run 1 first.
ModeDB reads per refill (both runs)Errors per 24,000 requestsp99 / peak-second p99 (ms)
Naive96308 / 3091.141 / 428.623 1.319 / 379.25
Per-process singleflight30 / 087.976 / 196.371 87.23 / 195.468
Shared Redis lease10 / 0105.228 / 212.995 138.687 / 268.599

Three replicas, 2,000 offered requests per second, and a deliberate 200 ms PostgreSQL delay.

PostgreSQL reads per refill
  • 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

24,000 requests per run. The Redis outage numbers are from the lease run.
ScenarioDB readsErrorsWhat happened
2,000 distinct keys Naive / singleflight / lease2,000 each7,074 7,078 7,034About 7,050 errors in each mode. Per-key coordination doesn’t fix total-capacity overload.
Cold 20-key cache (lease)200The sentinel lease was preserved and all 20 keys refilled.
75 ms lease / 200 ms query9416,093Owners repeatedly lost the right to publish: 15,772 HTTP 504s and 321 client timeouts.
Owner killed during an 800 ms queryPartial metrics98A surviving replica refilled the key. The proxy returned 98 HTTP 502s.
Redis outage015,915The 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.