Cache Stampede Bug: High Hit Ratio Can Mask Burst Database Overload
A FastAPI service appeared healthy with a cache hit ratio above 90 percent, yet database CPU spiked sharply during traffic bursts caused by releases or retry storms. The root cause was a cache stampede: when a cached key expired, all concurrent requests missed simultaneously, each independently querying the database before any result was stored. A minimal code reproduction confirmed that 20 concurrent requests for the same tenant each triggered a separate database call, despite caching being in place. The issue stems from caches only deduplicating reads after a key is warm, not preventing simultaneous cold-start queries for the same key. The recommended fix is a singleflight pattern, which allows only one caller to fetch data while others wait and reuse that single result.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in