InnoDB Gap Locks and Next-Key Locks Explained

Under REPEATABLE READ InnoDB locks the gaps between index records to stop phantoms, which also blocks inserts you did not expect.

In this chapter

InnoDB gap and next-key locks: why a range is locked when no row is there

More on MySQL and InnoDB

Keep learning

Overview

the gap (10, 20) No record exists, so only the space is locked. record 20 only Nothing else can be inserted with this key, so no gap is needed. next-key (10, 20], plus the gap (20, 30), plus the primary-key record every record above 20 and every gap up to +infinity Includes the gap after the last record. id BETWEEN 15 AND 25 gap (10, 20), record 20, gap (20, 30) Both ends fall inside gaps, which are closed. the gap (40, +infinity) This blocks every append that would take a larger key. any of the above under READ COMMITTED only the existing records Gaps stay open (see section 4). chapters · mysql and innodb The gap lock: why a range is locked when no row is there

1. Next-key lock = record + the gap before it, and the phantom it prevents

Under REPEATABLE READ (InnoDB's default) a locking read takes next-key locks: for each index entry it touches, a lock on the entry itself (the record lock) and on the open interval between it and the previous entry (the gap lock). Written as an interval, the next-key lock on 20 with a neighbour 10 is (10, 20]: open at the left, closed at the right.

A gap lock does not lock a gap for its owner

It has one job: stop other transactions from inserting into the interval. It does not stop other sessions from holding a gap lock on the same interval. A shared gap lock and an exclusive gap lock have the same effect.

Why the insert is the one who waits

An INSERT first announces itself with an insert intention lock on the gap. Two inserts of different keys into the same gap do not block each other, but any gap lock held by someone else blocks the announcement.

The primary key is dragged in

Which region each query locks, REPEATABLE READ, keys 10, 20, 30, 40

5. The classic gap-lock deadlock: two sessions insert into the same gap

Ways out, from best to least intrusive: Use READ COMMITTED for that code path, where the existence check takes no gap lock. Keep the transaction short, so the window for the collision is tiny. Long transactions with gap locks also create the waits described in the isolation levels chapter. ✓ Recap

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.