InnoDB early lock release

InnoDB early lock release is a feature in MySQL 8 and higher that allows the database to release locks earlier in the transaction processing process. This helps to reduce lock contention and improve overall performance. With early lock release, InnoDB can release locks for modified rows after the data changes have been recorded in the redo log buffer, but before the changes are flushed to disk. This allows other transactions to access the updated data more quickly, which can help to reduce wait times and improve overall performance. The early lock release feature can be controlled using the innodb_early_lock_release system variable.

How does early lock release in InnoDB affect performance?

Early lock release in InnoDB can positively affect performance by reducing the amount of time transactions have to wait to acquire locks on database pages. When early lock release is used, InnoDB releases locks as soon as it’s safe to do so, rather than waiting until the end of the transaction. This can reduce the amount of lock contention and lead to improved parallelism and overall database performance. However, early lock release also has some trade-offs, such as increased complexity in the lock management system and the potential for decreased consistency in the event of transaction failures. It’s important to carefully consider the trade-offs and make an informed decision when enabling early lock release in InnoDB.

How to implement early lock release in MySQL 8?

In MySQL 8, the early lock release feature can be implemented by setting the innodb_locks_unsafe_for_binlog system variable to 1. This allows InnoDB to release locks early, reducing the contention and lock wait times.

However, it should be noted that enabling this feature can cause data inconsistency issues in some cases, especially when used with statement-based replication. It is recommended to use row-based replication and to thoroughly test the setup before enabling the feature in a production environment.

Here’s a sample code to enable early lock release in MySQL 8:

This setting can also be added to the my.cnf configuration file for persistence across restarts.

Real-Time MySQL 8 Lock Monitor

The performance_schema.data_locks table in MySQL 8’s Performance Schema provides information about the data locks that threads have acquired in the server. This table is populated with data whenever a thread acquires a data lock, such as a lock on a row, table or database. The table contains columns that describe the lock, including:

  • THREAD_ID: The ID of the thread that holds the lock.
  • EVENT_NAME: The name of the event or process that is acquiring the lock.
  • OBJECT_SCHEMA: The schema of the database object being locked.
  • OBJECT_NAME: The name of the database object being locked.
  • OBJECT_TYPE: The type of the database object being locked (e.g. table, database, row).
  • LOCK_TYPE: The type of lock being acquired (e.g. read, write, exclusive).
  • LOCK_MODE: The mode of the lock being acquired (e.g. shared, exclusive).
  • LOCK_DURATION: The duration of the lock in microseconds.
  • LOCK_STATUS: The status of the lock (e.g. granted, waiting).
  • SQL_TEXT: The SQL statement that caused the lock.

You can download the real-time MySQL lock monitor script(Python Script) from GitHub – https://github.com/shiviyer/MySQL-Toolkit/blob/main/mdb-mylock.py

About Shiv Iyer 455 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.