Databases 14 min read

Understanding MySQL Locks: InnoDB Row Locks vs MyISAM Table Locks and Deadlock Prevention

This article explains the different lock mechanisms used by MySQL storage engines—including MyISAM table‑level locks and InnoDB row‑level locks—covers lock compatibility, scheduling, transaction properties, optimistic locking, and practical strategies to avoid lock conflicts and deadlocks.

Architect's Alchemy Furnace
Architect's Alchemy Furnace
Architect's Alchemy Furnace
Understanding MySQL Locks: InnoDB Row Locks vs MyISAM Table Locks and Deadlock Prevention

MySQL Lock Overview

MySQL is indispensable in development, and beyond basic CRUD operations, understanding its locking mechanisms is essential for building robust applications.

Storage Engines and Their Locks

MySQL provides several storage engines, the most common being InnoDB (default), MyISAM, and the third‑party TokuDB, each implementing locks differently.

MyISAM Table Locks

MyISAM supports only table‑level locks, which come in two modes:

Table read lock (shared)

Table write lock (exclusive)

Compatibility matrix (simplified): read‑lock is compatible with another read‑lock, but any write‑lock conflicts with both read and write locks.

When a thread acquires a write lock on a MyISAM table, all other threads must wait for the lock to be released, making read and write operations serial.

MyISAM automatically acquires a read lock before a SELECT and a write lock before INSERT, UPDATE, or DELETE. Explicit LOCK TABLE statements can also be used.

Lock Scheduling in MyISAM

Write requests are given priority over reads; even if a read request arrives first, a later write request will be placed ahead in the queue. This behavior can cause severe contention in write‑heavy workloads. MySQL offers parameters to adjust the priority:

Start‑up option low‑priority‑updates to favor reads.

Session variable SET LOW_PRIORITY_UPDATES=1.

Using the LOW_PRIORITY attribute on INSERT, UPDATE, or DELETE statements.

InnoDB Row Locks

InnoDB differs from MyISAM in two major ways: it supports transactions and uses row‑level locking.

Transaction ACID properties:

Atomicity : all statements succeed or none do.

Consistency : data remains consistent before and after the transaction.

Isolation : concurrent transactions do not interfere with each other.

Durability : committed changes survive crashes.

InnoDB provides two row‑lock types:

Shared lock (S) : allows a transaction to read a row while preventing others from acquiring an exclusive lock on the same row.

Exclusive lock (X) : allows a transaction to modify a row and blocks both shared and exclusive locks from other transactions.

InnoDB also uses intention locks (table‑level) to coordinate row‑level locking:

Intention shared (IS)

Intention exclusive (IX)

Compatibility matrix (simplified):

IX is compatible with IS and other IX locks.

S is compatible with IS.

X conflicts with all other lock types.

Row locks are granted automatically based on index usage; without an index, InnoDB falls back to a table lock.

SELECT * FROM table_name WHERE ... LOCK IN SHARE MODE   // shared lock (S)
SELECT * FROM table_name WHERE ... FOR UPDATE           // exclusive lock (X)

Using SELECT ... IN SHARE MODE is suitable when you need to ensure a row exists without allowing updates, while SELECT ... FOR UPDATE should be used when the transaction intends to modify the row.

Optimistic Locking in MySQL

Commonly implemented by adding a version or timestamp column and updating it on each modification, allowing concurrent updates to be detected and resolved.

Optimistic lock illustration
Optimistic lock illustration

Controlling the version field effectively solves resource‑contention problems.

Deadlock Overview

Deadlocks usually stem from application design. Common mitigation techniques include:

Access tables in a consistent order across all sessions.

Sort data before batch processing to enforce a fixed order.

Acquire the highest required lock level (e.g., exclusive) directly instead of upgrading from shared.

Use a lower isolation level (e.g., READ COMMITTED) to reduce lock wait scenarios.

Handle deadlock errors gracefully and retry transactions.

When a deadlock occurs, SHOW ENGINE INNODB STATUS can reveal the cause.

Summary

MyISAM table locks:

Shared read locks are compatible; read‑write and write‑write locks conflict, making reads and writes serial.

Write‑priority scheduling can cause read starvation; adjust with LOW_PRIORITY settings.

Heavy write workloads may suffer severe lock contention; consider switching to InnoDB.

InnoDB row locks:

Row locks are based on index entries; without an index, a table lock is used.

Intention locks coordinate row‑level locking.

Isolation level and gap‑lock mechanisms affect lock behavior.

Deadlocks are hard to eliminate; design, indexing, transaction size, and proper lock acquisition reduce risk.

By understanding these mechanisms and applying best‑practice strategies—such as using appropriate isolation levels, designing effective indexes, keeping transactions short, and acquiring sufficient lock granularity—developers can minimize lock conflicts and improve MySQL performance.

InnoDBMySQLmyisamLocks
Architect's Alchemy Furnace
Written by

Architect's Alchemy Furnace

A comprehensive platform that combines Java development and architecture design, guaranteeing 100% original content. We explore the essence and philosophy of architecture and provide professional technical articles for aspiring architects.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.