ASP.NET Core Web API Interview Question #196
How do you design caching for a horizontally scaled API?
Senior API Design, Architecture, Scalability & Production Scenarios Senior Advanced
Quick Interview Answer
Process-local memory caches are independent on each instance, so shared cache requirements generally need a distributed cache or another coordinated mechanism. Define cache keys, tenant/user partitioning, expiration, invalidation, and failure behavior explicitly.
Detailed Explanation
Process-local memory caches are independent on each instance, so shared cache requirements generally need a distributed cache or another coordinated mechanism. Define cache keys, tenant/user partitioning, expiration, invalidation, and failure behavior explicitly.
The database or authoritative service should remain the source of truth unless the architecture intentionally uses a different consistency model.
Code Example
var key = $"product:{productId}";
var json = await _cache.GetStringAsync(key, token);
if (json == null)
{
var product = await LoadProductAsync(productId, token);
await _cache.SetStringAsync(key, Serialize(product), options, token);
}