Multi-Threaded Index Practice Problems
- What is the benefit of having latches with separate read and write modes?1
- Why wouldn't we want to keep things simple and have one latch for a whole index?2
- When considering a latching protocol for a B+ Tree index, what makes a node safe? (When can we release the latch on the parent node)3
- In latch crabbing on a B+ tree with \(N\) nodes, what is the greatest number of latches a thread might need to hold at one time?4
- Descibe a situation where deadlock can occur when two threads are simultaneously using a B+ tree index.5
Footnotes:
Multiple threads doing simultaneous reads of shared data do not pose a problem, so we can getter better parallelism if we allow this. However, a thread that is modifying (writing) shared data needs exclusive access in order to avoid race conditions (i.e., concurrency bugs). So allowing threads to acquire a latch in read or write mode can facilitate increased parallelism where possible while still protecting writes.
This would potentially cause the index to become a bottleneck for system performace, with lots of threads stuck waiting for their turn with the index (when in reality they could safely access it simultaneously).
A node is safe when the current operation will not cause a split or a merge (and thus will not require modifying the parent).
If a thread is modifying the tree in such a way that changes might propagate all the way up to the root, it will need to hold latches along the whole path from root to leaf. In a balanced tree of \(N\) nodes, this will be \(\log N\) latches.
Thread A is scanning along the leaves of the tree to read data, and thread B is performing an update (such as delete) that involves modifying a leaf and its sibling. Thread A has holds a read latch on node 1 and needs to acquire a read latch on node 2. Thread B holds a write latch on node 2 and need to acquire a write latch on node 1.