Writer-Preferring Read-Write Lock for a Hot Config Cache
Last updated: November 29, 2026
Quick Overview
Implement a read-write lock for an in-process feature-flag cache read on every request and refreshed every few seconds, so many readers proceed in parallel but a waiting writer is never starved by a steady stream of readers.
ByteDance
November 29, 20260
0
0 solved
Implement a read-write lock for an in-process feature-flag cache read on every request and refreshed every few seconds, so many readers proceed in parallel but a waiting writer is never starved by a steady stream of readers.
ByteDance services read experiment and config flags on every request, and the refresher must not be starved when QPS is high. This onsite concurrency question checks whether you can reason about fairness, not just mutual exclusion, and discuss alternatives like copy-on-write snapshots.
What the Interviewer Expects
- Allow concurrent readers and exclusive writers
- Track waiting writers so new readers block behind them
- Use notify_all where several kinds of waiters exist
- Explain reader starvation versus writer starvation
- Offer copy-on-write with an atomic reference 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
- Make the lock reentrant for readers.
- Support upgrading a read lock to a write lock: why is it deadlock-prone?
- Compare this to an RCU or snapshot pointer approach in Go or C++.
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
Requirements: acquire_read and release_read, acquire_write and release_write. Any number of readers at once, writers exclusive with readers and with each other. Once a writer is waiting, new readers m...
High-Level Design
One Condition guards three counters: active readers, whether a writer holds the lock, and how many writers are waiting. Readers wait while a writer is active or waiting. Writers announce themselves by...