Gap lock
A locking mechanism in MySQL/InnoDB that locks the "gap" between index values — not just existing rows, but the range where new rows could be inserted. It prevents phantom reads under Repeatable Read by blocking inserts into a locked range.
The problem it solves: without gap locks, another transaction could insert a new row in the middle of a range you just read → a "phantom" appears and breaks Repeatable Read. InnoDB's "next-key lock" = a row lock + gap lock combined → locks both the row and the gap before it. Mechanic: SELECT ... WHERE x BETWEEN 10 AND 20 FOR UPDATE also locks the gaps so no new rows with x in the range can be added. Trade-off: it protects against phantoms but can cause unexpected deadlocks and block seemingly unrelated inserts → a common source of mysterious InnoDB lock conflicts. Gap locks disappear under Read Committed (one reason to lower the isolation for certain workloads). A specific InnoDB behavior that often surprises developers.