Isolation levels and anomalies
You can choose transaction isolation levels by understanding which anomalies they prevent and how MySQL InnoDB snapshots and locking reads affect concurrent order operations.
Learning outcomes
- Explain how MySQL 8.4 InnoDB implements READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ and SERIALIZABLE.
- Distinguish consistent reads from locking reads and explain when InnoDB takes row locks and next-key locks.
- Identify dirty writes, dirty reads, non-repeatable reads, read skew, phantom reads, lost updates and write skew in concurrent order operations.
- Explain why REPEATABLE READ does not prevent lost updates or write skew in MySQL.
- Choose appropriate isolation levels and protect read-dependent writes with conditional updates or locking.
Concurrent changes to an order
Consider order 42. Its current status is PLACED. A restaurant employee accepts it while the customer cancels it.
Both requests read the same status. Both decide that their transition is legal. Each updates the order and inserts an order event. If both transactions commit, the final status is CANCELLED, even though the employee's acceptance also returned success.
The problem is not that either request used an invalid transition. The problem is that each transaction made its decision using a view of the data that could become outdated before its write.
Transaction isolation
Isolation is the I in ACID. Without it, one transaction can read or modify data that another transaction is still working on. This can produce inconsistent results when concurrent operations interact.
MySQL InnoDB provides four SQL-standard isolation levels: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ and SERIALIZABLE. You can configure the level globally or for an individual session. Each level makes a different trade-off between performance and consistency.
You already know how transactions group database operations into one unit of work. Isolation determines how those operations interact with concurrent transactions. It controls which changes you can see and how conflicting operations behave.
The snapshot model
MySQL InnoDB uses multi-version concurrency control (MVCC) for consistent reads. Instead of making every plain SELECT wait for concurrent writers, InnoDB can return an appropriate committed version of a row from its snapshot.
A plain SELECT is a consistent read. It does not acquire row locks.
SELECT status
FROM orders
WHERE id = 42;The important detail is when InnoDB creates the snapshot.
Under REPEATABLE READ, the first consistent read creates it. BEGIN does not create the snapshot by itself. Every later plain SELECT in the same transaction reuses that snapshot.
Under READ COMMITTED, every consistent read gets a fresh snapshot. A commit by another transaction can therefore become visible between two SELECT statements.


This distinction matters when a handler reads an order and then reads its event history. You want those reads to describe a consistent view of the same point in time.
There is also a second category of reads. A locking read requests locks rather than relying only on the transaction's consistent-read snapshot.
SELECT status
FROM orders
WHERE id = 42
FOR UPDATE;FOR UPDATE takes an exclusive lock on the matching record. FOR SHARE takes a shared lock. Both are locking reads, and both read the latest committed row version.
UPDATE and DELETE also read and lock the latest committed row versions. They do not use the old consistent-read snapshot to decide which row version to modify.
This difference explains several surprising behaviours under REPEATABLE READ.
The four isolation levels
READ UNCOMMITTED
READ UNCOMMITTED is the lowest isolation level. It uses minimal locking for reads, which can make it fast, but it allows dirty reads.
A dirty read happens when a transaction reads changes another transaction has not committed.
-- T1
UPDATE orders
SET status = 'ACCEPTED'
WHERE id = 42;
-- T2: READ UNCOMMITTED
SELECT status
FROM orders
WHERE id = 42;
-- ACCEPTED
-- T1
ROLLBACK;T2 reads ACCEPTED even though T1 later rolls back. The committed status never changed from PLACED, but T2 temporarily observed a value that was not committed.
Writes still acquire exclusive row locks. Dirty writes are therefore prevented even at this level.
READ UNCOMMITTED is rarely used in production. A dashboard or handler that reads an order status should not report a change that might disappear immediately after a rollback.
READ COMMITTED
READ COMMITTED prevents dirty reads. A plain SELECT sees committed data as of the beginning of that query.
Each consistent read creates a fresh snapshot, including when several SELECT statements execute inside the same transaction.
-- T2: READ COMMITTED
SELECT status FROM orders WHERE id = 42;
-- PLACED
-- T1
UPDATE orders
SET status = 'ACCEPTED'
WHERE id = 42;
COMMIT;
-- T2
SELECT status FROM orders WHERE id = 42;
-- ACCEPTEDThe second SELECT can return a different status from the first. This is a non-repeatable read.
InnoDB does not use gap locking for ordinary searches and index scans at this level. Gap locks remain available for foreign-key constraint checks and duplicate-key checks.
UPDATE and DELETE still acquire exclusive record locks. Locking reads acquire the requested locks on the matching records.
READ COMMITTED is the default in many other database systems, though not in MySQL. It is often recommended for Galera and InnoDB Cluster deployments because it balances consistency and concurrency.
You might choose it when you want each statement to see recent committed data and do not need repeated reads within one transaction to share a snapshot.
REPEATABLE READ
REPEATABLE READ is the default isolation level in MySQL 8.4 InnoDB.
Once a transaction reads data through a consistent read, subsequent consistent reads continue to see the same snapshot. The first SELECT creates the snapshot. Every later plain SELECT in the transaction reuses it.
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
START TRANSACTION;
SELECT status FROM orders WHERE id = 42;
-- Another transaction commits an update.
SELECT status FROM orders WHERE id = 42;
COMMIT;Both SELECT statements return the same status, even if another transaction committed a change between them.
You can also create the snapshot immediately when starting a transaction:
START TRANSACTION WITH CONSISTENT SNAPSHOT;This differs from a normal START TRANSACTION, which does not create the snapshot until the first consistent read.
REPEATABLE READ eliminates non-repeatable reads for plain SELECT statements. It also handles phantom reads differently from the SQL standard's general description. Although the standard permits phantoms at this level, InnoDB uses MVCC to keep repeated plain SELECT statements on the same snapshot.
Keeping snapshots available can add overhead in highly concurrent systems because InnoDB must retain older row versions while transactions still need them.
Locking reads, UPDATE and DELETE behave differently. InnoDB reads and locks the latest committed row version for these operations.
For range searches and non-unique index conditions, these operations can acquire next-key locks. A next-key lock covers an index record and the gap immediately before it. This prevents another transaction from inserting a row into the scanned range.
A unique-index equality lookup locks only the matching record.
REPEATABLE READ is a sensible default for this course. You get consistent snapshots for multi-query reads without needing every plain SELECT to acquire locks. You still need explicit protection when a write depends on an earlier read.
SERIALIZABLE
SERIALIZABLE is the strongest isolation level. It behaves like REPEATABLE READ but adds stricter locking.
When autocommit is disabled, InnoDB converts every plain SELECT into a shared locking read, equivalent to SELECT ... FOR SHARE.
SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
SELECT status
FROM orders
WHERE id = 42;
COMMIT;The SELECT acquires a shared lock. A conflicting writer cannot modify the locked record until the reader finishes.
This prevents dirty reads, non-repeatable reads and phantom reads. More generally, transactions behave as though they ran one after another rather than concurrently.
However, stricter locking reduces concurrency. Consider two transactions that both read the same order using shared locks. Both then attempt to update it. Each transaction holds a shared lock that conflicts with the other transaction's requested exclusive lock.
Neither can proceed. InnoDB detects the deadlock, rolls one transaction back and returns error 1213.
Your application must retry the failed transaction when that is safe.
Amazon Aurora does not support SERIALIZABLE in exactly the same way as standard MySQL. Check the behaviour of your specific Aurora version before relying on MySQL's locking assumptions.
SERIALIZABLE is useful when the consistency requirements justify its locking cost. It is not a free upgrade over REPEATABLE READ.
Write conflicts and anomalies
The following scenarios use two concurrent transactions, T1 and T2. Each anomaly describes a different way their operations can interact.
Dirty writes
A dirty write happens when one transaction overwrites another transaction's uncommitted change.
-- T1
UPDATE orders
SET status = 'ACCEPTED'
WHERE id = 42;
-- T2
UPDATE orders
SET status = 'CANCELLED'
WHERE id = 42;
-- Waits for T1.
-- T1
COMMIT;
-- T2
-- The UPDATE proceeds.
COMMIT;InnoDB prevents this at every isolation level.
An UPDATE takes an exclusive row lock and holds it until COMMIT or ROLLBACK. T2 must wait for T1 to release that lock before it can update order 42.


The second update can still overwrite the first committed value. That is a lost update, not a dirty write.
Dirty reads
A dirty read happens when a transaction reads another transaction's uncommitted changes.
Suppose the restaurant dashboard counts PLACED orders for restaurant 3. T1 inserts order 43 but has not committed yet.
-- T1
INSERT INTO orders (id, restaurant_id, status)
VALUES (43, 3, 'PLACED');
-- T2: READ UNCOMMITTED
SELECT COUNT(*)
FROM orders
WHERE restaurant_id = 3
AND status = 'PLACED';
-- Returns 5, including order 43.
-- T1
-- The outbox insert fails.
ROLLBACK;The dashboard showed an order that never existed in committed data. T2 counted an uncommitted insert that disappeared when T1 rolled back.


Only READ UNCOMMITTED allows dirty reads. READ COMMITTED, REPEATABLE READ and SERIALIZABLE prevent them.
Non-repeatable reads
A non-repeatable read happens when the same row returns different values across two reads in one transaction.
-- T1: READ COMMITTED
SELECT status FROM orders WHERE id = 42;
-- PLACED
-- T2
UPDATE orders
SET status = 'ACCEPTED'
WHERE id = 42;
COMMIT;
-- T1
SELECT status FROM orders WHERE id = 42;
-- ACCEPTEDT1 first saw PLACED and then saw ACCEPTED. Another transaction committed an update between its two statements.


READ UNCOMMITTED and READ COMMITTED allow non-repeatable reads. REPEATABLE READ and SERIALIZABLE prevent them for consistent reads.
Read skew
Read skew occurs when separate reads in one transaction return individually valid data that does not belong to the same database snapshot.
Consider GET /v1/orders/42. The handler reads the order and then its event history.
-- T1: READ COMMITTED
SELECT status FROM orders WHERE id = 42;
-- PLACED
-- T2
UPDATE orders
SET status = 'ACCEPTED'
WHERE id = 42;
INSERT INTO order_events
(order_id, from_status, to_status, actor)
VALUES
(42, 'PLACED', 'ACCEPTED', 'restaurant');
COMMIT;
-- T1
SELECT to_status
FROM order_events
WHERE order_id = 42
ORDER BY id;
-- PLACED, ACCEPTEDT1 returns an order with status PLACED, but its history ends in ACCEPTED. Both queries returned committed data. The problem is that they observed different snapshots.


READ UNCOMMITTED and READ COMMITTED allow this anomaly. REPEATABLE READ and SERIALIZABLE keep both plain SELECT statements on the same snapshot.
For this API, REPEATABLE READ gives you a consistent view of the order and its event history.
Phantom reads
A phantom read happens when a repeated search using the same predicate returns a different set of rows.
Suppose a restaurant dashboard counts its PLACED orders and then lists them.
-- T1: READ COMMITTED
SELECT COUNT(*)
FROM orders
WHERE restaurant_id = 3
AND status = 'PLACED';
-- 4
-- T2
INSERT INTO orders (id, restaurant_id, status)
VALUES (43, 3, 'PLACED');
COMMIT;
-- T1
SELECT *
FROM orders
WHERE restaurant_id = 3
AND status = 'PLACED';
-- 5 rowsT1 counted four orders but then found five matching rows. The new order is a phantom.


READ UNCOMMITTED and READ COMMITTED allow phantom reads. A plain SELECT under REPEATABLE READ continues to see four rows because it reuses its original snapshot.
A locking read provides a different form of protection:
-- T1: REPEATABLE READ
SELECT *
FROM orders
WHERE restaurant_id = 3
AND status = 'PLACED'
FOR UPDATE;The query uses idx_orders_rest_status. InnoDB takes next-key locks across the scanned range. T2's matching INSERT must wait until T1 finishes.
SERIALIZABLE also prevents the phantom through its locking behaviour.
Lost updates
A lost update occurs when two transactions read the same value, independently calculate valid changes and then overwrite each other's results.
Order 42 starts as PLACED. T1 represents restaurant staff accepting the order. T2 represents the customer cancelling it.
Both transitions are legal from PLACED in Go.
-- T1
SELECT status FROM orders WHERE id = 42;
-- PLACED
-- T2
SELECT status FROM orders WHERE id = 42;
-- PLACED
-- T1
UPDATE orders
SET status = 'ACCEPTED'
WHERE id = 42;
COMMIT;
-- T2
UPDATE orders
SET status = 'CANCELLED'
WHERE id = 42;
COMMIT;Both UPDATE statements succeed. Both callers can receive success, and both can write an order event. The final status is CANCELLED. T1's acceptance has been silently overwritten.


This can happen under READ COMMITTED and REPEATABLE READ in MySQL. The initial plain SELECT uses a snapshot, but the later UPDATE reads and locks the latest committed row version. It does not check whether that version is still the one the application originally read.
SERIALIZABLE prevents this pattern through shared locks on the initial reads. When both transactions attempt to write, a deadlock occurs and InnoDB rolls one back with error 1213.
There are also more targeted solutions:
- Use a conditional UPDATE with
WHERE id = 42 AND version = 1. Only the transaction updating the expected version succeeds. - Use
SELECT ... FOR UPDATEbefore deciding which transition to apply. - Use SERIALIZABLE when the broader transaction requires serializable behaviour.
Write skew
Write skew occurs when two transactions read shared information, make decisions based on that information and update different rows.
Suppose rider 9 is allowed to carry at most one order with status PICKED_UP. Orders 41 and 42 are both READY. The rider currently carries none.
-- T1
SELECT COUNT(*)
FROM orders
WHERE rider_id = 9
AND status = 'PICKED_UP';
-- 0
-- T2
SELECT COUNT(*)
FROM orders
WHERE rider_id = 9
AND status = 'PICKED_UP';
-- 0
-- T1
UPDATE orders
SET rider_id = 9, status = 'PICKED_UP'
WHERE id = 41;
-- T2
UPDATE orders
SET rider_id = 9, status = 'PICKED_UP'
WHERE id = 42;
-- Both commit.Both transactions read a snapshot in which rider 9 carries zero orders. They independently conclude that assigning another order is valid. They update different rows, so their UPDATE statements do not conflict.
After both commits, rider 9 carries two orders. The shared rule has been violated even though neither transaction individually broke it based on its snapshot.


READ UNCOMMITTED, READ COMMITTED and REPEATABLE READ allow this anomaly. In particular, REPEATABLE READ does not automatically protect a business rule that spans multiple rows.
Write skew is prevented by SERIALIZABLE. In this scenario, each SELECT becomes a shared locking read. Since there is no index on rider_id, the searches scan and share-lock the rows. The subsequent updates conflict with the other transaction's shared locks. InnoDB detects a deadlock and rolls one transaction back with error 1213.
You can also protect the rule directly by locking a row that represents the rider. Riders are stored as users with the rider role.
-- T1
SELECT id
FROM users
WHERE id = 9
FOR UPDATE;Both assignment transactions must acquire that lock before checking the rider's current assignments. The second transaction waits until the first finishes, then checks the updated state.
Anomaly matrix
The following matrix summarizes which isolation levels allow each anomaly in MySQL 8.4 InnoDB. Dirty writes are prevented by exclusive row locks at every level.


Notice that REPEATABLE READ provides stronger snapshot consistency than READ COMMITTED, but it does not automatically enforce every business rule.
Choosing an isolation level
No single isolation level fits every situation. Lower isolation levels generally offer better performance but allow more anomalies. Higher isolation levels provide stronger consistency but can reduce concurrency.
Systems that prioritise data accuracy over speed may choose stronger isolation. High-throughput systems may prefer a level that balances consistency with concurrency. The decision affects data reliability, user experience and the system's ability to scale.
For this course, keep MySQL's default REPEATABLE READ. It provides a consistent snapshot for operations that read multiple related records.
When a write depends on an earlier read, add protection at the point where that decision is made:
- Use a conditional UPDATE when you need to ensure that a row has not changed since you read it.
- Use
SELECT ... FOR UPDATEwhen you need to lock a row before inspecting it and deciding what to do. - Lock a shared resource row when a business rule spans multiple records.
- Retry a transaction when InnoDB reports a deadlock with error 1213, provided the operation is safe to retry.
MySQL lets you configure the isolation level globally, for a session or for the next transaction.
-- Server-wide default for new sessions
SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED;
-- Current connection
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
-- Next transaction only
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;The current setting is exposed through transaction_isolation.
A transaction-level setting must be applied before the transaction starts. Attempting to change it inside an open transaction is an error.
In Go, you can choose the isolation level for an individual transaction through database/sql:
tx, err := db.BeginTx(ctx, &sql.TxOptions{
Isolation: sql.LevelReadCommitted,
})
if err != nil {
return err
}
defer func() { _ = tx.Rollback() }()This sets READ COMMITTED for that transaction without changing the database-wide default. The same approach works with sql.LevelRepeatableRead when you want to explicitly request the MySQL default.
Connection to order creation and lifecycle
W3B4 creates an order, its order lines, its first order event and its outbox row in one transaction. REPEATABLE READ provides a consistent snapshot for any plain reads involved in that transaction.
The atomicity of the transaction ensures that the order and its associated records commit together. Isolation determines how those operations interact with other transactions creating or changing orders.
W3B5 introduces the order state machine. Its transitions depend on the current status of an order. Two callers can read the same status and both attempt transitions that are individually legal.
That is the lost-update problem from this page. The next module introduces optimistic locking through orders.version, so a write can succeed only when the version still matches the one the caller read.
Keep the distinction clear: isolation controls database visibility and locking. Your application still needs to enforce the conditions under which a particular order transition is allowed.
Check your understanding
Two transactions operate on order 42. T1 updates its status to ACCEPTED but has not committed. T2 attempts to update it to CANCELLED. What happens?
References
Everything above is explained on this page; these are where it comes from (MySQL 8.4 InnoDB, Go 1.25 database/sql).