When a hot cache key expires, a thousand concurrent requests can hammer the same database query. The singleflight pattern collapses those into a single backend call. Here is a production-grade TypeScript implementation, the edge cases that break naive versions, and the one-line integration that makes it cache-aware.
A cached endpoint quietly serves 50k req/s for weeks — until the key expires and 4,000 simultaneous misses hit Postgres in the same millisecond. Here is the 40 lines of single-flight + probabilistic early refresh that turn cache expiration from a cliff into a soft handoff, with the load-test numbers that prove it.