Row Locks
InnoDB locks rows (strictly, index records), not whole tables, for normal changes. UPDATE and DELETE take an exclusive lock on the rows they change, and the lock lasts until the transaction ends.
Writers Block Writers
Session A updates Asha's row without committing. Session B tries to update the same row and waits. After A commits, B continues from the new value, so no update is lost. This is real output of two connections:
Asha: 100 - 30 + 5 = 75, correct.
Lock Wait Timeout
B does not wait forever. After innodb_lock_wait_timeout seconds (50 by default) MySQL gives up with error 1205. This test set it to 2 seconds. Only the statement fails; the transaction stays open, so the application should roll back and retry:
Result:
| default_timeout_seconds |
|---|
| 50 |
SELECT ... FOR UPDATE
A plain SELECT takes no lock. To read a row and be sure nobody changes it before your update (check-then-act), lock it while reading:
Often an atomic statement is better and needs no explicit lock:
Result:
| rows_changed |
|---|
| 1 |
If rows_changed is 0 there was not enough stock. This is the safest pattern for counters and inventory.
Gap and Next-Key Locks
Under REPEATABLE READ, a locking range query also locks the gaps between index records, to stop phantom rows. The table here holds ids 1, 2, 10, 20. Session A locks the range 5 to 15 and session B tries to insert id 12 (which lies in the gap):
The insert was blocked until it timed out, although id 12 does not exist. With READ COMMITTED there are no gap locks for these statements:
READ COMMITTED removes most of them, at the price of phantom rows. Check your application's needs before changing it.
NOWAIT and SKIP LOCKED (8.0+)
| Option | Meaning |
|---|---|
NOWAIT | Fail immediately (error 3572) if a row is locked |
SKIP LOCKED | Skip locked rows and return only free ones |
SKIP LOCKED turns a table into a work queue: many workers each grab different jobs with no waiting. Session A has row 1 locked:
Seeing Locks
Two views help when something hangs. Both exist in MySQL 8:
Result:
| data_locks_view_exists |
|---|
| 1 |
Table Locks
LOCK TABLES t WRITE locks a whole table. It is rarely needed with InnoDB and blocks others heavily. Prefer row-level methods. FLUSH TABLES WITH READ LOCK freezes all writes and is used by some backup tools.
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Long transactions holding locks | Everyone queues | Keep them short |
FOR UPDATE on a non-indexed column | Locks many rows or the whole table | Lock by indexed key |
| Read, check, then write without a lock | Lost updates, oversold stock | Atomic UPDATE ... WHERE stock >= n or FOR UPDATE |
| Ignoring error 1205 | Failing requests | Roll back and retry |
| Locking in different order in different code | Deadlocks | Same order everywhere |
Practice
- Reproduce the writer-blocks-writer test with two
mysqlwindows. - Lower
innodb_lock_wait_timeoutfor one session and trigger error 1205. - Write an atomic stock decrement that never goes below zero.
- Design a jobs table and a worker query with
SKIP LOCKED.
Conclusion
InnoDB uses row locks that last until the end of the transaction. REPEATABLE READ adds gap locks, READ COMMITTED does not, FOR UPDATE locks what you read, and NOWAIT and SKIP LOCKED make queues and fast failures possible.

