Phase 28 of 30 · Topic 28.2

Cache Stampede (Thundering Herd) Lock-Free Prevention

1Concept

When a hot cache key expires, thousands of concurrent requests attempt to query the database simultaneously. `HybridCache` locks execution so only 1 request queries the database while all other requests wait for the cached result.

2Architecture Diagram

10,000 Concurrent Requests for Expired Key:
├── Request 1: Acquires single background fetch lock ──> Queries Database
└── Requests 2..10,000: Await Request 1's result ──> All served from fresh cache! (Zero DB Crash!)

3Code Example

C# 13 & .NET 9
using System;

public class CacheStampedeConceptDemo
{
    public static void Main()
    {
        Console.WriteLine("HybridCache locks concurrent factory execution per key automatically to eliminate Cache Stampedes.");
    }
}

4Expected Output

HybridCache locks concurrent factory execution per key automatically to eliminate Cache Stampedes.

5Key Takeaways

  • Prevents catastrophic database outages during cache expiration spikes.
  • Zero configuration required in `HybridCache`.
  • Works across distributed server clusters.