MySQL & InnoDB (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What is a Phantom Read and how does InnoDB prevent it using Next-Key Locks?
senior LevelSystem DesignMySQL
In Simple Words:
A Phantom Read occurs when a transaction queries a range of rows (e.g. WHERE age > 30), and another transaction inserts a new row matching that range (age = 35), causing the first transaction to see a "phantom" row on re-query.
- Next-Key Lock: Combines a Record Lock (locks the index record itself) with a Gap Lock (locks the gap between index records).
- InnoDB locks the entire interval, preventing other transactions from inserting new rows into the gap until the transaction commits!