Sometimes I cache: implementing lock-free probabilistic caching
Dec 26, 2024Thibault Meunier

Summary
This post introduces a probabilistic approach to cache revalidation as an alternative to traditional cache locks. It details the problem of cache stampedes when cache entries expire and multiple requests hit the origin simultaneously. The post presents a probabilistic revalidation strategy where a request to revalidate the cache is sent with a probability 'p' (e.g., 1/500). This approach avoids the latency and fallibility of contacting an external lock service, relying instead on local randomness. The post also discusses the mathematical expectation of requests hitting the origin over time with this probabilistic method and outlines the implementation using Cloudflare Workers and the Cache API.