Mutexes and reader–writer locks 00:00:01 Two readers follow the old index Juno and Kai open edition seven of a search index. A publisher has a corrected eighth edition ready. How can the two searches finish consistently while the new edition becomes available? Juno and Kai both hold read access, so they can keep reading V7 without blocking each other. The system allows multiple readers at once. First consider a design where each reader holds its shared lock throughout the search. A writer that edits the shared index in place needs exclusive access. Picture the index as a published edition of a catalogue. Two people can consult the same complete edition simultaneously. Editing its pages underneath either reader raises a different coordination problem. 00:00:41 Why a writer cannot edit in place yet The reader–writer lock lets multiple readers in at once, but blocks any writer until all readers release their locks. The writer is stuck waiting for both readers to finish. No one else can write until they’re done, even if the system is idle. Suppose Kai waits 3.2 seconds on a remote call while still holding the read lock. The publisher must wait for the conflicting holder to release it. An unrelated outbound call held inside the read-side lock can stretch the publication delay. Copy or pin the exact data needed, then perform slow downstream work outside the protected operation where the design permits. 00:01:15 Build privately and switch the pointer A different design builds immutable edition eight separately while the published catalogue remains available. Only the reference to the current edition needs a short protected exchange. In this example the exclusive swap takes twelve milliseconds once it acquires the lock. Readers holding already-pinned immutable pages can continue on edition seven. After the swap, a fresh lookup acquires a reference to edition eight. Earlier readers finish against their already-pinned edition, so neither search sees half of one edition and half of the next. Here the design changes. The lock now protects selecting or swapping a reference to an immutable edition. A reader pins that edition before releasing the lock, so its later work no longer blocks publication. 00:01:57 Old readers still need their pages Even though V8 is live, V7 must stay around until the last reader finishes. It’s still referenced. We use reference counting to track how many readers are still using V7. When it hits zero, we can free it. Swapping the pointer alone isn’t enough. If we freed V7 too soon, readers would crash with invalid memory. Reference counting is one option; epoch-based reclamation is another. In either case the publisher needs evidence that no reader can still dereference an old page. Seeing that the current pointer changed is insufficient. 00:02:28 A busy system must make room for publication For the original read-lock design, replay the moment a fresh reader arrives while a writer is waiting. Some policies admit that reader immediately; others reserve the next turn for the writer. Continually favoring readers can starve publication. Continually favoring writers can delay readers. The applicable scheduling policy determines which risk the application accepts. Track query latency and the delay between a completed build and publication. These measurements reveal whether the chosen lock and snapshot policy meets both user-facing commitments. The workload defines the tradeoff. A new index that waits forever to publish is stale for everyone, while readers delayed behind constant publication may miss their own deadlines. Measure both sides under realistic traffic.