TestForge | Aidevops | 📊 Plogger ✍️ Blog 📚 Docs
plogger

AI DevOps Korea

Turn AI service development and operations into one improvement loop

Aidevops.kr covers LLMOps, RAG, agents, observability, evaluation, and cost-performance optimization for production AI services.

A Complete Guide to Database Transactions and Isolation Levels

A Complete Guide to Database Transactions and Isolation Levels diagram
Visual guide to the key flow, architecture, and decision points covered in this post.
## Structure at a Glance
T1: read ------ update ------ commit
T2:      read ------ read ------ commit

Isolation level decides:
- can T2 see uncommitted data?
- can T2 read different values twice?
- can new rows appear between reads?

Transaction isolation becomes much easier to understand when you think of it not as abstract theory, but as a question of what is allowed to happen between two overlapping transactions. Dirty Read, Non-Repeatable Read, and Phantom Read are all differences in what can be observed during that overlapping time window.

ACID Properties

PropertyDescription
AtomicityA transaction either succeeds completely or fails completely
ConsistencyData integrity is preserved before and after the transaction
IsolationConcurrent transactions do not interfere with each other
DurabilityCommitted data is stored permanently

Problems by Isolation Level

Isolation LevelDirty ReadNon-Repeatable ReadPhantom Read
READ UNCOMMITTEDYesYesYes
READ COMMITTEDNoYesYes
REPEATABLE READNoNoYes
SERIALIZABLENoNoNo

MySQL InnoDB default: REPEATABLE READ

Dirty Read

-- Transaction A
BEGIN;
UPDATE accounts SET balance = 0 WHERE id = 1;
-- Not committed yet

-- Transaction B (READ UNCOMMITTED)
SELECT balance FROM accounts WHERE id = 1;
-- Reads 0 -> if A rolls back, this was a wrong value

Non-Repeatable Read

-- Transaction A
BEGIN;
SELECT balance FROM accounts WHERE id = 1;  -- reads 1000

-- Transaction B
UPDATE accounts SET balance = 2000 WHERE id = 1;
COMMIT;

-- Transaction A (read again in the same transaction)
SELECT balance FROM accounts WHERE id = 1;  -- reads 2000 (changed!)

Phantom Read

-- Transaction A
BEGIN;
SELECT * FROM orders WHERE amount > 1000;  -- 3 rows

-- Transaction B
INSERT INTO orders (amount) VALUES (5000);
COMMIT;

-- Transaction A
SELECT * FROM orders WHERE amount > 1000;  -- 4 rows! (phantom row appears)

Configuring MySQL Isolation Levels

-- Session level
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;

-- Global level
SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ;

-- Check the current isolation level
SELECT @@transaction_isolation;

Optimistic vs. Pessimistic Locking

-- Pessimistic lock (SELECT FOR UPDATE)
BEGIN;
SELECT * FROM inventory WHERE id = 1 FOR UPDATE;
UPDATE inventory SET stock = stock - 1 WHERE id = 1;
COMMIT;

-- Optimistic lock (version column)
UPDATE products
SET stock = stock - 1, version = version + 1
WHERE id = 1 AND version = 5;
-- if affected rows = 0, conflict happened -> retry

What Gets Hard in Production

  • Transaction isolation is not an academic setting; it directly shapes correctness, latency, and contention under concurrency.
  • Teams often choose an isolation level by default and then debug anomalies without a shared model of what is allowed.
  • Higher isolation can protect correctness while quietly reducing throughput if access patterns are not designed for it.

Architecture Decisions That Matter

  • Start from the business invariant you must protect, not from the prestige of stronger isolation.
  • Design read-modify-write paths to minimize conflict windows.
  • Use locking, retries, and idempotency policies alongside isolation choices.

Practical Example

The right question is usually “what anomaly is unacceptable here?”

inventory reservation
  need to prevent oversell
  may require row lock or optimistic retry depending on workload

Anti-Patterns to Avoid

  • Using the default isolation level without knowing what anomalies remain possible.
  • Raising isolation globally when the hotspot exists in only a few workflows.
  • Ignoring retry design after introducing stricter concurrency control.

Operational Checklist

  • Document invariants for money, inventory, and ordering workflows.
  • Load-test contention scenarios, not just happy-path latency.
  • Measure deadlock and retry frequency.
  • Review lock scope and transaction length.

Final Judgment

Isolation level is a correctness budget decision. The best level is the lowest one that still protects the invariants you actually care about.

Continue Reading

Related posts

Next Path

Keep exploring this topic as a system