In MySQL 8.4 InnoDB, what does innodb_autoinc_lock_mode control, and why is its default 2 (interleaved) rather than 1 (consecutive)?
answer
- a table-level lock for counters
- held to statement end
- simple versus bulk inserts
- row-based logging makes order irrelevant
basics
~20 sinnodb_autoinc_lock_mode chooses how InnoDB hands out AUTO_INCREMENT values: 0 and 1 use a statement-long table-level AUTO-INC lock for some or all inserts, while 2, the 8.4 default, uses none, trading consecutive per-statement values for concurrency.
solid answer
~40 sAn **AUTO-INC lock** is a special table-level lock InnoDB can take while it generates `AUTO_INCREMENT` values; it is held to the end of the statement, not the transaction. `innodb_autoinc_lock_mode` picks the algorithm: `0` (traditional) takes it for every insert-like statement, `1` (consecutive) only for bulk inserts such as `INSERT ... SELECT` and `LOAD DATA`, and `2` (interleaved) never. Mode 2 is the default in MySQL 8.4 (since 8.0.3) because it is the fastest and row-based binary logging, the default format, does not care in which order concurrent statements took values. The cost is that one bulk statement's values may be non-consecutive and interleaved with others', which is unsafe for statement-based replication. The variable is global and can only be set at startup.
code
ini · 5 lines[mysqld]
# Read only at startup: innodb_autoinc_lock_mode is not dynamic.
# 2 (interleaved) is already the MySQL 8.4 default; set 1 only if
# something still replays statement-based binary logs.
innodb_autoinc_lock_mode = 2go deeper
Recall that AUTO_INCREMENT values can have gaps and that a rolled-back insert never gives its value back.
Explain the AUTO-INC table lock, the three modes in terms of simple and bulk inserts, and why 2 became the default.
Spot code that assumes contiguous ids from one statement, and check binlog format before anyone sets interleaved mode on a statement-logging source.
Treat id contiguity as an explicit non-guarantee in data contracts, and retire statement-based logging rather than pinning slower lock modes for it.
## What the AUTO-INC lock is InnoDB, MySQL's default storage engine, needs to hand out `AUTO_INCREMENT` values to concurrent inserts. The column must be the first or only column of some index so InnoDB can find the current maximum. The heavyweight tool for this is the **AUTO-INC lock**: a special **table-level** lock that serialises inserts into one table so a statement's rows receive consecutive values. It is held until the **end of the statement**, not the end of the transaction, and it shows up in `performance_schema.data_locks` with `LOCK_MODE` `AUTO_INC`. The server variable `innodb_autoinc_lock_mode` decides when that lock is used. It is **global**, **not dynamic** (set it in the option file and restart) and takes `0`, `1` or `2`. ## Three kinds of insert The modes are defined in terms of how many rows a statement will insert: - **Simple inserts**: the row count is known up front, such as single-row and multi-row `INSERT` or `REPLACE` without a nested subquery (not `INSERT ... ON DUPLICATE KEY UPDATE`). - **Bulk inserts**: the count is not known in advance: `INSERT ... SELECT`, `REPLACE ... SELECT` and `LOAD DATA`. InnoDB assigns values one at a time as rows arrive. - **Mixed-mode inserts**: simple inserts that supply some values and leave others to the counter, such as `INSERT INTO t1 (c1,c2) VALUES (1,'a'), (NULL,'b'), (5,'c'), (NULL,'d')`, plus `INSERT ... ON DUPLICATE KEY UPDATE`. ## The three modes | Mode | Name | AUTO-INC table lock | Values per statement | Safe for statement-based replication | |---|---|---|---|---| | `0` | traditional | every insert-like statement | consecutive | yes | | `1` | consecutive | bulk inserts only; simple inserts use a light mutex for the allocation | consecutive | yes | | `2` | interleaved | never | unique and increasing, but a bulk statement's values can have gaps and interleave with others' | no | In mode 1 a simple insert still waits if another transaction already holds the AUTO-INC lock. Mode 0 exists mainly for backward compatibility, performance testing and working around mixed-mode insert differences. ## Why 2 is the default MySQL 8.4 defaults to `2`, as every release since 8.0.3 has; earlier releases defaulted to `1`. The manual ties the change to row-based replication being the default replication type. With statement-based logging a replica replays the `INSERT` text, so it must generate exactly the same values in exactly the same order, which only the statement-long lock of modes 0 and 1 guarantees. With **row-based logging** the binary log carries the actual row values, so the order in which concurrent statements drew numbers no longer matters and the table-level lock is pure cost. In 8.4 binary logging is on by default in row format, and `binlog_format` itself is deprecated, which makes the interleaved default the natural fit. The practical effect is concurrency: under modes 0 and 1 a long `INSERT ... SELECT` holds the AUTO-INC lock for its whole run and every other insert into that table queues behind it. Under mode 2 they proceed side by side, which is why the manual's bulk-loading advice is to use 2 rather than 1. To see what a server is actually running, query `SELECT @@GLOBAL.innodb_autoinc_lock_mode`; an upgraded server can still carry an old `1` pinned in its option file. ## What you give up, and what is true in every mode 1. **Gaps and interleaving under mode 2.** A bulk insert running concurrently with other inserts can receive non-consecutive values. Code that assumes one statement's ids form a contiguous block (computing them from the first id plus the row count) is wrong under mode 2. 2. **Statement-based replication.** If a source still logs statements, use mode 0 or 1 and the same value on source and replicas; with row-based or mixed logging every mode is safe. 3. **Lost values in all modes.** A value handed out is never given back, whether the statement fails or the transaction rolls back, so gaps exist in every mode. 4. **Over-allocation.** In modes 1 and 2 a mixed-mode insert allocates more values than it uses; the excess is lost. In the manual's example, with the last value 100, the four-row statement above leaves the next value at 105 in mode 1, against 103 in mode 0. Column types, counter exhaustion and how `AUTO_INCREMENT` behaves as a schema feature belong with MySQL's types and online DDL; this lock mode is about how concurrent inserts are serialised.
- Why does mode 2 break code that derives a bulk insert's ids from the first id and the row count?In interleaved mode no table-level lock serialises bulk inserts, so while an `INSERT ... SELECT` runs, concurrent inserts draw values from the same counter. The bulk statement's ids stay unique and increasing but can have holes, so first-id-plus-count arithmetic can name rows the statement never inserted. Read the ids back, or insert with an explicit key you control.
- Do modes 0 and 1 guarantee an AUTO_INCREMENT column with no gaps?No. They guarantee consecutive values within one statement, but in every mode a value is lost when its statement fails or its transaction rolls back, and modes 1 and 2 over-allocate for mixed-mode inserts. Gap-free numbering, such as invoice numbers, needs its own locked counter row, not `AUTO_INCREMENT`.
- The AUTO-INC lock is held until the inserting transaction commits.
- innodb_autoinc_lock_mode can be switched at runtime with SET GLOBAL.
- Lock mode 1 guarantees AUTO_INCREMENT values never have gaps.
- Interleaved mode can hand two rows the same AUTO_INCREMENT value.
- Interleaved mode is safe however the binary log is formatted.
Assumes MySQL 8.4, where innodb_autoinc_lock_mode defaults to 2 (interleaved), the default since 8.0.3; releases before 8.0.3 defaulted to 1 (consecutive).