In a monolith, you can use MemoryCache to store data in RAM. In microservices, if you have 10 instances of an API, each with its own local cache, they will quickly become out of sync. Redis is an external, ultra-fast, in-memory database that acting as a "Shared Brain" for all your services.
This is the industry standard. Your .NET code checks Redis. If the data is there (Cache Hit), return it. If not (Cache Miss), query the database, save the result to Redis for next time, and return it. This protects your database from being overwhelmed by repetitive queries.
var cachedUser = await _cache.GetStringAsync("user:5");
if (cachedUser != null) return JsonConvert.DeserializeObject<User>(cachedUser);
var user = await _db.Users.FindAsync(5);
await _cache.SetStringAsync("user:5", JsonConvert.SerializeObject(user));
return user;
Q: "What is a 'Cache Stampede' and how do we prevent it?"
Architect Answer: "A Cache Stampede happens when a high-traffic item (e.g., 'Product-123') expires from the cache. Suddenly, 1,000 requests hit the database simultaneously to refresh that item, potentially crashing the DB. We prevent this using **Distributed Locking**. We ensure that only ONE request is allowed to query the database and update the cache, while the other 999 requests wait for a few milliseconds until the cache is repopulated."