Implement a Writer-Preferring Read-Write Lock for a Product Catalog Cache

Last updated: November 29, 2026

Quick Overview

Build a read-write lock that allows many concurrent readers of an in-memory catalog cache but never starves the periodic writer that swaps in a fresh snapshot. Use it to guard a cache with get and refresh operations.

Amazon
Software Engineering Fundamentals
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Hard

0

0

0 solved


Build a read-write lock that allows many concurrent readers of an in-memory catalog cache but never starves the periodic writer that swaps in a fresh snapshot. Use it to guard a cache with get and refresh operations.

Amazon detail-page services keep hot catalog data in process memory and refresh it from a feed every few minutes, so read traffic dwarfs writes. The interviewer is checking whether you can build a correct condition-variable protocol, explain reader versus writer starvation, and keep lock release safe under exceptions.

What the Interviewer Expects
  • Uses a single condition variable with clear invariants for readers, writer, and waiting writers
  • Blocks new readers while a writer is queued so refreshes are not starved
  • Releases locks in finally blocks so an exception cannot leak a held lock
  • Explains why notify_all is needed when the last reader leaves
  • Discusses copy-on-write snapshot swapping as a lock-free alternative
Key Topics to Cover
read-write lock
condition variables
starvation
writer preference
copy-on-write
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. 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 a thread that already holds the read side?
  • Readers can now starve under constant refreshes; how would you add fairness?
  • Could you avoid the lock entirely by swapping an immutable dict reference?
  • How would you test for starvation and deadlock deterministically?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements
  • get(asin) may run concurrently from many request threads.
  • refresh(snapshot) replaces the whole cache and must be exclusive.
  • A refresh that is waiting must eventually run even under a constan...
High-Level Design

State is three fields behind one Condition: active reader count, a writer-held flag, and a count of waiting writers. Readers wait while a writer holds the lock or one is queued; that second clau...


Submit Your Answer
Markdown supported