Implement a Writer-Preferring Reader-Writer Lock for Vdisk Metadata

Last updated: November 29, 2026

Quick Overview

Implement a reader-writer lock with acquire_read, release_read, acquire_write and release_write where many readers may hold it together, a writer holds it alone, and a waiting writer blocks new readers so writers are never starved by a steady stream of reads.

Nutanix
Software Engineering Fundamentals
Software Engineer
Nutanix
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Medium

0

0

0 solved


Implement a reader-writer lock with acquire_read, release_read, acquire_write and release_write where many readers may hold it together, a writer holds it alone, and a waiting writer blocks new readers so writers are never starved by a steady stream of reads.

The I/O path reads vdisk metadata such as block maps and config far more often than it changes them, but operations like snapshot or migration must eventually get exclusive access. Nutanix data path roles probe concurrency primitives closely, so the interviewer checks correct condition variable use, starvation reasoning, and what can still go wrong.

What the Interviewer Expects
  • Uses one mutex plus a condition variable and waits in a while loop
  • Tracks active readers, an active writer flag and waiting writers
  • Blocks new readers while any writer waits
  • Wakes waiters with notify_all when state changes
  • Keeps counters correct even if a wait is interrupted
  • Discusses reader starvation and recursion hazards
Key Topics to Cover
concurrency
condition variables
reader-writer lock
starvation
threading
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
  • Now readers can starve under constant writes. How would you make it fair (FIFO phases)?
  • How would you support upgrading a read lock to a write lock without deadlock?
  • Why is a reader-writer lock sometimes slower than a plain mutex?
  • How would you avoid cache line contention on the reader count with many cores?
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

Invariants: writer implies readers == 0; at most one writer. Liveness: once a writer is waiting, no new reader may enter, so the writer gets in after current readers drain. Spurious wakeups must b...

High-Level Design

A single threading.Condition protects three fields: readers, writer, waiting_writers. Readers wait while a writer is active or waiting. Writers announce themselves by incrementing `waiting_wri...


Submit Your Answer
Markdown supported

Related Questions