---
title: "How to manage cache consistency and avoid cache stampedes during high-concurrency flash sales?"  
description: "How to manage cache consistency and avoid cache stampedes during high-concurrency flash sales?"  
author: "Anubhav Sharma"  
published: 2026-08-23  
updated: 2026-08-23  
canonical: https://answers.mindstick.com/qa/117105/how-to-manage-cache-consistency-and-avoid-cache-stampedes-during-high-concurrency-flash-sales  
category: "Database Systems"  
tags: ["Caching", "Redis", "Database Optimization", "System Design"]  
reading_time: 1 minute  

---

# How to manage cache consistency and avoid cache stampedes during high-concurrency flash sales?

## 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:

```python
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)
```


---

Original Source: https://answers.mindstick.com/qa/117105/how-to-manage-cache-consistency-and-avoid-cache-stampedes-during-high-concurrency-flash-sales

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
