Single-flight caching pattern stops cache stampedes from crashing databases
Engineers at podbor-minuta.ru, a Moscow new-build property monitoring service, encountered repeated HTTP 500 timeout errors under load caused by a cache stampede — a condition where hundreds of simultaneous requests hit an expired cache key and flood the database at once. The root cause was a naive cache-refresh logic that sent every concurrent request to the database independently when a cached value expired, exhausting the connection pool. To fix this, the team implemented a single-flight caching pattern, where only the first request for an expired key triggers a database query while all other concurrent requests wait for the same shared promise. The in-flight promise map is cleaned up in a finally block to ensure the key is released even if the computation fails, preventing deadlocks. After the fix, 200 concurrent requests on a hot key now produce just one database query instead of 200, eliminating connection pool exhaustion entirely.
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