0
Overview
During flash sale events on e-commerce platforms, thousands of concurrent users query popular item details simultaneously. If a cached key expires at peak traffic, hundreds of requests fall through to the relational database at once—a phenomenon known as the cache stampede (or thundering herd problem).
Strategies for Mitigation
- Cache-Aside with Mutex Locking: Only the first worker that acquires a distributed lock queries the database to repopulate the cache.
- Probabilistic Early Expiration (XFetch): Recomputes the cached value ahead of time based on a probabilistic function.
- Soft Expiration: Serves stale data temporarily while a background worker updates the store.
Mutex Lock Invalidation Pattern in Python
Here is an example using Redis distributed locks to eliminate cache stampedes:
import redis
import time
r = redis.Redis(host='localhost', port=6379)
def get_product_details(product_id):
cache_key = f"product:{product_id}"
data = r.get(cache_key)
if data:
return data
lock_key = f"lock:{product_id}"
if r.set(lock_key, "true", nx=True, ex=5):
try:
data = fetch_from_database(product_id)
r.setex(cache_key, 3600, data)
return data
finally:
r.delete(lock_key)
else:
time.sleep(0.05)
return get_product_details(product_id)