Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 86

Transaction Isolation Levels in MySQL

What Is a Transaction Isolation Level?

Isolation is the ACID property that controls how concurrent transactions observe and affect shared data. The isolation level chooses a defined balance among reproducible reads, lock behavior, concurrency and overhead. It does not simply mean “safe versus unsafe” or “fast versus slow.”

Faculty definition: A transaction isolation level specifies which effects of other concurrent transactions can become visible and which conflicting operations must wait.

InnoDB supports all four SQL isolation levels: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ and SERIALIZABLE. Its default is REPEATABLE READ.

Reproducible Two-Session Lab

Create the table once, outside the test transactions. Then open two MySQL connections named Session A and Session B.

DROP TABLE IF EXISTS isolation_demo;
CREATE TABLE isolation_demo (
  item_id INT PRIMARY KEY,
  item_name VARCHAR(40) NOT NULL,
  quantity INT NOT NULL,
  INDEX idx_quantity (quantity)
) ENGINE = InnoDB;

INSERT INTO isolation_demo VALUES
(1, 'Notebook', 100),
(2, 'Pen',       20);
COMMIT;
1 | Notebook | 100 2 | Pen | 20

Reset quantity to 100 and commit before each independent experiment. Timing matters, so execute the labelled statements in the shown session order.

Dirty Read, Nonrepeatable Read and Phantom

PhenomenonWhat changes?Simple example
Dirty readUncommitted value is observedB sees A's 150, then A rolls it back
Nonrepeatable readSame row returns a different committed valueB sees 100, A commits 110, B sees 110
PhantomSame predicate returns a different row setA inserts a qualifying row between B's searches
Lost updateOne writer overwrites another decisionTwo applications calculate from the same old stock

Dirty, nonrepeatable and phantom reads describe visibility. Lost updates are prevented through correct update patterns, locking or optimistic version checks; an isolation label alone should not replace application design.

The Four InnoDB Isolation Levels

LevelPlain consistent-read behaviorImportant InnoDB point
READ UNCOMMITTEDCan expose an earlier or uncommitted versionDirty reads are possible; otherwise broadly resembles READ COMMITTED
READ COMMITTEDEvery consistent read uses a fresh snapshotRecord locking is reduced; gap locking remains mainly for foreign-key and duplicate-key checks
REPEATABLE READConsistent reads reuse the snapshot established by the first readDefault; range locking reads can use gap or next-key locks
SERIALIZABLELike REPEATABLE READ with stricter locking for plain reads when autocommit is disabledPlain SELECT is implicitly treated like SELECT FOR SHARE in that case
Correction to a common chart: “SERIALIZABLE is always slowest” is not a usable engineering rule. Cost depends on workload, contention, predicates, indexes and transaction length. Measure the actual system.

Two Verified Snapshot Experiments

READ COMMITTED: second read gets a fresh snapshot

-- Session B
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
START TRANSACTION;
SELECT quantity FROM isolation_demo WHERE item_id = 1;
-- 100

-- Session A, between B's two reads
START TRANSACTION;
UPDATE isolation_demo SET quantity = 110 WHERE item_id = 1;
COMMIT;

-- Session B
SELECT quantity FROM isolation_demo WHERE item_id = 1;
-- 110
COMMIT;
Session B: first read=100, second read=110

REPEATABLE READ: plain reads reuse one snapshot

-- Reset and commit quantity=100 first.
-- Session B
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
START TRANSACTION;
SELECT quantity FROM isolation_demo WHERE item_id = 1;
-- 100; snapshot established.

-- Session A
START TRANSACTION;
UPDATE isolation_demo SET quantity = 110 WHERE item_id = 1;
COMMIT;

-- Session B
SELECT quantity FROM isolation_demo WHERE item_id = 1;
-- Still 100 in the consistent-read snapshot.
COMMIT;
Session B: first read=100, second plain read=100

A locking read such as SELECT FOR UPDATE reads the current version and takes locks, not the old consistent-read snapshot. Avoid casually mixing locking and nonlocking reads in one REPEATABLE READ transaction because they can represent different database states.

Set Global, Session or Next-Transaction Scope

-- Inspect current session value.
SELECT @@SESSION.transaction_isolation;

-- Every subsequent transaction in this session.
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;

-- Only the next transaction; execute outside a transaction.
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
SELECT COUNT(*) FROM isolation_demo;
COMMIT;

-- Server-wide default for future sessions; admin privilege required.
SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  • A GLOBAL change affects subsequent connections, not existing sessions.
  • A SESSION change affects subsequent transactions in that connection, not an already active transaction.
  • An unqualified SET TRANSACTION applies only to the next transaction.
  • Set the level before START TRANSACTION when using next-transaction scope.
  • Connection pools should establish session settings deliberately when a connection is checked out.

How to Choose an Isolation Level

Workload needStarting consideration
General InnoDB OLTPKeep REPEATABLE READ unless evidence supports a change
Fresh committed view on each report statementEvaluate READ COMMITTED
Read a value and then modify itUse an appropriate locking read or atomic conditional update
Queue workersConsider locking reads with SKIP LOCKED only for queue-like data
Strict serial behavior for a specialized unitEvaluate SERIALIZABLE and measure contention
Approximate reporting where dirty values are truly acceptableREAD UNCOMMITTED only after explicit risk review
  1. Define the anomaly that would violate the business rule.
  2. Choose the minimum correct combination of isolation, locks and constraints.
  3. Keep transactions small and predicates indexed.
  4. Test with two concurrent sessions, not only single-user data.
  5. Handle deadlocks and lock timeouts explicitly.
  6. Benchmark representative production volume before changing defaults.

Exam Answer and Practical Tasks

Model answer: An isolation level determines the visibility of concurrent transaction changes. InnoDB supports READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ and SERIALIZABLE. READ UNCOMMITTED can permit dirty reads. READ COMMITTED creates a fresh consistent-read snapshot per statement. REPEATABLE READ, the InnoDB default, reuses the first-read snapshot for consistent reads. SERIALIZABLE applies stricter locking behavior. The correct choice depends on required anomalies, locking and workload.

Practice: reproduce one dirty read and roll it back; compare two reads under READ COMMITTED and REPEATABLE READ; insert a range-matching row between two count queries; then repeat using a locking range read and observe whether the insert waits.

Continue to concurrency control and locks and deadlocks.

Official References

Snapshot, locking and scope behavior were checked against the official MySQL 8.4 manual.

Frequently Asked Questions

What is the default InnoDB isolation level in MySQL 8.4?
REPEATABLE READ is the default. Plain consistent reads in one transaction use the snapshot established by its first consistent read.
What is the difference between READ COMMITTED and REPEATABLE READ?
At READ COMMITTED each consistent read gets a fresh snapshot. At REPEATABLE READ consistent reads in the transaction normally reuse the first-read snapshot.
Can READ UNCOMMITTED show data that is later rolled back?
Yes. A dirty read can observe an uncommitted version from another transaction, so the value may disappear after that transaction rolls back.
Does a higher isolation level automatically make every application correct?
No. Isolation controls concurrency visibility and locking. Correct transaction boundaries, constraints, affected-row checks and application rules are still required.
When must SET TRANSACTION ISOLATION LEVEL be executed?
Without SESSION or GLOBAL it configures only the next transaction and must be issued outside an active transaction. SET SESSION TRANSACTION affects subsequent transactions in that session.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.