← Lessons

quiz vs the machine

Platinum1820

Databases

Gap Locks and Phantom Prevention

How databases stop new rows from sneaking into a range.

5 min read · advanced · beat Platinum to climb

The Phantom Problem

A phantom read happens when a transaction runs the same range query twice and sees new rows the second time because another transaction inserted them. Row locks alone cannot prevent this, since the new rows did not exist to lock.

Gap Locks

To block phantoms at the serializable or repeatable read level, engines lock the gaps between existing index values, not just the rows. A gap lock on the space between two keys stops any other transaction from inserting a new key into that range.

Tradeoffs

  • Gap locks prevent phantoms but reduce concurrency because they block inserts into whole ranges.
  • They apply on index ranges, so good indexes keep the locked gap small.
  • A combined row plus gap lock is often called a next key lock.

Key idea

Gap locks guard the spaces between indexed values so new rows cannot appear inside a queried range, which prevents phantom reads at the cost of lower insert concurrency.

Check yourself

Answer to earn rating on the learn ladder.

1. What is a phantom read?

2. What do gap locks lock?

3. What is a combined row and gap lock called?