Build1 distinct publisher3 min readUpdated
A small FastAPI repro turns 20 simultaneous requests for one tenant into 20 database queries while the dashboard still reads healthy. The metric that catches it is in-flight loads per key.
The Engineer · Build desk
Compiled by The EngineerSomething wrong?How this is made
A developer profiling a small FastAPI service found the cache hit ratio sitting above 90 percent, single requests fast, and only modest database load during ordinary traffic [1]. The database CPU still climbed whenever a release or a retry storm sent twenty requests for the same tenant at once, and the hit ratio stayed high enough through it to look innocent [2].
That contradiction is not a monitoring bug. A hit ratio describes what happened after a key was already warm; it says nothing about how many cold-start requests became database work in the same instant [4]. The service cached tenant project lists for thirty seconds [3], so the window in which a key is unowned and cold is roughly one query duration long. In the published repro the query is stubbed at 0.1 seconds [11], which makes the dangerous window about 0.33 percent of the TTL [15]. Aggregate ratios are averages over minutes. They cannot resolve a hazard that lives for a fraction of a second per key, several hundred times smaller than the refresh interval it hides inside.
The repro is worth running because it is honest about the mechanism. Twenty threads hit one tenant endpoint, and the counter prints db_calls: 20 [5]. The first request checked the cache, missed, released the lock, and went to the database; the other nineteen did exactly the same thing, because nothing had marked the key as being loaded [6]. The author names this a cache stampede [7]. Nineteen of the twenty queries were redundant, or 95 percent of the burst's database work [16].
Note what the author did before touching the code: he made the failure measurable, and set the bar that any change failing to move db_calls from twenty toward one was not solving the race [8]. That is the right order of operations, and it also tells you which metric belongs on the dashboard. Not hit ratio. Concurrent cold misses per key.
The fix supplies the instrument for free. The singleflight wrapper keeps an inflight dictionary mapping each key to an event and a result box; the first caller runs the loader and the rest wait on the event and receive the same result [9]. Rerun the burst and db_calls drops to one, so the database sees a single query while the cache remains useful afterwards [10]. The size of that inflight map, and the count of waiters per entry, is exactly the signal a hit ratio cannot express. Export it as a gauge. An alert on waiters-per-key above a small threshold fires during the stampede, not in the incident review afterwards.
Two caveats on the material. The author states he used MonkeyCode's free model access to compare a per-key lock against a process-wide lock before committing [12], and discloses that the article was prepared as part of MonkeyCode's product outreach [13]. Treat the tooling mention accordingly; the repro and the counter stand on their own. The author also warns that thread scheduling can hide a race [14], which is the reason a single passing burst test is weak evidence and a per-key concurrency gauge in production is strong evidence.
What to watch: whether your cache client exposes in-flight loads at all. Most expose hits, misses, and evictions, which means the number that predicts the outage is the one you have to add yourself. Check also whether your singleflight is per process. Twenty threads deduplicated to one query per replica is still one query per replica, and a fleet-wide release still lands as a burst on the database.
Follow any of these and your For You feed starts watching them — no settings page required.
Ranked by verification strength, evidence, and original report placement.
After wiring the singleflight into the endpoint's cache miss path, the same burst test drops db_calls to one; other callers wait on the event and receive the same result, so the database sees a single query while the cache stays useful afterwards.
The service cached tenant project lists for thirty seconds.
A high hit ratio only tells you what happened after a key was already warm; it does not tell you how many cold-start requests turned into database work at the same moment.
In the minimal reproduction, a ThreadPoolExecutor with 20 workers issues 20 requests to /projects/tenant-a and the output shows db_calls: 20.
The repro's read_from_db function sleeps 0.1 seconds, described in the code comment as a slow query shortened for the repro.
The author states he used MonkeyCode's free model access to review a per-key lock against a process-wide lock before committing to one.
Evidence-backed comparisons of source perspectives and observed adoption signals. Read the methodology
Which Builder, Operator, and Investor concerns the observed source mix emphasized—not a truth score.
Evidence, demonstrated adoption, hype gap, incentives, and confidence are assessed independently, each on its own current evidence. How these are measured.
Self-contained repro, no production data
The core mechanism is demonstrated with runnable code, a stated before value (db_calls: 20), a stated after value (db_calls: 1), an explicit pass condition, and a candid limitations list, which is stronger than a typical opinion post. But every number is self-reported by one author, the 'slow query' is a 0.1s sleep against a 30s TTL, verification is only a rerun of the same script in a second container, and the headline's production recommendation (alert on cold misses per key) is never instrumented or measured.
No adoption signal in sources
The single source reports no release, deployment, benchmark suite, pricing, licensing, or usage disclosure. The SingleFlight snippet is illustrative code in a blog post, and no user counts, downloads, or third-party usage of it or of MonkeyCode are given, so adoption cannot be scored without inventing facts.
Headline generalizes slightly beyond the toy repro
Mildly overstated. The title asserts a monitoring rule ('Alert on Cold Misses Per Key') and 'melted the database', while the demonstrated result is twenty duplicated calls to a 0.1s sleep in a single-process test and no alerting implementation. The overstatement is limited because the body explicitly refuses to universalize the fix, lists five conditions where a local singleflight is wrong or wasted, and names the multi-replica illusion-of-protection risk.
Disclosed vendor outreach with in-body tool plugs
The post states it was prepared as part of MonkeyCode's product outreach, and the vendor appears twice inside the technical narrative: 'free model access' used to weigh per-key against process-wide locking, and a 'free server option' used as the second test environment. That is a clear commercial incentive shaping the tooling mentions, partly mitigated by an explicit disclosure and by technical content that is otherwise vendor-neutral and verifiable.
Verifiable code, single sponsored source
Confidence is moderate-low: one publisher, one author, no independent replication, and a disclosed commercial incentive. It is not lower because the central claims are mechanical and independently checkable by anyone running the two code blocks, and because the author documents the counter-measurement method and the fix's limits rather than only its wins.
build
An AI test suite hit 94% coverage and missed the one branch that mattered1 distinct publisher
build
The dangerous cell in your state machine is the one nobody filled in1 distinct publisher
build
Force the tool call, then hand Lightsail a long-lived key1 distinct publisher
build
Stop timing your GraphQL tests and start counting loader calls1 distinct publisher
Distinct publishers with included, body-backed reporting in this cluster.
dev.to
1 article · August 17, 2026