Quick revision
The short answer
A record lock locks one index record, a gap lock locks the space between two index records, and a next-key lock is a record lock plus a gap lock on the gap before that record. At REPEATABLE READ, a locking read such as SELECT ... FOR UPDATE takes next-key locks on the range it scans so that no new row can appear inside it. Inserting id 15 needs an insert intention lock in the gap where 15 would go, and that gap is covered by the FOR UPDATE's gap lock, so the INSERT waits even though no row 15 exists.
What to remember
In this answer, we saw that InnoDB locks index entries and the gaps between them. A record lock covers one entry, a gap lock covers the space between two entries, and a next-key lock covers an entry plus the gap before it. At REPEATABLE READ, SELECT ... FOR UPDATE takes next-key locks over the scanned range to prevent phantoms, so inserting 15 waits because its insert intention lock conflicts with the gap lock in (12, 18). At READ COMMITTED gap locks are used only for foreign key and duplicate checks, a unique equality lookup that finds its row takes only a record lock, and performance_schema.data_locks shows all of it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A record lock locks one index record, a gap lock locks the space between two index records, and a next-key lock is a record lock plus a gap lock on the gap before that record. At REPEATABLE READ, a locking read such as SELECT ... FOR UPDATE takes next-key locks on the range it scans so that no new row can appear inside it. Inserting id 15 needs an insert intention lock in the gap where 15 would go, and that gap is covered by the FOR UPDATE's gap lock, so the INSERT waits even though no row 15 exists.
Why this question is asked
Lock waits on rows that do not exist confuse many developers the first time they meet them. Interviewers ask this because it tests whether we understand that InnoDB locks index entries and the spaces between them, not abstract rows, and why it does so. In this answer, we will recap what InnoDB locks, define the three lock types, walk through the BETWEEN example step by step, and then see how READ COMMITTED and unique lookups change the picture and how to look at locks with performance_schema.data_locks.
1A Quick Recap: InnoDB Locks Index Entries
InnoDB does not lock rows as abstract objects. It locks entries in an index. When a statement searches through an index, every entry it examines can be locked, and since the table itself is the clustered index ordered by primary key, a search by primary key locks entries in that tree.
The reason for locking more than the matching rows is the phantom problem. At REPEATABLE READ, if our transaction runs SELECT ... WHERE id BETWEEN 10 AND 20 FOR UPDATE twice, the second run should see the same rows. If another transaction could insert id 15 in between, a new row would appear: a phantom. To prevent this, InnoDB must lock not only the rows it found but also the empty spaces where new rows could land.
2Record, Gap and Next-Key Locks
A record lock locks a single index entry, for example the entry for id 12.
A gap lock locks the open interval between two neighbouring entries, for example the space between 12 and 18. It does not lock 12 or 18 themselves; it only stops other transactions from inserting a value into that space. Two transactions can hold gap locks on the same gap without conflict.
A next-key lock is the combination: a record lock on an entry plus a gap lock on the gap before it. A next-key lock on 18 covers the interval (12, 18], that is everything after 12 up to and including 18. This is the default lock InnoDB takes for locking reads and updates at REPEATABLE READ.
3Walking Through the BETWEEN Example
Suppose the table has rows with ids 5, 10, 12, 18, 20 and 25, and session A locks the range.
| Index position | Lock | Effect on an INSERT |
|---|---|---|
| gap (5, 10) | none | insert of 7 succeeds |
| record 10 | record lock | row 10 cannot be changed |
| gap (10, 12) and record 12 | next-key lock on 12 | insert of 11 waits |
| gap (12, 18) and record 18 | next-key lock on 18 | insert of 15 waits |
| gap (18, 20) and record 20 | next-key lock on 20 | insert of 19 waits |
| gap (20, 25) | gap lock | insert of 22 waits |
| after 25 | none | insert of 30 succeeds |
Session A scans the primary key from 10 up to the first entry past 20, and locks what it scans. Entries 12, 18 and 20 get next-key locks, so the gaps (10, 12), (12, 18) and (18, 20) are locked along with the records. The scan has to look at 25 to know the range has ended, so the gap (20, 25) is locked too. Exactly which edge entries get a full next-key lock or only a record or gap lock has been refined between versions, so we check data_locks rather than memorise it.
Now session B inserts 15. Before an insert, InnoDB asks for an insert intention lock, a special gap lock that says I want to insert into this gap. Insert intention locks do not conflict with each other, so many transactions can insert different values into the same gap. But they do conflict with ordinary gap locks held by others. Value 15 belongs in the gap (12, 18), and session A holds a lock on that gap, so B waits until A commits or rolls back, or until innodb_lock_wait_timeout (50 seconds by default) expires.
4How READ COMMITTED Changes It
At READ COMMITTED, InnoDB does not promise to prevent phantoms, so it stops taking gap locks for searches and index scans. It uses them only for foreign key checks and duplicate key checks. Session A would hold record locks on 10, 12, 18 and 20 only, and session B's insert of 15 would succeed at once. A second run of the same SELECT would then see 15, a phantom.
This is why so much gap lock blocking disappears when a team switches to READ COMMITTED. The trade-off is that the application can no longer rely on a locked range staying free of new rows, so rules like 'no two bookings in this slot' must be enforced by a unique constraint instead.
5Unique Lookups That Find a Row
Next-key locking is about ranges. When a statement searches a unique index with an equality condition and finds exactly one matching row, no other row with the same value can ever be inserted, because the unique index forbids it. So InnoDB takes only a record lock on that entry, with no gap.
Notice the second case. When the equality search finds nothing, InnoDB locks the gap where the value would be, to keep the empty result stable, and that blocks other inserts into the same gap. This detail is the root of a well-known deadlock pattern with read-then-insert code.
6Seeing the Locks in performance_schema.data_locks
Since MySQL 8.0, performance_schema.data_locks shows every lock InnoDB holds or is waiting for, and data_lock_waits shows which lock waits for which. The LOCK_MODE column tells the lock type: X alone means a next-key lock, X,REC_NOT_GAP a record lock, X,GAP a gap lock, and X,INSERT_INTENTION an insert intention lock. LOCK_STATUS is GRANTED or WAITING, and LOCK_DATA shows the index value, or supremum pseudo-record for the gap after the last entry.
The last line is session B: it waits for an insert intention lock in the gap before 18, exactly the gap A has locked.
7Follow-up questions
What does performance_schema.data_locks show?
It shows one row per lock held or requested by InnoDB transactions: the transaction, the table and index, whether it is a table or record lock, the lock mode (next-key, record only, gap, insert intention, and the intention locks IX and IS on the table), whether it is granted or waiting, and the locked index value. Joined with data_lock_waits and the threads table, it tells us who blocks whom.
Do gap locks conflict with each other?
No. Two transactions can hold gap locks on the same gap, shared or exclusive, because a gap lock only prevents inserts. They conflict with insert intention locks from other transactions, which is what makes an insert wait.
8Conclusion
In this answer, we saw that InnoDB locks index entries and the gaps between them. A record lock covers one entry, a gap lock covers the space between two entries, and a next-key lock covers an entry plus the gap before it. At REPEATABLE READ, SELECT ... FOR UPDATE takes next-key locks over the scanned range to prevent phantoms, so inserting 15 waits because its insert intention lock conflicts with the gap lock in (12, 18). At READ COMMITTED gap locks are used only for foreign key and duplicate checks, a unique equality lookup that finds its row takes only a record lock, and performance_schema.data_locks shows all of it.
Open with
InnoDB locks index entries, and at REPEATABLE READ it also locks the spaces between them to prevent phantoms. A record lock locks one index entry. A gap lock locks the open interval between two entries and only blocks inserts into it; gap locks do not conflict with each other.
Then, if they want more
A next-key lock is a record lock plus the gap before the record, and it is what locking reads and updates take on the range they scan at REPEATABLE READ.
So with ids 10, 12, 18 and 20, SELECT FOR UPDATE with BETWEEN 10 AND 20 locks those records and the gaps between them, plus the gap up to the next entry after 20.
An INSERT of 15 first asks for an insert intention lock in the gap between 12 and 18, and that conflicts with the gap lock the first transaction holds, so it waits even though no row 15 exists.
At READ COMMITTED, InnoDB uses gap locks only for foreign key and duplicate key checks, so this blocking mostly disappears, but phantoms become possible. And an equality lookup on a unique index that finds its row takes only a record lock, while one that finds nothing locks the gap.
To see it on a real server I would query performance_schema.data_locks, where the lock mode shows X, X,GAP, X,REC_NOT_GAP or INSERT_INTENTION and the status shows granted or waiting.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8.0
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "At REPEATABLE READ in InnoDB, what are record locks, gap locks and next-key locks? Why...": https://scrivio-interview-library.netlify.app/databases/mysql/5ff7874cd9/