Implement a Writer-Preferring Read-Write Lock for a Hot Routing Table
Last updated: November 29, 2026
Quick Overview
Every API request reads an in-memory table mapping model names to serving clusters, while a background job occasionally updates it. Implement a read-write lock that allows many concurrent readers, gives writers exclusive access, and prevents writer starvation under constant read traffic.
OpenAI
November 29, 20260
0
0 solved
Every API request reads an in-memory table mapping model names to serving clusters, while a background job occasionally updates it. Implement a read-write lock that allows many concurrent readers, gives writers exclusive access, and prevents writer starvation under constant read traffic.
OpenAI's request routing layer reads configuration like model-to-cluster mappings on every call and must be able to push changes during incidents. The interviewer is checking whether you can build a correct synchronization primitive from a condition variable and reason about fairness and starvation.
What the Interviewer Expects
- Allows concurrent readers and excludes readers during a write
- Tracks waiting writers so new readers yield to them
- Re-checks conditions in loops after every wait
- Uses try and finally so exceptions cannot leak a held lock
- Discusses reader starvation in the opposite direction
- Mentions copy-on-write or atomic pointer swap as an alternative
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
- How would you make the lock reentrant for readers?
- Could you avoid the lock entirely by swapping an immutable dict?
- How would you support upgrading a read lock to a write lock, and why is it dangerous?
- How does the Python GIL change what you actually need here?
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
Readers run in parallel. A writer waits for current readers to finish, then runs alone. Once a writer is waiting, new readers queue behind it so a steady read stream cannot block updates forever....
High-Level Design
State under one condition variable: readers inside, writer inside, waiting_writers. A reader enters only when no writer is inside or waiting. A writer registers as waiting, then waits until no r...