C# Interview Question #196

How would you design a thread-safe in-memory cache?

Senior .NET Practical Scenarios Senior Advanced

Quick Interview Answer

Prefer established caching abstractions such as IMemoryCache instead of implementing a cache from scratch.

Detailed Explanation

Prefer established caching abstractions such as IMemoryCache instead of implementing a cache from scratch.

If custom shared state is necessary, use thread-safe data structures such as ConcurrentDictionary and atomic APIs such as GetOrAdd. Consider the cache stampede problem, where many requests simultaneously load the same missing key.

A production cache also needs expiration, eviction, memory limits, invalidation, error handling, and metrics. For multi-server deployments, an in-memory cache exists independently in each process, so distributed caching may be required for shared consistency.