Back to problems

Make Cache Fetch Thread-Safe and Single-Flight

System Design · Tubi · Hard

Consider an in-memory TTL cache with an operation fetch(key, loader, ttl). If key is present and has not expired, the call returns the cached value immediately. Otherwise, the call invokes loader, stores the value produced by a successful load, and returns that value. A load that fails, throws, or times out must not populate the cache. Design this operation for a multithreaded environment. If many threads miss on the same key at the same time, the cache should avoid…

Checking your access…