Implement a TTL and LRU Cache for User Permission Lookups
Last updated: November 29, 2026
Quick Overview
Implement a cache mapping a user id to that user's resolved group set, with a fixed capacity evicting the least recently used entry, a time-to-live after which entries are treated as missing, and explicit invalidation when a membership change event arrives.
Glean
November 29, 20260
0
0 solved
Implement a cache mapping a user id to that user's resolved group set, with a fixed capacity evicting the least recently used entry, a time-to-live after which entries are treated as missing, and explicit invalidation when a membership change event arrives.
Every Glean query needs the searcher's full principal set, and recomputing it from the identity provider each time would blow latency and rate limits. The interviewer checks a correct O(1) LRU, careful TTL semantics, testability through an injectable clock, and awareness that stale permissions are a security issue, not just a freshness issue.
What the Interviewer Expects
- Uses an ordered dict (or hash map plus doubly linked list) for O(1) get and put
- Expires entries lazily on read using a monotonic clock
- Injects the clock so tests do not sleep
- Provides invalidate for membership change events
- Stores immutable group sets so callers cannot mutate cached state
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- The cache runs on 40 query servers. How do invalidations reach all of them?
- Should a cache miss during an identity provider outage fail open or fail closed, and why?
- How would you avoid a thundering herd when a popular user's entry expires?
- How would you size capacity and TTL from production metrics?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
get(user) returns the group set or None if absent or expired, and marks the entry recently used. put(user, groups) inserts or refreshes the entry and its expiry, evicting the least recently used...
High-Level Design
OrderedDict keeps insertion order and supports move_to_end and popitem(last=False) in O(1), which is exactly LRU. Each value stores (expires_at, frozenset(groups)). Expiry is lazy: checked on ...