Implement a Writer Preferring Read Write Lock for Membership Checks
Last updated: November 29, 2026
Quick Overview
Every API request checks project membership from an in-process cache that is read constantly and replaced occasionally. Implement a read write lock that allows concurrent readers, exclusive writers, and does not starve writers.
Asana
November 29, 20260
0
0 solved
Every API request checks project membership from an in-process cache that is read constantly and replaced occasionally. Implement a read write lock that allows concurrent readers, exclusive writers, and does not starve writers.
Permission checks sit on the hot path of every Asana request, and a stale or torn membership view is a security bug, not just a performance bug. The interviewer wants a correct condition-variable implementation, clear reasoning about starvation, and judgment about when a simpler design (copy-on-write) would be better.
What the Interviewer Expects
- Implements the lock with a single mutex and condition variable and while-loop waits
- Explains reader starvation of writers and adds writer preference
- Uses try/finally or context managers so exceptions release the lock
- Tests with concurrent threads and an invariant checker
- Proposes copy-on-write snapshots as an alternative for read-heavy caches
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
- Is this lock reentrant? What happens if a reader tries to upgrade to a writer?
- When would a plain mutex outperform this lock?
- How would you make the cache lock-free for readers?
- How do you keep the cache consistent with the database across many servers?
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
Many threads call can_view(project, user); occasionally one replaces a project's member set. Readers may overlap. A writer needs exclusive access. Once a writer is waiting, new readers must not keep...
High-Level Design
State: active reader count, a writer flag, and a waiting-writer count, all guarded by one condition variable. Readers wait while a writer is active or waiting. Writers register as waiting, then wait u...