Read-write lock 00:00:00 Make a prediction You will predict which lock modes can overlap and recognize how a steady stream of readers can starve a writer. Two readers hold a shared lock on a timetable. Can a writer update one departure while either reader is still inside that protected region? No. Shared readers may coexist with each other; an exclusive writer must wait for existing readers to leave. Once the writer completes, readers can observe the new consistent version. 00:00:31 Fish, half a bird, bird Museum visitors may view a mural together, but an artist repainting it closes the room. If new visitors can always slip past a waiting artist, the repaint may never start. Readers share while observing; writers need exclusion while changing. Whether a waiting writer blocks newcomers is a fairness policy, not an automatic property of every lock. A read-write lock is a lock with two modes. In shared mode any number of threads may hold it, to read. In exclusive mode exactly one thread holds it, to write, and nobody reads meanwhile. Much data is read far more often than changed: prices, settings, routes, permissions. Two reads can not disturb each other, so making them wait is waste. 00:01:21 Publish a timetable update Two passengers read the timetable at the same time. Neither changes it, so their shared locks can overlap. A dispatcher wants to update a departure time. The writer needs exclusive access and waits for active readers to leave. Once the update completes, a new reader sees the new timetable consistently. Under continuous traffic, a fairness policy may be needed so fresh readers cannot postpone writers forever. Let two readers enter together and then start a writer. While both readers hold their lock, both can observe the old complete timetable. The writer publishes its replacement only after they leave. Reading one part outside the guard could combine old times with new platform numbers. 00:02:06 Avoid upgrading blindly Two readers both decide they must update and each tries to upgrade while keeping a shared lock. Depending on the lock, both can wait indefinitely. Release the read lock, acquire the write lock, then recheck the condition because another writer may have changed it in between. Two readers each discover that they want to update, and both retain their read lock while requesting exclusive access. Neither can become the only holder. Drop the read lock, acquire write permission and validate the condition again, or use a documented upgradeable-lock primitive. Dropping a read lock and later acquiring the write lock opens a gap. The condition that motivated the update may no longer hold, so recheck it. For rarely changing immutable snapshots, publishing a replacement can give readers a simpler path than repeated lock acquisition. Java ReentrantReadWriteLock. Supports reentry and downgrading, not upgrading: a reader that asks for the write lock never gets it. The order is unspecified by default, about first come in fair mode. At most 65,535 read holds. Go sync.RWMutex. Any number of readers or one writer. A blocked Lock call excludes new readers, so the documentation forbids recursive read locking. It can be neither upgraded nor downgraded. 00:03:33 Try an interview follow-up How do you test that readers really share? Two readers meet at a barrier inside the lock. It passes only if both are inside. All exclusion tests would also pass with a plain lock. Readers arrive continuously while a writer waits to update a catalogue. Under a reader-preferring lock the writer may wait indefinitely. Decide whether that outcome is acceptable. Test an arriving reader after the writer has queued, and explain how your chosen fairness policy changes latency for both sides. 00:04:08 What to take with you Many readers or one writer, never both. Three numbers are enough: readers, writer, waiting writers. Readers wait for the last two, writers for the first two. Readers first starves writers, writers first starves readers, one line or phases starve nobody. A waiting writer that gives up must wake the others.